How to add user and group without a password using Ansible?

Levintovich picture Levintovich · Mar 29, 2016 · Viewed 11.5k times · Source

I need to add group and user without password (nologin user) using Ansible script.

I execute the following command:

$ansible-playbook deploy_nagios_client.yml -i hosts -e hosts=qa1-jetty -v

Below is main.yml

---

# Create Nagios User and Group 
  - name: Add group "nagios"
    group: name=nagios
    become: true

  - name: Add user "nagios"  
    user: name=nagios groups=nagios password="" shell=/bin/bash append=yes comment="Nagios nologin User" state=present
    become: true

Result

Answer

user2599522 picture user2599522 · Apr 2, 2016

If you want to create a nologin user, you should specify that to the shell argument like this:

  - name: Add user "nagios"  
    user:
      name: nagios
      groups: nagios
      shell: /sbin/nologin
      append: yes
      comment: "Nagios nologin User"
      state: present
    become: true