在Jinja2模板中使用带有循环的Ansible Array变量

时间:2018-02-14 15:51:06

标签: dictionary ansible jinja2

这是我的配置数组。

    tomcatsconfs:
    - {instance: tc-p1_i01_3001,  port: 30011, connector: ajp-nio-, connector_port: 30012}
    - {instance: tc-p1_i02_3002,  port: 30021, connector: ajp-nio-, connector_port: 30022}
    - {instance: tc-p1_i03_3003,  port: 30031, connector: ajp-nio-, connector_port: 30032}

现在我想用这些任务创建一个带有jinja2模板的nrpe.cfg:

    - name: copy nrpe.conf from template
      template: src=nrpe.cfg.j2 dest=/etc/nagios/nrpe.cfg mode=0644 owner=root group=root
      with_items:
         - tomcatsconfs

Ansible将此数组作为字典传输。

    +[{u'connector': u'ajp-nio-', u'instance': u'tc-p1_i01_3001', u'connector_port': 30012, u'port': 30011}, {u'connector': u'ajp-nio-', u'instance': u'tc-p1_i02_3002', u'connector_port': 30022, u'port': 30021}, {u'connector': u'ajp-nio-', u'instance': u'tc-p1_i03_3003', u'connector_port': 30032, u'port': 30031}]

我尝试用这个循环迭代这个字典

    {% for key value  in tomcatconfs.iteritems() %}
        key value
    {% endfor %}

但我收到错误消息:

    failed: [host] (item=tomcatconfs) => {"failed": true, "item": "tomcatconfs", "msg": "AnsibleUndefinedVariable: 'list object' has no attribute 'iteritems'"}

如何在此模板中迭代此词典?

问候niesel

2 个答案:

答案 0 :(得分:1)

我用过这个。

---
- name: Run Ansible
  hosts: 127.0.0.1
  connection: local
  gather_facts: true
  vars:
    tomcatsconfs:
    - {instance: tc-p1_i01_3001,  port: 30011, connector: ajp-nio-, connector_port: 30012}
    - {instance: tc-p1_i02_3002,  port: 30021, connector: ajp-nio-, connector_port: 30022}
    - {instance: tc-p1_i03_3003,  port: 30031, connector: ajp-nio-, connector_port: 30032}

  tasks:
    - name: Testing Iteration
      copy:
        dest: /tmp/testtemp
        content: |
          {% for var in tomcatsconfs %}
          instance: {{ var.instance }}
          port: {{ var.port }}
          connector: {{ var.connector }}
          connector_port: {{ var.connector_port }}

      {% endfor %}

输出:

instance: tc-p1_i01_3001
port: 30011
connector: ajp-nio-
connector_port: 30012

instance: tc-p1_i02_3002
port: 30021
connector: ajp-nio-
connector_port: 30022

instance: tc-p1_i03_3003
port: 30031
connector: ajp-nio-
connector_port: 30032

答案 1 :(得分:0)

我认为您需要更改的是如何将列表传递给with_items。尝试更改

- name: copy nrpe.conf from template
  template: src=nrpe.cfg.j2 dest=/etc/nagios/nrpe.cfg mode=0644 owner=root group=root
  with_items: 
     - tomcatsconfs 

    - name: copy nrpe.conf from template
      template: src=nrpe.cfg.j2 dest=/etc/nagios/nrpe.cfg mode=0644 owner=root group=root
      with_items: "{{ tomcatsconfs }}"

我认为发生的事情是你给with_items列出了一个列表。如果你把它改成我在我的例子中所拥有的,你只是给它列表。

这用我简化的样本剧本修正了它:

---         
- hosts: localhost
  connection: local
  vars:
    tomcatsconfs:
      - {instance: tc-p1_i01_3001,  port: 30011, connector: ajp-nio-, connector_port: 30012}
      - {instance: tc-p1_i02_3002,  port: 30021, connector: ajp-nio-, connector_port: 30022}
      - {instance: tc-p1_i03_3003,  port: 30031, connector: ajp-nio-, connector_port: 30032}
     tasks:
       - debug: var="{{item}}"
         with_items:
           - tomcatsconfs

       - debug: var="{{item['port']}}"
         with_items: "{{ tomcatsconfs }}"