6

An answer on StackOverflow suggests using - debug: var=vars or - debug: var=hostvars to print out all the variables used by an Ansible playbook.

Using var=hostvars did not print out all of the variables. But I did get all of the variables printed out when I added the following lines to the top of the main.yml file of the role executed by my playbook:

- name: print all variables
  debug:
    var=vars

The problem is that the values of the variables printed out are not fully evaluated if they are dependent on the values of other variables. For example, here is a portion of what gets printed out:

"env": "dev", 
"rpm_repo": "project-subproject-rpm-{{env}}",
"index_prefix": "project{{ ('') if (env=='prod') else ('_' + env) }}",
"our_server": "{{ ('0.0.0.0') if (env=='dev') else ('192.168.100.200:9997') }}",

How can I get Ansible to print out the variables fully evaluated like this?

"env": "dev", 
"rpm_repo": "project-subproject-rpm-dev",
"index_prefix": "project_dev",
"our_server": "0.0.0.0",

EDIT:

After incorporating the tasks section in the answer into my playbook file and removing the roles section, my playbook file looks like the following (where install-vars.yml contains some variable definitions):

- hosts: all
  become: true
  vars_files:
    - install-vars.yml
  tasks:
    - debug:
        msg: |-
          {% for k in _my_vars %}
          {{ k }}: {{ lookup('vars', k) }}
          {% endfor %}
      vars:
        _special_vars:
          - ansible_dependent_role_names
          - ansible_play_batch
          - ansible_play_hosts
          - ansible_play_hosts_all
          - ansible_play_name
          - ansible_play_role_names
          - ansible_role_names
          - environment
          - hostvars
          - play_hosts
          - role_names
        _hostvars: "{{ hostvars[inventory_hostname].keys() }}"
        _my_vars: "{{ vars.keys()|
                      difference(_hostvars)|
                      difference(_special_vars)|
                      reject('match', '^_.*$')|
                      list|
                      sort }}"

When I try to run the playbook, I get this failure:

shell> ansible-playbook playbook.yml 
SSH password: 
SUDO password[defaults to SSH password]: 

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.100.111]

TASK [debug] *******************************************************************
fatal: [192.168.100.111]: FAILED! => {"failed": true, "msg": "lookup plugin (vars) not found"}
to retry, use: --limit @/usr/local/project-directory/installer-1.0.0.0/playbook.retry

PLAY RECAP *********************************************************************
192.168.100.111             : ok=1    changed=0    unreachable=0    failed=1  
pacoverflow
  • 3,726
  • 11
  • 41
  • 71
  • The error is: ``"lookup plugin (vars) not found"``. This plugin was added to Ansible 2.5. What version do you use? – Vladimir Botka May 25 '22 at 03:28
  • Try ``{{ k }}: {{ vars[k] }}`` – Vladimir Botka May 25 '22 at 03:31
  • @VladimirBotka I'm using Ansible 2.2.0.0. When I replaced `{{ k }}: {{ lookup('vars', k) }}` with `{{ k }}: {{ vars[k] }}`, I got it to print out the variables (all on one line with `\n` between them). However, it did not evaluate a variable - for example it printed out `test_var2: {{ test_var1 }}\n`. – pacoverflow May 25 '22 at 06:37
  • 1
    I can't help you with Ansible 2.2. It's pretty outdated don't you think? Install [supported version](https://docs.ansible.com/ansible/devel/reference_appendices/release_and_maintenance.html). – Vladimir Botka May 25 '22 at 08:21

2 Answers2

5

Looking for an answer to the same question, I found the following solution from this link:

- name: Display all variables/facts known for a host
  debug:
    var: hostvars[inventory_hostname]
  tags: debug_info
HanniBaL90
  • 535
  • 6
  • 18
2

The minimal playbook below

shell> cat pb.yml
- hosts: localhost
  gather_facts: false
  vars:
    test_var1: A
    test_var2: "{{ test_var1 }}"
  tasks:
    - debug:
        var: vars

reproduces the problem you described. For example,

shell> ansible-playbook pb.yml | grep test_var
    test_var1: A
    test_var2: '{{ test_var1 }}'

Q: How can I print out the actual values of all the variables used by an Ansible playbook?

A: You can get the actual values of the variables when you evaluate them. For example,

shell> cat pb.yml
- hosts: localhost
  gather_facts: false
  vars:
    test_var1: A
    test_var2: "{{ test_var1 }}"
  tasks:
    - debug:
        msg: |-
          {% for k in _my_vars %}
          {{ k }}: {{ lookup('vars', k) }}
          {% endfor %}
      vars:
        _special_vars:
          - ansible_dependent_role_names
          - ansible_play_batch
          - ansible_play_hosts
          - ansible_play_hosts_all
          - ansible_play_name
          - ansible_play_role_names
          - ansible_role_names
          - environment
          - hostvars
          - play_hosts
          - role_names
        _hostvars: "{{ hostvars[inventory_hostname].keys() }}"
        _my_vars: "{{ vars.keys()|
                      difference(_hostvars)|
                      difference(_special_vars)|
                      reject('match', '^_.*$')|
                      list|
                      sort }}"

gives the evaluated playbook's vars

  msg: |-
    test_var1: A
    test_var2: A
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63