# Add special value to variable or merge 2 variables into one (user manegment)

**URL:** <https://forum.ansible.com/t/add-special-value-to-variable-or-merge-2-variables-into-one-user-manegment/15319>\
**Category:** Ansible Project\
**Created:** [May 23, 2014, 3:41pm UTC](https://forum.ansible.com/t/add-special-value-to-variable-or-merge-2-variables-into-one-user-manegment/15319 "2014-05-23T15:41:38Z")\
**Posts on this page:** 2\
**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:** [May 23, 2014, 3:41pm UTC](https://forum.ansible.com/t/add-special-value-to-variable-or-merge-2-variables-into-one-user-manegment/15319/1 "2014-05-23T15:41:38Z")

</div>

Hello!  
I try to make ssh users management via ansible and find some problem, with blow up my brain.  
My environment: some numbers of users, some numbers of hosts. Production, test and demo projects.  
I put all users in file group\_vars/all and it looks like this:

user1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { user: 'user1', group: 'test1', comment: "Test\_User\_1", uid: '10001', password: 'HaSH1' }  
user2:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { user: 'user2', group: 'test2', comment: 'Test\_User\_2', uid: '10000', password: 'HaSh2' }

My role file for users looks like this:

- name: Add ssh user  
&nbsp;&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name={{ item.user }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups={{ item.group }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment={{ item.comment }} uid={{ item.uid }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password={{ item.password }}  
&nbsp;&nbsp;&nbsp;with\_items: "ssh\_users"

&nbsp;&nbsp;&nbsp;And ssh\_users describes for each host group like

- hosts: app  
&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh\_users: "{{ssh\_users\_app}}"  
&nbsp;&nbsp;&nbsp;vars\_files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vars/production  
&nbsp;&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- users

- hosts: db  
&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh\_users: "{{ssh\_users\_db}}"  
&nbsp;&nbsp;&nbsp;vars\_files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vars/production  
&nbsp;&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- users

&nbsp;&nbsp;&nbsp;&nbsp;And last step is:  
&nbsp;&nbsp;&nbsp;In vars/production i give list of users from group\_vars/all file:

&nbsp;&nbsp;&nbsp;ssh\_users\_app:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{user1}}"  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{user2}}"  
&nbsp;&nbsp;&nbsp;&nbsp;ssh\_users\_db:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{user2}}"

&nbsp;&nbsp;&nbsp;&nbsp;So, ansible take ssh\_users\_app variable from vars/production file, put it in ssh\_users variable and send to role. For each host group i can specifies list of users and all going well... BUT!  
&nbsp;&nbsp;&nbsp;&nbsp;When i need to remove user - i need to remove it from ssh\_users\_app variable and then run something like this:  
&nbsp;&nbsp;&nbsp;&nbsp;ansible -i hosts app -m user -a 'name=user1 state=absent'

&nbsp;&nbsp;&nbsp;&nbsp;It make me feel little uncomfortable and i try to improve my solution.

&nbsp;&nbsp;&nbsp;&nbsp;My idea was to specified special value in vars/production with will be describe state value for user. Like this:

&nbsp;&nbsp;&nbsp;&nbsp;ssh\_users\_app:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{user1}}" state: 'present'  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{user2}}" state: 'absent'  
&nbsp;&nbsp;&nbsp;&nbsp;ssh\_users\_db:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{user2}}" state: 'present'

&nbsp;&nbsp;&nbsp;&nbsp;End if i need to remove user from host group - i will changing his state and apply role to hosts  
&nbsp;&nbsp;&nbsp;&nbsp;Does any legal method to do this? Or ansible have more elegant solution for user management case?

&nbsp;&nbsp;&nbsp;&nbsp;Thanks a lot for any help!

---

<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:** [May 25, 2014, 8:43pm UTC](https://forum.ansible.com/t/add-special-value-to-variable-or-merge-2-variables-into-one-user-manegment/15319/2 "2014-05-25T20:43:54Z")

</div>

General idiom most people use is like:

ssh\_users:

- 
# list of users

ssh\_users\_removed:

- 
# list of users

And add to \_removed when people leave your company, etc. This list can gradually be pruned over time when you are sure no traces of them are around.
