# How to edit a variable in a later variable file

**URL:** <https://forum.ansible.com/t/how-to-edit-a-variable-in-a-later-variable-file/24544>\
**Category:** Ansible Project\
**Created:** [May 22, 2017, 5:01pm UTC](https://forum.ansible.com/t/how-to-edit-a-variable-in-a-later-variable-file/24544 "2017-05-22T17:01:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Lininger](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Adam\_Lininger](https://forum.ansible.com/u/Adam_Lininger)\
**Post date:** [May 22, 2017, 5:01pm UTC](https://forum.ansible.com/t/how-to-edit-a-variable-in-a-later-variable-file/24544/1 "2017-05-22T17:01:00Z")

</div>

I would like to be able to edit a variable (per host) that was originally defined in a default file.

The larger situation is this. We have a large number of servers with a defined set of local accounts. The list of accounts is defined something like this in the default.yml file for the role. The task in the role iterates over the list and creates all the accounts using the user module.

`  
list\_of\_users:

- username: user1  
state: present  
shell: /bin/bash  
password\_hash: 

- username: user2  
state: present  
shell: /bin/zsh  
password\_hash:   
`

Ideally, I’d like to be able to trim down the list of users to a much smaller set on a per-host basis. I don’t want to have to edit the per host file when the main file adds a new user. I also don’t want to have to change passwords or shells in every per-host file (which is what I’m doing now).

The ideal case would be to (in a host\_vars or group\_vars file) iterate over the list\_of\_users array and change the state variable to absent…except when username matches a local list.

It seems like such a thing should be possible somehow, but I haven’t found any way of doing that.

Thanks,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 28, 2017, 5:29pm UTC](https://forum.ansible.com/t/how-to-edit-a-variable-in-a-later-variable-file/24544/2 "2017-05-28T17:29:48Z")

</div>

> I would like to be able to edit a variable (per host) that was originally  
> defined in a default file.

You can overwrite variables per host, but not edit or partial overwrite a variable.

> The larger situation is this. We have a large number of servers with a  
> defined set of local accounts. The list of accounts is defined something  
> like this in the default.yml file for the role. The task in the role  
> iterates over the list and creates all the accounts using the user module.
> 
> list\_of\_users:  
> &nbsp;&nbsp;- username: user1  
> &nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/bash  
> &nbsp;&nbsp;&nbsp;&nbsp;password\_hash: \<hash\>
> 
> &nbsp;&nbsp;- username: user2  
> &nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/zsh  
> &nbsp;&nbsp;&nbsp;&nbsp;password\_hash: \<hash\>
> 
> Ideally, I'd like to be able to trim down the list of users to a much  
> smaller set on a per-host basis. I don't want to have to edit the per host  
> file when the main file adds a new user. I also don't want to have to  
> change passwords or shells in every per-host file (which is what I'm doing  
> now).
> 
> The ideal case would be to (in a host\_vars or group\_vars file) iterate over  
> the list\_of\_users array and change the state variable to absent...except  
> when username matches a local list.
> 
> It seems like such a thing should be possible somehow, but I haven't found  
> any way of doing that.

What you could do is introduce a list variable called enabled\_users.  
Then you can list all the user that you would like to create on a host, and the role has all the users.

If you need to overwrite some of the values you could do something like list\_of\_user\_overwrite.

- user:  
&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ list\_of\_user\_overwrite[item.username] | default(item.username) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: "{{ list\_of\_user\_overwrite[item.state] | default(item.state) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;when: item.username in enabled\_users  
&nbsp;&nbsp;with\_items: "{{ list\_of\_users }}"
