I want the following task to be executed only when etc_hosts is defined
- name: other important hosts in /etc/hosts
lineinfile: >
dest=‘/etc/hosts’
regexp=‘.*{{ item.hostname }}$’
line=‘{{ item.address }} {{ item.hostname }}’
state=‘present’
with_items: - ‘{{ etc_hosts }}’
All these tasks fail for one reason or another.
- name: other important hosts in /etc/hosts
lineinfile: >
dest=‘/etc/hosts’
regexp=‘.*{{ item.hostname }}$’
line=‘{{ item.address }} {{ item.hostname }}’
state=‘present’
when: etc_hosts is defined
with_items: - ‘{{ etc_hosts }}’
FAILED! => {“failed”: true, “msg”: “ERROR! ‘etc_hosts’ is undefined”}
- name: other important hosts in /etc/hosts
lineinfile: >
dest=‘/etc/hosts’
regexp=‘.*{{ item.hostname }}$’
line=‘{{ item.address }} {{ item.hostname }}’
state=‘present’
when: {{ etc_hosts }} is defined
with_items: - ‘{{ etc_hosts }}’
The offending line appears to be:
state=‘present’
when: {{ etc_hosts }} is defined
^ here
- name: other important hosts in /etc/hosts
lineinfile: >
dest=‘/etc/hosts’
regexp=‘.*{{ item.hostname }}$’
line=‘{{ item.address }} {{ item.hostname }}’
state=‘present’
when: “{{ etc_hosts }}” is defined
with_items: - ‘{{ etc_hosts }}’
The offending line appears to be:
state=‘present’
when: “{{ etc_hosts }}” is defined
^ here