Automation 模板中Ansible中的通配符

Automation 模板中Ansible中的通配符,automation,ansible,Automation,Ansible,是否有人试图在模板或剧本中使用通配符 通配符在目录和列表主机中工作,但在模板或playbook中不工作 以下命令工作: ansible -i inventory/ec2.py tag_Name_Hbase* --list-host` 但同样的事情在剧本中不起作用 示例(不起作用): 示例(工作): dict key的通配符无效。您需要迭代group.keys() playbook.yml: --- - hosts: all gather_facts: no vars: Nod

是否有人试图在模板或剧本中使用通配符

通配符在目录和列表主机中工作,但在模板或playbook中不工作

以下命令工作:

ansible -i inventory/ec2.py tag_Name_Hbase*  --list-host`
但同样的事情在剧本中不起作用

示例(不起作用):

示例(工作):


dict key的通配符无效。您需要迭代
group.keys()

playbook.yml:

---
- hosts: all
  gather_facts: no
  vars:
    Node: |
      {% set o = [] %}
      {%- for i in groups.keys() %}
        {%- if i.startswith("tag_Name_Zookeeper") %}
          {%- for j in groups[i] %}
            {%- if o.append(j+":2181") %}
            {%- endif %}
          {%- endfor %}
        {%- endif %}
      {% endfor %}
      {{ ",".join(o) }}
  tasks:
    - debug:
        var: Node
      run_once: yes
      delegate_to: localhost
主持人:

[tag_Name_Zookeeper_1]
a
b
[tag_Name_Zookeeper_2]
c
d
[tag_Name_Zookeeper_3]
e
f
[others]
localhost
示例会话:

$ ansible-playbook -i hosts playbook.yml 

PLAY [all] ******************************************************************** 

TASK: [debug ] **************************************************************** 
ok: [a -> localhost] => {
    "var": {
        "Node": "a:2181,b:2181,c:2181,d:2181,e:2181,f:2181"
    }
}

PLAY RECAP ******************************************************************** 
a                          : ok=1    changed=0    unreachable=0    failed=0   
b                          : ok=1    changed=0    unreachable=0    failed=0   
c                          : ok=1    changed=0    unreachable=0    failed=0   
d                          : ok=1    changed=0    unreachable=0    failed=0   
e                          : ok=1    changed=0    unreachable=0    failed=0   
f                          : ok=1    changed=0    unreachable=0    failed=0   
localhost                  : ok=1    changed=0    unreachable=0    failed=0   
[tag_Name_Zookeeper_1]
a
b
[tag_Name_Zookeeper_2]
c
d
[tag_Name_Zookeeper_3]
e
f
[others]
localhost
$ ansible-playbook -i hosts playbook.yml 

PLAY [all] ******************************************************************** 

TASK: [debug ] **************************************************************** 
ok: [a -> localhost] => {
    "var": {
        "Node": "a:2181,b:2181,c:2181,d:2181,e:2181,f:2181"
    }
}

PLAY RECAP ******************************************************************** 
a                          : ok=1    changed=0    unreachable=0    failed=0   
b                          : ok=1    changed=0    unreachable=0    failed=0   
c                          : ok=1    changed=0    unreachable=0    failed=0   
d                          : ok=1    changed=0    unreachable=0    failed=0   
e                          : ok=1    changed=0    unreachable=0    failed=0   
f                          : ok=1    changed=0    unreachable=0    failed=0   
localhost                  : ok=1    changed=0    unreachable=0    failed=0