# SSH users via ansible

**URL:** <https://forum.ansible.com/t/ssh-users-via-ansible/14652>\
**Category:** Ansible Project\
**Created:** [February 25, 2014, 4:58pm UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652 "2014-02-25T16:58:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_Sukharev](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Petr\_Sukharev](https://forum.ansible.com/u/Petr_Sukharev)\
**Post date:** [February 25, 2014, 4:58pm UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/1 "2014-02-25T16:58:26Z")

</div>

Hello!

I am newby in ansible world, but i like this tool and i try to use in my enviroment…  
I have some question about user managment, and can’t get solution ☹  
I have several projects (test, developer, production and same) I make separate folders for this projects with contain hosts, main.yml, ansible.cfg and roles folder… I try to add user accounts by adding special role- user\_ssh, like this:

> - name: Add ssh user  
> user: name={{ item.user }} shell={{ item.shell }} groups=‘admins’  
> with\_items:
> - $ssh\_users

Variable ssh\_users i use in play-book and use it for any host grous. It looks like this:

> - hosts: apps  
> vars:
> - ssh\_users:
> - $user1
> - $user2

In next group (db for example) i use different values for ssh\_users variable (like user1 and user3)

Main question is how to grab file with users parameters and use it in this role?.. I have no luck two days in row.  
How to deal with this situation, or use other strategy?..

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 26, 2014, 4:56am UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/2 "2014-02-26T04:56:49Z")

</div>

> Hello!
> 
> I am newby in ansible world, but i like this tool and i try to use in my enviroment…  
> I have some question about user managment, and can’t get solution ☹  
> I have several projects (test, developer, production and same) I make separate folders for this projects with contain hosts, main.yml, ansible.cfg and roles folder… I try to add user accounts by adding special role- user\_ssh, like this:
> 
> > - name: Add ssh user  
> > user: name={{ item.user }} shell={{ item.shell }} groups=‘admins’  
> > with\_items:
> > - $ssh\_users
> 
> Variable ssh\_users i use in play-book and use it for any host grous. It looks like this:
> 
> > - hosts: apps  
> > vars:
> > - ssh\_users:
> > - $user1
> > - $user2
> 
> In next group (db for example) i use different values for ssh\_users variable (like user1 and user3)

1. You are mixing old style variables with new style variables… (replace $var with {{ var }})

2. in your play you have item.name and item.shell but you are only passing in a single variable

try something like this for your variables…  
vars:  
ssh\_users:

- {{ name: user1, shell: /bin/ksh }}
- {{ name: user2, shell: /bin/bash }}

I hope that this helps,

Adam

---

<div class="post-metadata">

**Author:** ![Petr\_Sukharev](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Petr\_Sukharev](https://forum.ansible.com/u/Petr_Sukharev)\
**Post date:** [February 26, 2014, 9:05am UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/3 "2014-02-26T09:05:33Z")

</div>

Thanks for you reply, Adam!  
I think, this solution is working like charm, but main reason for defined users like variables - is keeping all information in separate file. It is more simple for managing information and apply changes…  
For example, if i have 10 host groups and 40 users, and changing some information or add field for user - i need to change each entry in my playbook (or multiple playbooks). If i use separate file - i need to change only this file 🙂

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 26, 2014, 11:57am UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/4 "2014-02-26T11:57:54Z")

</div>

I would suggest that you put the variables into some kind of vars file (group\_vars if they are different for different groups)

then you can use the same playbook and task… If you add fields you may want to change the tasks to apply the fields…

So with your inventory file…

[group\_1]  
host\_1  
host\_2

[group\_2]  
host\_3  
host\_4

You would have a playbook that would have something like

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 26, 2014, 12:04pm UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/5 "2014-02-26T12:04:01Z")

</div>

I should add that you might want to look at the best practices document

[http://docs.ansible.com/playbooks\_best\_practices.html](http://docs.ansible.com/playbooks_best_practices.html)

which will give you a clear picture of how you could lay out your files. If you want the same users on all hosts in all groups then you might want to put them in group\_vars/all rather than group\_vars/group\_1

Adam

---

<div class="post-metadata">

**Author:** ![Petr\_Sukharev](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Petr\_Sukharev](https://forum.ansible.com/u/Petr_Sukharev)\
**Post date:** [February 26, 2014, 4:35pm UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/6 "2014-02-26T16:35:59Z")

</div>

works like charm!

One more little question - i have user variables like this:

> user1:
> 
> - { user: ‘user1’, shell: ‘/bin/sh’ }

how i can change shell value in special host group to /bin/nologin for example? And keep it default value for other groups?..

---

<div class="post-metadata">

**Author:** ![Petr\_Sukharev](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Petr\_Sukharev](https://forum.ansible.com/u/Petr_Sukharev)\
**Post date:** [February 26, 2014, 5:11pm UTC](https://forum.ansible.com/t/ssh-users-via-ansible/14652/7 "2014-02-26T17:11:43Z")

</div>

Solved by myself.

Use trick with

shell={{ variable | default(‘/bin/sh’) }} value.

Thanks a lot for help!
