# Add a username in the sshd config file,

**URL:** <https://forum.ansible.com/t/add-a-username-in-the-sshd-config-file/4928>\
**Category:** Get Help\
**Tags:** ssh, playbook\
**Created:** [April 12, 2024, 1:00pm UTC](https://forum.ansible.com/t/add-a-username-in-the-sshd-config-file/4928 "2024-04-12T13:00:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unwissende](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/unwissende/32/2881_2.png) [@unwissende](https://forum.ansible.com/u/unwissende)\
**Post date:** [April 12, 2024, 1:00pm UTC](https://forum.ansible.com/t/add-a-username-in-the-sshd-config-file/4928/1 "2024-04-12T13:00:30Z")

</div>

Hello everyone,

i have a question. I’m trying to create a new user for all of my virtual servers. The creating works fine, only issue i have is the sshd config file. In this file we have one Line “AllowedUsers: User1 User2 User3 …”

I tried to create a playbook to add User4 in that file. This is how my playbook looks like:

```
 - name: Find
   lineinfile:
     dest: /etc/ssh/sshd_testcfg
     line: "user4"
   register: presence

 - name: Add string after pattern
   lineinfile:
     path: /etc/ssh/sshd_testcfg
     regexp: '^(AllowUsers.+)(\n?)$'
     line: '\1user4'
   when: presence is failed

```

The result i need is:  
User1 User2 User3 User 4  
While Ansible does this:  
User1 User2 User3  
User 4

Can someone help me please?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 12, 2024, 1:13pm UTC](https://forum.ansible.com/t/add-a-username-in-the-sshd-config-file/4928/2 "2024-04-12T13:13:59Z")

</div>

You need to provide the whole of the desired line to `lineinfile` not just the value to append.

Personally I’d probably use the [JC sshd parser](https://kellyjonbrazil.github.io/jc/docs/parsers/sshd_conf) with the [JC filter](https://docs.ansible.com/ansible/latest/collections/community/general/jc_filter.html) to read the existing configuration.

Also since [AllowUsers](https://man.openbsd.org/sshd_config#AllowUsers) _“may appear multiple times in `sshd_config` with each instance appending to the list”_, I’d suggest that if your SSHD is recent enough to support [Include](https://man.openbsd.org/sshd_config#Include) and has a directive like this in it already:

```auto
Include /etc/ssh/sshd_config.d/*.conf

```

Then you might be best off simply writing one file per user to this directory, for example a `/etc/ssh/sshd_config.d/user4.conf` file containing:

```auto
AllowUser user4

```

Of course there are lots of different ways to achieve what you want and the above are just my initial thoughts on it, there might well be a better way…
