How To Create a Unix Group with Ansible

Red Hat Ansible

I’m configuring a new dedicated server this weekend and think it will be great to capture small notes as I go. Today it’s a small basic example, perfect for Ansible beginners: creating a Unix group.

Ansible Playbook for Creating a Group

Here’s the sample playbook file groups.yaml in its entirety:

---
- hosts: techstack
  become: true
  become_user: root
  become_method: su 

  tasks: 
    - name: Create Tech Stack group
      group: name=techstack2 gid=1002 state=present

Going line by line, this example uses:

  1. Ansible hosts file – specifically, group of hosts named techstack
  2. Become method – meaning I give instructions to Ansible playbook for elevating privileges on remote hosts – effectively becoming another user. In this case, the user (become_user) is root, and the way to become this user is su (not sudo!)
  3. group module – it’s one of the most standard modules in Ansible, allowing you to manage Unix groups. As always, you specify the name of the group (techstack2) and the group id for it (1002).

How To Run Group Playbook in Ansible

Because there’s su method involved, Ansible playbook will be expected to provide a password for root user on the remote hosts. But with no password definition in sight, we have to supply password using command line:

greys@maverick:~ $ ansible-playbook groups.yaml –ask-become-pass

This will ask for a password before running the playbook – strangely enough it says SUDO password, but then works just fine:

Ansible Playbook Creating a Unix Group

See Also