pacoverflow
pacoverflow

Reputation: 3881

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

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  

Upvotes: 12

Views: 72802

Answers (3)

HanniBaL90
HanniBaL90

Reputation: 625

Looking for an answer to the same question, I found the following solution from [link is no good now]:

- name: Display all variables/facts known for a host
  debug:
    var: hostvars[inventory_hostname]
  tags: debug_info

Upvotes: 12

Jim Panse
Jim Panse

Reputation: 11

You can get all Host Variables on the commandline output:

ansible all -m setup -i IP-ADDRESS-HERE,

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68124

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

Upvotes: 4

Related Questions