0

I am having an issue with my Ansible playbook printing every file in the directory that the .yml file is in. This only happens when I run it in a bash script.

So say that I had

 /MY_ANSIBLE
  dumbPlaybook.yml
  some_other_file1.txt
  some_other_file2.txt
  some_other_file3.txt
  some_other_file4.txt

Shell script that calls my playbook and looks for errors. I want to do this:

do_stuff_res=`doStuff`
echo $do_stuff_res

because that way I can execute the command AND see the output from the level of my bash script. (one which runs a bunch of playbooks)

doStuff () {
    cd /web/MY_ANSIBLE/ansible/
    # If I run this line alone I DO NOT see the printout of all the files
    /home/MY_ANSIBLE/.local/bin/ansible-playbook dumbPlaybook.yml
    cd -
}

do_stuff_res=`doStuff`
echo $do_stuff_res
if echo $do_stuff_res | grep -q 'failed=1'; then
  echo "Failed!!!"
  exit 1
fi

The playbook:

- hosts: localhost
  tasks:
    - name: RANDOM DEBUG
      debug: 
        msg: "Foo"

If I call that shell script, I get this output:

/home/MY_ANSIBLE/.local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
  from cryptography.exceptions import InvalidSignature
/usr/local/lib/python3.6/dist-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
Using /web/MY_ANSIBLE/ansible/ansible.cfg as config file PLAY [localhost] 

[[ ... here it will print a HUGE list of EVERY single file in the directory of my playbook ... ]]

dumbPlaybook.yml some_other_file1.txt some_other_file2.txt some_other_file3.txt some_other_file4.txt

127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 /web/MY_ANSIBLE/ansible

ansible.cfg, in case that's relevant:

[defaults]
inventory = inventory/ansible.host
host_key_checking=False
deprecation_warnings=False
timeout = 120
verbosity = 1

This becomes incredibly tedious to read as there are many playbooks that ALL do this.

clayton groth
  • 199
  • 2
  • 16
  • I have copied your script and run it and get the expected output `TASK [RANDOM DEBUG] foo main.yml script.sh Thursday 27 October 2022 19:30:43 +0100 (0:00:00.997) 0:00:01.018 foo main.yml script.sh ok: [localhost] => { "msg": "Foo" }` – Chris Doyle Oct 27 '22 at 18:32
  • 1
    `echo $do_stuff_res` -- quote your variables. – glenn jackman Oct 27 '22 at 18:33
  • For future reference for anyone it may help, this solved my problem: ```shell #!/bin/bash doStuff () { cd /web/cquant/ansible/ /home/cquant/.local/bin/ansible-playbook dumbPlaybook.yml --extra-vars "domain_name=devtest.cquant.io" 2>&1 | tee blah.txt cd - } doStuff do_stuff_res=`cat blah.txt` if echo $do_stuff_res | grep -q 'failed=1'; then echo "Failed!!!" exit 1 fi ``` – clayton groth Oct 27 '22 at 18:53

0 Answers0