2

I recently started writing playbooks. Below is my task:

- name: apache templates 
  template:
     src: "{{ item.templatename }}"
     dest: /opt/apache/default/"{{ item.config }}"
  loop:
    - { templatename: 'apache_webconf.j2', config: 'web.conf' }
    - { templatename: 'apache_sitesconf.j2',  config: 'sites.conf' }

What i am trying to achieve here is that ansible should replace template with web.conf at destination. However, ansible is throwing the below error while running the playbook.

ERROR! The field 'loop' is supposed to be a string type, however the incoming data structure is a <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
Neeraj Kumar
  • 771
  • 2
  • 16
  • 37
learning fun
  • 519
  • 1
  • 5
  • 12

3 Answers3

4

I found the solution. All we have to do is to upgrade to ansible 2.5 because loop keyword is supported in only that version.

learning fun
  • 519
  • 1
  • 5
  • 12
2

Another alternative is to replace 'loop' with 'with_items'. I need to do this as for some reason, my machine can't upgrade ansible to at least v2.5 (where loop keyword is supported). My ansible is v2.4 and so far worked the same with the keyword 'with_items'.

icasimpan
  • 1,253
  • 3
  • 16
  • 28
0

If you're using Ansible on CentOS I have found that you need to install the epel-release to get past 2.4.x. That should allow you to use loops now without throwing this error.

yum install epel-release