0

This is my configuration array.

    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}

Now I woul like to create a nrpe.cfg with a jinja2 template with these task:

    - 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 transfers this array as a dictionary.

    +[{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}]

And I try to iterate this dictionary with this loop

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

But I get the error message:

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

How I can iterate this dictionary in this template?

Greetings niesel

Georg Gutsche
  • 452
  • 4
  • 9
  • 17

2 Answers2

1

I used this.

---
- 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 %}

OUTPUT:

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
0

I think all you need to change is how you are passing the list to with_items. Try changing

- 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 

to

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

I think what is going on is that you are giving with_items a list of one list. If you change it to what I have in my example, you are just giving it the list.

This fixed it with my simplified sample playbook:

---         
- 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 }}"
A-Beck
  • 1
  • 2