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

问题描述

我想获得一本ansible剧本中使用的变量的列表。 我研究了设置和调试模块,但我怀疑我可以做到这一点。

有什么通用的方法吗?

解决方法

看看vars

    - debug: var=vars

,您将看到所有变量,其中一些重复作为hostvars的属性

    - debug: var=hostvars

可能只列出不在hostvars中的变量。例如,剧本

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

给予(删节)

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"
    ]
}

您可以看到剩下的是Special variables和变量var1。我不知道如何仅列出特殊变量的任何方法,过滤器或函数。如果创建这样的列表,则可以创建差异并仅获取变量。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...