Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create user with option --disabled-password by Ansible

On Ubuntu 14.04 I creating user with disabled password like:

sudo adduser --disabled-password myuser

I need to do same with Ansible user module

--disabled-password

Similiar option in Ansible documentation is missing. Could somebody help me, how can I get the same result with user module?

like image 913
32cupo Avatar asked Aug 18 '16 08:08

32cupo


People also ask

How do I pass Ansible user password?

If the remote user needs to provide a password in order to run sudo commands, you can include the option --ask-become-pass to your Ansible command. This will prompt you to provide the remote user sudo password: ansible all -m ping --ask-become-pass.

How do you create an encrypted password user module in Ansible?

Creating encrypted variables To create a basic encrypted variable, pass three options to the ansible-vault encrypt_string command: a source for the vault password (prompt, file, or script, with or without a vault ID) the string to encrypt. the string name (the name of the variable)

How do I add users to Ansible?

First, log in to the Ansible controller host, 2. Run the following commands to create the ~/ansible_create_user directory and change to that directory. This directory will contain the playbook and all the required configuration files that you'll use to invoke the Ansible create user module.

How do you mention sudo privilege in Ansible?

To specify a password for sudo, run ansible-playbook with --ask-become-pass ( -K for short). If you run a playbook utilizing become and the playbook seems to hang, most likely it is stuck at the privilege escalation prompt. Stop it with CTRL-c , then execute the playbook with -K and the appropriate password.


2 Answers

user module use useradd command under the hood.
If you omit password parameter for user module, ansible calls useradd without -p flag.
Man page of useradd states:

-p, --password PASSWORD
The encrypted password, as returned by crypt(3). The default is to disable the password.

This is exactly what is needed by OP.

Comparison of adduser --disabled-password test1 and - user: name=test2 state=present:

# grep test /etc/shadow
test1:*:17031:0:99999:7:::
test2:!:17031:0:99999:7:::
# passwd -S test1
test1 L 08/18/2016 0 99999 7 -1
# passwd -S test2
test2 L 08/18/2016 0 99999 7 -1

As you see in both cases passwords are locked.

like image 91
Konstantin Suvorov Avatar answered Oct 16 '22 05:10

Konstantin Suvorov


Since Ansible 2.6 the user module has the option password_lock, which will run usermod -L (Linux), pw lock (FreeBSD), or usermod -C (?):

usermod -L:

Lock a user's password. This puts a '!' in front of the encrypted password, effectively disabling the password.

pw lock:

The pw utility supports a simple password locking mechanism for users; it works by prepending the string *LOCKED* to the beginning of the password field in master.passwd to prevent successful authentication.

So you could use:

- name: Create password locked user
  user:
    name: myuser
    state: present
    password_lock: yes
like image 37
Felipe Zavan Avatar answered Oct 16 '22 06:10

Felipe Zavan