Ansible userモジュールを使ってユーザを作成するサンプル

環境
Red Hat Enterprise Linux release 9.2 (Plow)
Ansible 2.14.2
Python 3.11.2

概要
user モジュールのパラメータ
state
absent ユーザが存在しない状態
present ユーザが存在する状態
groups 所属するサブグループを「,」区切りですべて指定

操作例
1.ユーザの新規作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- hosts: all
become: yes
tasks:
- name: add user
user:
name: cft
state: present
password: "{{ 'cftpasswd' | password_hash('sha512') }}"
createhome: yes
- hosts: all become: yes tasks: - name: add user user: name: cft state: present password: "{{ 'cftpasswd' | password_hash('sha512') }}" createhome: yes
- hosts: all
  become: yes
  tasks:
    - name: add user
      user:
        name: cft
        state: present
        password: "{{ 'cftpasswd' | password_hash('sha512') }}"
        createhome: yes

2.ユーザのサブグループを編集する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- hosts: all
become: yes
tasks:
- name: Edit user groups
user:
name: cft
groups: wheel, cftuser
- hosts: all become: yes tasks: - name: Edit user groups user: name: cft groups: wheel, cftuser
- hosts: all
  become: yes
  tasks:
    - name: Edit user groups
      user:
        name: cft
        groups: wheel, cftuser

3.ユーザの削除

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- hosts: all
become: yes
tasks:
- name: delete user
user:
name: cftuser
state: absent
- hosts: all become: yes tasks: - name: delete user user: name: cftuser state: absent
- hosts: all
  become: yes
  tasks:
    - name: delete user
      user:
        name: cftuser
        state: absent

 

IT

Posted by arkgame