How to get a list of variables used in Ansible playbook?

5.8k views Asked by At

I would like to get a list of variables used in an ansible playbook. I looked into the setup and debug module, but I doubt I can do this.

Is there any generic way ?

1

There are 1 answers

0
Vladimir Botka On

Take a look at vars

    - debug: var=vars

and you'll see all variables, some of them duplicated as attributes of hostvars

    - debug: var=hostvars

It's possible to list only variables that are not in hostvars. For example, the playbook

shell> cat playbook.yml
- hosts: test_01
  vars:
    var1: test
  tasks:
    - set_fact:
        my_vars: "{{ vars.keys()|
                     difference(hostvars[inventory_hostname].keys())|
                     list|
                     sort }}"
    - debug:
        var: my_vars

gives (abridged)

ok: [test_01] => {
    "my_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",
        "var1"
    ]
}

You can see that what's left are Special variables and the variable var1. I'm not aware of any method, filter, or function on how to list special variables only. If you create such a list you can create the difference and get your variables only.