简体   繁体   English

如何获取 Ansible playbook 中使用的变量列表?

[英]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.我想获得 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看看vars

    - debug: var=vars

and you'll see all variables, some of them duplicated as attributes of hostvars你会看到所有的变量,其中一些作为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 .您可以看到剩下的是Special 变量和变量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.如果您创建这样的列表,您可以创建差异并仅获取您的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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