Hi all,
this will probably sound very basic but I tried my best to find a solution, without success. This is what I’m trying to achieve:
- name: sample task
lineinfile:
path: /etc/hosts
state: present
owner: root
group: root
mode: 0644
regexp: “{{ item.regexp }}”
line: “{{ item.line }}”
with_file:
And the contents of sample_file:
-
{ regexp: ‘abc’, line: ‘abc’ }
-
{ regexp: ‘xyz’, line: ‘xyz’ }
This is pretty much how the with_file documentation illustrates the use, with the exception that it doesn’t make reference to the use of subkeys like {{ item.regexp }}, but instead only single values (e.g. {{ item }}). During run, ansible will complain that regexp isn’t defined:
the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘ansible.vars.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘regexp’
Is there a way to achieve I’m trying to do? Is with_file the right method for this?
Thanks a lot for your help.
Hi all,
this will probably sound very basic but I tried my best to find a solution,
without success. This is what I'm trying to achieve:
- name: sample task
lineinfile:
path: /etc/hosts
state: present
owner: root
group: root
mode: 0644
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_file:
- "{{ sample_file }}"
And the contents of sample_file:
- { regexp: 'abc', line: 'abc' }
- { regexp: 'xyz', line: 'xyz' }
This is pretty much how the with_file documentation illustrates the use,
with the exception that it doesn't make reference to the use of subkeys
like {{ item.regexp }}, but instead only single values (e.g. {{ item }}).
During run, ansible will complain that regexp isn't defined:
*the field 'args' has an invalid value, which appears to include a variable
that is undefined. The error was:
'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute
'regexp'*
The with_file read one and one line and the lines is a string.
Since they are just stings one can't do item.regexp.
Is there a way to achieve I'm trying to do? Is with_file the right method
for this?
Probably, but without context it's hard to give a solution.
You can put the content in the file in a variable instead and use that, or make it a variable in a file and use include_vars to read the file.