I have wired problem with running ansible
I'd like to print to log or anywhere else the extra-vars
I'm executing the playbook with.
I am running the following command:
ansible-playbook /foo/main.yml --extra-vars "main_playbook=app_install start_path=work" --extra-vars {"db_config":{"db_multi_config":["value1","value2"]}}'
with the following main.yml
playbook
- name: start playbookimport_playbook: "{{ playbook_dir }}/{{ main_playbook}}/{{ start_path}}.yml"
I'd like to print the values of all extra-vars
passed on the command line before it gets to main.yml
. Is there any way to do this?
Best Answer
You can get this information in the output running ansible in full debug mode:
Given the following test.yml
playbook:
- hosts: localhostgather_facts: false
and running it with the command:
ansible-playbook test.yml -vvvv -e toto=bla -e '{"test1":2}'
I get the following result (see the debug lines for playbook before play starts)
ansible-playbook [core 2.11.3] config file = Noneconfigured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/local/lib/python3.8/dist-packages/ansibleansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/local/bin/ansible-playbookpython version = 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0]jinja version = 2.11.3libyaml = TrueNo config file found; using defaultsThe vault password file /home/user/bin/vault-keyring-client is a client script.Executing vault password client script: /home/user/bin/vault-keyring-client --vault-id avaultid1The vault password file /home/user/bin/vault-keyring-client is a client script.Executing vault password client script: /home/user/bin/vault-keyring-client --vault-id avaultid2The vault password file /home/user/bin/vault-keyring-client is a client script.Executing vault password client script: /home/user/bin/vault-keyring-client --vault-id avaultid3setting up inventory pluginshost_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() methodscript declined parsing /etc/ansible/hosts as it did not pass its verify_file() methodauto declined parsing /etc/ansible/hosts as it did not pass its verify_file() methodParsed /etc/ansible/hosts inventory source with ini pluginLoading callback plugin default of type stdout, v2.0 from /usr/local/lib/python3.8/dist-packages/ansible/plugins/callback/default.pySkipping callback 'default', as we already have a stdout callback.Skipping callback 'minimal', as we already have a stdout callback.Skipping callback 'oneline', as we already have a stdout callback.PLAYBOOK: test.yml **********************************************************************************************************************Positional arguments: test.ymlverbosity: 4connection: smarttimeout: 10become_method: sudotags: ('all',)inventory: ('/etc/ansible/hosts',)extra_vars: ('toto=bla', '{"test1":2}')forks: 51 plays in test.ymlPLAY [all] ******************************************************************************************************************************skipping: no hosts matchedPLAY RECAP ******************************************************************************************************************************
You can't separate extra vars from all other vars, because they are merged. But you can see all variables for the host. (except for setup
facts, which you can browse separately with -m setup
).
The trick is to print all variables from hostvars group for 'itself', ansible_host
:
ansible -i inventory -e extra_vars_here -m debug -a 'msg={{hostvars[inventory_hostname]}}' all
(replace all
with specific hostname if you wish).
If you are overwhelmed, you can filter it with .keys()
: hostvars[inventory_hostname].keys()
.