Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible create user with password, dont change it if user exists

I have some servers which I want to administer with ansible. Currently I need to create user acounts on all of them. On some of them, some accounts are already present. I want to create the users with a default password, but if the user exist don't change his password.

Can someone help me with this condition ?

Here is my playbook :

---
- hosts: all
  become: yes
  vars:
    sudo_users:
       # password is generated through "mkpasswd" command from 'whois' package
      - login: user1
        password: hashed_password
      - login: user1
        password: hashed_password

  tasks:
    - name: Make sure we have a 'sudo' group
      group:
        name: sudo
        state: present

    - user:
        name: "{{ item.login }}"
        #password: "{{ item.password }}"
        shell: /bin/bash
        groups: "{{ item.login }},sudo"
        append: yes
      with_items: "{{ sudo_users }}"
like image 407
Marin Bînzari Avatar asked Oct 27 '25 09:10

Marin Bînzari


1 Answers

From the docs of user module:

update_password (added in 1.3) always/on_create
always will update passwords if they differ. on_create will only set the password for newly created users.

like image 184
Konstantin Suvorov Avatar answered Oct 30 '25 07:10

Konstantin Suvorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!