# ansible user module change passwd

**URL:** <https://forum.ansible.com/t/ansible-user-module-change-passwd/22687>\
**Category:** Ansible Project\
**Created:** [September 30, 2016, 9:08am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687 "2016-09-30T09:08:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hzj.zh](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@hzj.zh](https://forum.ansible.com/u/hzj.zh)\
**Post date:** [September 30, 2016, 9:08am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/1 "2016-09-30T09:08:27Z")

</div>

hello，all

i want to use ansible user module to change password on the remote hosts,

but , if the user is not in the remote hosts, ansible will create the user, how can i change password only, if user is not exist, return error is ok;

---

<div class="post-metadata">

**Author:** ![JayB](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JayB](https://forum.ansible.com/u/JayB)\
**Post date:** [September 30, 2016, 9:18am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/2 "2016-09-30T09:18:18Z")

</div>

Please try the below. It works.

---

<div class="post-metadata">

**Author:** ![hzj.zh](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@hzj.zh](https://forum.ansible.com/u/hzj.zh)\
**Post date:** [September 30, 2016, 9:50am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/3 "2016-09-30T09:50:18Z")

</div>

i tried, the playbook still creates the user if user not exists on the remote hosts ;

---

<div class="post-metadata">

**Author:** ![JayB](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JayB](https://forum.ansible.com/u/JayB)\
**Post date:** [September 30, 2016, 10:31am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/4 "2016-09-30T10:31:19Z")

</div>

It worked for me. It throws error if user doesn’t exist.

---

<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:** [September 30, 2016, 11:53am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/5 "2016-09-30T11:53:28Z")

</div>

> i tried, the playbook still creates the user if user not exists on  
> the remote hosts ;

You'll need to add a test to check if the user exist and use a when on the update password task.

> ---  
> - hosts: localhost  
> &nbsp;&nbsp;remote\_user: root  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;gather\_facts: yes  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;user\_name: youruser
> 
> &nbsp;&nbsp;vars\_prompt:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: "new\_password"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: "Enter New Password"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encrypt: "md5\_crypt"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;confirm: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salt\_size: 7
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Change password of existing user  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: name={{user\_name}} update\_password=always password={{new\_password}}

tasks:  
&nbsp;&nbsp;&nbsp;- name: Get information about the user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getent:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key={{user\_name}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database=passwd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail\_key=false

&nbsp;&nbsp;&nbsp;- name: Change password of existing user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name={{user\_name}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;update\_password=always  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password={{new\_password}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: getent\_passwd[user\_name] != None

---

<div class="post-metadata">

**Author:** ![hzj.zh](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@hzj.zh](https://forum.ansible.com/u/hzj.zh)\
**Post date:** [October 1, 2016, 2:24am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/6 "2016-10-01T02:24:25Z")

</div>

thanks for ur advice, i have aix and linux, i wrote the playbook based on ur reply, it works , but i think it’s too long, is there a better way to rewrite this ?

---

<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:** [October 1, 2016, 9:00am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/7 "2016-10-01T09:00:42Z")

</div>

> thanks for ur advice, i have aix and linux, i wrote the playbook based on ur reply, it works , but i think it's too long, is there a better way to rewrite this ?

In my opinion it's not long, you could but the code in it's one file and use include if you would like the playbook to have less code.

> ---  
> - name: test  
> &nbsp;&nbsp;hosts: all  
> &nbsp;&nbsp;gather\_facts: true
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- name: Get information about the user for linux  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getent:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key=foo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database=passwd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail\_key=false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: user\_info\_linux  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_system == 'Linux'  
> &nbsp;&nbsp;&nbsp;- debug: var=user\_info\_linux
> 
> &nbsp;&nbsp;&nbsp;- name: Get information about the user for aix  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "lsuser foo"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: user\_info\_aix  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_system == 'AIX'  
> &nbsp;&nbsp;&nbsp;- debug: var=user\_info\_aix
> 
> &nbsp;&nbsp;&nbsp;- name: change pwd for linux  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: uname -a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_system == 'Linux' and user\_info\_linux.ansible\_facts.getent\_passwd.foo != None
> 
> &nbsp;&nbsp;&nbsp;- name: change pwd for aix  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: uname -a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_system == 'AIX' and user\_info\_aix.stdout != ""

The other option is to use grep/egrep on /etc/passwd, the you can use the same code on AIX and Linux. I think AIX has egrep.

tasks:  
&nbsp;&nbsp;&nbsp;- Name get user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: egrep "^{{ user }}:" /etc/passwd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: user\_info

&nbsp;&nbsp;&nbsp;- name: Change passwd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: \<whatever to change password\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: user\_info.rc == 0

---

<div class="post-metadata">

**Author:** ![hzj.zh](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@hzj.zh](https://forum.ansible.com/u/hzj.zh)\
**Post date:** [October 3, 2016, 12:51am UTC](https://forum.ansible.com/t/ansible-user-module-change-passwd/22687/8 "2016-10-03T00:51:37Z")

</div>

thanks, my final playbook comes to this, works pretty well, problem solved!
