ansible で linuxのユーザアカウントの作成・削除をする

playbookの作成

ユーザアカウントの作成
(useradd.yml)

---
- hosts: all

  tasks:
  - name:
    ansible.builtin.user:
      name: "{{username}}"
      groups: wheel
      password: "{{ password | password_hash('sha512') }}"
    become: yes


ユーザアカウントの削除
(userdel.yml)

---
- hosts: all

  tasks:
  - name:
    ansible.builtin.user:
      name: "{{username}}"
      remove: yes
      state: absent
    become: yes

ユーザアカウントを作成する

$ ansible-playbook -i 192.168.***.***, useradd.yml --extra-vars "username=scott password=tiger"

PLAY [all] **************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [192.168.***.***]

TASK [ansible.builtin.user] *********************************************************************************************
[DEPRECATION WARNING]: Encryption using the Python crypt module is deprecated. The Python crypt module is deprecated and
 will be removed from Python 3.13. Install the passlib library for continued encryption functionality. This feature will
 be removed in version 2.17. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [192.168.***.***]

PLAY RECAP **************************************************************************************************************
192.168.***.***             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

ユーザアカウントを削除する

$ ansible-playbook -i 192.168.***.***, userdel.yml --extra-vars "username=scott"

PLAY [all] **************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [192.168.***.***]

TASK [ansible.builtin.user] *********************************************************************************************
changed: [192.168.***.***]

PLAY RECAP **************************************************************************************************************
192.168.***.***             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0