# Nesting multiple with\_items?

**URL:** <https://forum.ansible.com/t/nesting-multiple-with-items/15803>\
**Category:** Ansible Project\
**Created:** [July 29, 2014, 5:33pm UTC](https://forum.ansible.com/t/nesting-multiple-with-items/15803 "2014-07-29T17:33:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Oliver](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@John\_Oliver](https://forum.ansible.com/u/John_Oliver)\
**Post date:** [July 29, 2014, 5:33pm UTC](https://forum.ansible.com/t/nesting-multiple-with-items/15803/1 "2014-07-29T17:33:44Z")

</div>

So, I want to add two lines to each of two files. Looking to see if lineinfile could add multiple lines, I found a post that seemed to indicate it could not.

1. Is it possible to make lineinfile add multiple lines?

2. If not, is there a way to do something like:

- name: Lock accounts after 3 unsuccessful logon attempts  
lineinfile: dest=/etc/pam.d/{{item1}}-auth-ac  
regexp=‘^auth\ [default=die]\ pam\_faillock.so\ authfail\ deny=3\ unlock\_time=604800\ fail\_interval=900’  
insertafter=‘^auth._pam\_unix.so._’ line=‘$item2’  
with\_items2:
- ‘auth [default=die] pam\_faillock.so authfail deny=3 unlock\_time=604800 fail\_interval=900’
- ‘auth required pam\_faillock.so authsucc deny=3 unlock\_time=604800 fail\_interval=900’  
with\_items1:
- system
- password

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 29, 2014, 9:01pm UTC](https://forum.ansible.com/t/nesting-multiple-with-items/15803/2 "2014-07-29T21:01:33Z")

</div>

Look up “with\_nested” for this one.

However ansible is not going to like you adding key=value arguments in recent versions that way. I’d try passing a hash of values instead.

Also seems the $item2 might be from a very old ansible version and you mean “{{item2}}”

Any any case, with\_nested should hook you up and let us know if you need more info!

---

<div class="post-metadata">

**Author:** ![John\_Oliver](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@John\_Oliver](https://forum.ansible.com/u/John_Oliver)\
**Post date:** [July 29, 2014, 9:14pm UTC](https://forum.ansible.com/t/nesting-multiple-with-items/15803/3 "2014-07-29T21:14:18Z")

</div>

Thanks.

I’m not quite sure what you mean by “pass a has of values”… I’m envisioning an md5 or aha checksum, and scratching my head 🙂

And I think that ‘$item2’ came from some example I dug up on the Internet.

I wound up with:

lineinfile: dest=/etc/pam.d/{{ item[0] }}-auth-ac  
regexp=‘^auth\ [default=die]\ pam\_faillock.so\ authfail\ deny=3\ unlock\_time=604800\ fail\_interval=900’  
insertafter=‘^auth._pam\_unix.so._’  
line={{ line[1] }}  
with\_nested:

- [‘system’ ‘password’]
- [‘auth [default=die] pam\_faillock.so authfail deny=3 unlock\_time=604800 fail\_interval=900’ ‘auth required pam\_faillock.so authsucc deny=3 unlock\_time=604800 fail\_interval=900’ ]

but have little confidence because of your remark 🙂 I’ll have to stand up another VM to test this set of recipes.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 29, 2014, 11:14pm UTC](https://forum.ansible.com/t/nesting-multiple-with-items/15803/4 "2014-07-29T23:14:50Z")

</div>

Sorry I was wrong about the key=value stuff as that’s all for your config file, ignore that part.

I would however, change this, which isn’t valid syntax on the first term at least to:

with\_nested:

- [‘system’ ‘password’]
- [‘auth [default=die] pam\_faillock.so authfail deny=3 unlock\_time=604800 fail\_interval=900’ 'auth required pam\_faillock.so authsucc deny=3 unlock\_time=604800

with\_nested:

- listone
- listtwo

to make it a little cleaner, then up in vars, structure like so:

vars:  
listone:

- system
- password  
listtwo:
- ‘really long options string one’
- ‘really long options string two’

And in the task:

{{ item.1 }} and {{ item.2 }} are your variables.

It may also be cleaner to just have two tasks unless you want both settings in both files. I’m not sure.
