简体   繁体   中英

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

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM