# Using with\_file with subkeys

**URL:** <https://forum.ansible.com/t/using-with-file-with-subkeys/24671>\
**Category:** Ansible Project\
**Created:** [June 7, 2017, 5:26pm UTC](https://forum.ansible.com/t/using-with-file-with-subkeys/24671 "2017-06-07T17:26:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo\_Ribeiro](https://avatars.discourse-cdn.com/v4/letter/h/bc79bd/32.png) [@Hugo\_Ribeiro](https://forum.ansible.com/u/Hugo_Ribeiro)\
**Post date:** [June 7, 2017, 5:26pm UTC](https://forum.ansible.com/t/using-with-file-with-subkeys/24671/1 "2017-06-07T17:26:06Z")

</div>

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’_

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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 8, 2017, 6:40pm UTC](https://forum.ansible.com/t/using-with-file-with-subkeys/24671/2 "2017-06-08T18:40:51Z")

</div>

> 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  
> &nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/hosts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: root  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: root  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: "{{ item.regexp }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ item.line }}"  
> &nbsp;&nbsp;&nbsp;with\_file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ 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.
