https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html
https://zenn.dev/y_mrok/books/ansible-enshumondai/viewer/chapter28
前提:
Python 3.9.14
Rocky Linux 9.1
ansible [core 2.15.2]
コントロールノード -> ターゲットノードの公開鍵認証設定済み
ターゲットノードでvisudo設定済み
-- 1. Ansibleインストール[mmm190で実施]
su - ansible
python -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install ansible-core
ansible --version
-- 2. 設定ファイル作成
vim ansible.cfg
[defaults]
forks = 10
log_path = $HOME/.ansible/ansible.log
host_key_checking = True
gathering = smart
transport = smart
-- 3. インベントリファイル作成
vim inventory.ini
localhost
mmm190
[server]
192.168.137.191
-- 4. ansible動作確認
ansible server -i inventory.ini -m ansible.builtin.ping
tail $HOME/.ansible/ansible.log
-- 5. 変数値の暗号化
echo -n "user01" | ansible-vault encrypt_string
echo -n "user02" | ansible-vault encrypt_string
-- 6. プレイブック作成
vim playbook.yml
---
- hosts: server
become: true
gather_facts: true
vars:
accounts:
- name: user01
uid: 4001
password: !vault |
$ANSIBLE_VAULT;1.1;AES256
31363932616561396566626563383933313136316361376332623661653031343738626130313166
3032336330666138316135313562316232646639383961610a633436373430343931376664353539
31376262396166323530373463393433303639313561633964346537623331636337633066393866
3534666265396561310a653930623139666162643533666263666461623033663033626134636130
6237
- name: user02
uid: 4002
password: !vault |
$ANSIBLE_VAULT;1.1;AES256
33303863363966333164313731396235666661373539656661633037613466633139386334643230
3932353839646663616364343561333163363531656531310a313634376538363561333130643737
31663663316637633234626665663135363565663835646438313039393166353038373531623565
3439356362656664380a623238623762613861663564383932613262373764343130326439666661
3834
tasks:
- name: Add users
ansible.builtin.user:
name: "{{ item['name'] }}"
uid: "{{ item['uid'] }}"
password: "{{ item['password'] | password_hash('sha512') }}"
state: present
loop:
"{{ accounts }}"
no_log: true
-- 7. プレイブック実行
ansible-playbook -i inventory.ini playbook.yml --ask-vault-pass --syntax-check
ansible-playbook -i inventory.ini playbook.yml --ask-vault-pass