代码之家  ›  专栏  ›  技术社区  ›  Vladimir Botka

可转换变量通配符选择

  •  2
  • Vladimir Botka  · 技术社区  · 7 年前

    我发现用通配符选择变量的唯一方法是循环所有变量并测试 match . 例如

      tasks:
        - debug:
            var: item
          loop: "{{ query('dict', hostvars[inventory_hostname]) }}"
          when: item.key is match("^.*_python_.*$")
    
    > ansible-playbook test.yml | grep \"key\":
        "key": "ansible_selinux_python_present", 
        "key": "ansible_python_version",
    

    有更有效的方法吗?

    既不 JSONYQuy查询(?)键= =“名称”) 也不 查找(“vars”、“name”) 使用通配符。

    是否有其他“启用通配符”的测试、筛选…?

    注:Regex_搜索在 What is the syntax within the regex_search() to match against a variable?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Konstantin Suvorov    7 年前

    你可以 select / reject 使用Jinja测试:

    - debug:
        msg: "{{ lookup('vars', item) }}"
      loop: "{{ hostvars[inventory_hostname].keys() | select('match', '^.*_python_.*$') | list }}"
    

    给予:

    ok: [localhost] => (item=ansible_selinux_python_present) => {
        "msg": false
    }
    ok: [localhost] => (item=ansible_python_version) => {
        "msg": "2.7.10"
    }