# Error in play using 'lineinfile' module

**URL:** <https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [November 3, 2014, 6:55pm UTC](https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674 "2014-11-03T18:55:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [November 3, 2014, 6:55pm UTC](https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674/1 "2014-11-03T18:55:37Z")

</div>

Hi all,

Trying to add a line (or modify if existing) to “sshd\_config” to lock down who can SSH to the target server. On RHEL, there is no “AllowGroups” (or “AllowUsers”) line present in the as-shipped sshd\_config file. In any case, here is the play I wrote:

- name: RHELFAM | Restrict SSH on Docker hosts to specific group members  
lineinfile:
- dest=/etc/ssh/sshd\_config
- state=present
- regexp='^AllowGroups ’
- line=‘AllowGroups {{ ssh\_allow\_groups }}’
- validate=‘/usr/sbin/sshd -t %s’  
when: ssh\_allow\_groups is defined  
notify:
- reload sshd  
tags: limitsshusers

Then in roles//vars/main.yml:

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [November 4, 2014, 7:53am UTC](https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674/2 "2014-11-04T07:53:52Z")

</div>

You are passing a list to lineinfile, and you should either pass in a string or a dictionary:

&nbsp;&nbsp;lineinfile: arg1=val1 foo=bar

Or (IMHO better, as it has less problems with escaping):

&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;arg1: 'val1'  
&nbsp;&nbsp;&nbsp;&nbsp;foo='bar'

Willard Dennis \<willard.dennis@gmail.com\> napisał:

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [November 4, 2014, 7:56am UTC](https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674/3 "2014-11-04T07:56:47Z")

</div>

Argh, the second example should say “foo: bar” instead of foo=bar 🙂

Tomasz Kontusz [tomasz.kontusz@gmail.com](mailto:tomasz.kontusz@gmail.com) napisał:

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [November 4, 2014, 9:33pm UTC](https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674/4 "2014-11-04T21:33:37Z")

</div>

Thanks, Tomasz, for the explanation – wasn’t aware that the ‘lineinfile’ module couldn’t accept list input.

Now, my question is: how to take a YAML list (such as the groups list in my vars file), form a string of the format of “group1 group2”, and use that in lineinfile?

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [November 4, 2014, 11:05pm UTC](https://forum.ansible.com/t/error-in-play-using-lineinfile-module/16674/5 "2014-11-04T23:05:36Z")

</div>

OK, I found the answer (yay RTFM!) in [http://docs.ansible.com/playbooks\_variables.html#other-useful-filters](http://docs.ansible.com/playbooks_variables.html#other-useful-filters)

Working play is:

`

- name: RHELFAM | Restrict SSH on Docker hosts to specific group members  
lineinfile: dest=/etc/ssh/sshd\_config  
state=present  
regexp=‘^AllowGroups’  
line=‘AllowGroups {{ ssh\_allowed\_groups | join(" ") }}’  
backup=yes  
validate=‘sshd -t -f %s’  
when: ssh\_allowed\_groups is defined  
notify:
- restart sshd  
tags: limitsshusers

`

Variable filters FTW!

Thanks all,  
W.
