I have the following task/main.yml part of my playbook. Everything works fine except for the last section with the blockinfile. I assume I have some sort of a syntax issue.
`
Enter code here…—
-
name: Dependency resolution – Install selinux bindings
yum:
name: libselinux-python
state: present -
name: Gather list of ifcfg-* files
shell: ls “{{ net_path }}” | grep ^ifcfg- | grep -ve ifcfg-lo -e @ # Last piece (@) excludes ansible backup files
register: ifcfg_list
changed_when: false -
name: DEBUG
debug:
msg: “{{net_path}}{{ item }}”
with_items: “{{ ifcfg_list.stdout_lines }}” -
name: Removing existing DNS entries from ifcfg-* files
lineinfile:
path: “{{ net_path }}{{ item }}”
backup: yes
state: absent
regexp: ‘^DNS’
with_items: “{{ ifcfg_list.stdout_lines }}” -
name: Removing existing DNS entries from resolv.conf
lineinfile:
path: /etc/resolv.conf
backup: yes
state: absent
regexp: ‘^nameserver’ -
name: Update DNS in ifcfg files
blockinfile:
path: “{{ net_path }}{{ item }}”
marker: ‘# {mark} ANSIBLE MANAGED BLOCK – Manual changes will be overwritten’
insertafter: EOF
block: |
DNS1=“{{ dns1 }}”
DNS2=“{{ dns2 }}”
with_items: “{{ ifcfg_list.stdout_lines }}”
`
As you can see from the following results, the DEBUG iterates through it fine, but I fail in the Update DNS section. It says the variable is undefined, but I can’t see my problem. Wondering if someone can help me out.
`
PLAY [ansibletest-oel6] ******************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
ok: [ansibletest-oel6]
TASK [dns_update : Dependency resolution – Install selinux bindings] ********************************************************************************************************************************************************************
ok: [ansibletest-oel6]
TASK [dns_update : Gather list of ifcfg-* files] *****************************************************************************************************************************************************************************************
ok: [ansibletest-oel6]
TASK [dns_update : DEBUG] ****************************************************************************************************************************************************************************************************************
ok: [ansibletest-oel6] => (item=ifcfg-eth0) => {
“item”: “ifcfg-eth0”,
“msg”: “/etc/sysconfig/network-scripts/ifcfg-eth0”
}
ok: [ansibletest-oel6] => (item=ifcfg-eth1) => {
“item”: “ifcfg-eth1”,
“msg”: “/etc/sysconfig/network-scripts/ifcfg-eth1”
}
TASK [dns_update : Removing existing DNS entries from ifcfg-* files] *********************************************************************************************************************************************************************
ok: [ansibletest-oel6] => (item=ifcfg-eth0)
ok: [ansibletest-oel6] => (item=ifcfg-eth1)
TASK [dns_update : Removing existing DNS entries from resolv.conf] ***********************************************************************************************************************************************************************
ok: [ansibletest-oel6]
TASK [dns_update : Update DNS in ifcfg files] ********************************************************************************************************************************************************************************************
fatal: [ansibletest-oel6]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/etc/ansible/roles/dns_update/tasks/service.yml’: line 32, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Update DNS in ifcfg files\n ^ here\n”}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
ansibletest-oel6 : ok=6 changed=0 unreachable=0 failed=1
`