6

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 ?

Morgosus
  • 807
  • 5
  • 18
Peter
  • 113
  • 1
  • 6
  • I think you can have a try with `-vvv` option, when you execute the playbook, it will output all the variables which from the included vars files – Z.Liu Aug 28 '20 at 09:09

1 Answers1

7

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.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63