# Modify uid/gid if exists, but don't create if absent

**URL:** <https://forum.ansible.com/t/modify-uid-gid-if-exists-but-dont-create-if-absent/26889>\
**Category:** Ansible Project\
**Created:** [March 2, 2018, 8:17pm UTC](https://forum.ansible.com/t/modify-uid-gid-if-exists-but-dont-create-if-absent/26889 "2018-03-02T20:17:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [March 2, 2018, 8:17pm UTC](https://forum.ansible.com/t/modify-uid-gid-if-exists-but-dont-create-if-absent/26889/1 "2018-03-02T20:17:16Z")

</div>

I don’t see a way to do this with ansible, but perhaps I am missing something. I have a need to change the uid/gid of users/groups on a bunch of servers; however, I don’t wish to create the user/group if it is missing, I just want to modify it if it exists. The following creates the users/groups. The only state options available are present and absent. I need something like, “if present”. I could script it, but I am looking to stick with the ansible stuff if possible. Any thoughts?

`

- name: Backup /etc/passwd,group files  
copy:  
src: “{{ item.src }}”  
dest: “{{ item.dest }}.{{ ansible\_date\_time.date }}”  
with\_items:

- { src: “/etc/passwd”, dest: “/etc/passwd.bak” }

- { src: “/etc/group”, dest: “/etc/group.bak” }

- name: Set group uids/gids  
group:  
name: “{{ item.name }}”  
gid: “{{ item.gid }}”  
with\_items:

- { name: “gomgroup”, gid: “2000” }

- { name: “pyle”, gid: “2001” }

- name: Set user uids/gids  
user:  
name: “{{ item.name }}”  
uid: “{{ item.uid }}”  
group: “{{ item.group }}”  
with\_items:

- { name: “gomer”, uid: “2000”, group: “gomgroup” }

- { name: “pyle”, uid: “2001”, group: “pyle” }

`

---

<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:** [March 2, 2018, 8:38pm UTC](https://forum.ansible.com/t/modify-uid-gid-if-exists-but-dont-create-if-absent/26889/2 "2018-03-02T20:38:37Z")

</div>

Not tested, but something like this should work

- getent:  
&nbsp;&nbsp;&nbsp;&nbsp;database: passwd  
- getent:  
&nbsp;&nbsp;&nbsp;&nbsp;database: group

- name: Set group uids/gids  
&nbsp;&nbsp;group:  
&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;gid: "{{ item.gid }}"  
&nbsp;&nbsp;when: getent\_group[item.name] is defined  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- { name: "gomgroup", gid: "2000" }  
&nbsp;&nbsp;&nbsp;&nbsp;- { name: "pyle", gid: "2001" }

And for the uid you need to add  
&nbsp;&nbsp;when: getent\_passwd[item.name] is defined

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [March 2, 2018, 8:46pm UTC](https://forum.ansible.com/t/modify-uid-gid-if-exists-but-dont-create-if-absent/26889/3 "2018-03-02T20:46:12Z")

</div>

Thank you Kai, you are always helpful

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [March 2, 2018, 8:56pm UTC](https://forum.ansible.com/t/modify-uid-gid-if-exists-but-dont-create-if-absent/26889/4 "2018-03-02T20:56:04Z")

</div>

Works perfectly! and it solves another issue I was having elsewhere. Thanks again!
