説明
演習問題
Q1. コントロールノード上のファイル「 accounts.yml 」を元に、コントロールノード上のファイル「 password.csv 」を参照し、管理対象ノードにアカウントを作成してください。
- プレイブックファイル名は「 regist_account.yml 」にする。
- ファイル「 accounts.yml 」は
vars_files
セクションで読み込む。 - パスワードは sha-512 形式に変換して登録する。
- ソルトは 0 ~ 65534 までの間のランダムな数字を使用する。
- 各ファイルの内容accounts.yml
accounts: - zoe - alice - matilda - steve - justin
password.csvalice,password@alice bob,password@bob carol,password@carol charlie,password@charlie dave,password@dave ellen,password@ellen frank,password@frank eve,password@eve isaac,password@isaac ivan,password@ivan justin,password@justin mallory,password@mallory marvin,password@marvin mallet,password@mallet matilda,password@matilda oscar,password@oscar pat,password@pat peggy,password@peggy victor,password@victor steve,password@steve trent,password@trent trudy,password@trudy walter,password@walter zoe,password@zoe
- インベントリーファイルの内容hosts.yml
--- all: hosts: marutamachi: ansible_host: 192.168.111.101 ansible_user: vagrant ansible_password: vagrant
解答
regist_account.yml
---
- name: Register a user account.
hosts: all
gather_facts: no
vars_files:
- accounts.yml
tasks:
- name: Register an account.
ansible.builtin.user:
name: "{{ item }}"
password: "{{ lookup('csvfile', '{{ item }} file=password.csv delimiter=, col=1') | password_hash('sha512', 65534 | random(seed=inventory_hostname) | string) }}"
state: present
loop: "{{ accounts }}"
become: yes
y_mrok@ctrl:~/code/chap30$ ansible-playbook -i hosts.yml register_account.yml
PLAY [Register a user account.] **************************************************************************************************************
TASK [Register an account.] ******************************************************************************************************************
changed: [marutamachi] => (item=zoe)
changed: [marutamachi] => (item=alice)
changed: [marutamachi] => (item=matilda)
changed: [marutamachi] => (item=steve)
changed: [marutamachi] => (item=justin)
PLAY RECAP ***********************************************************************************************************************************
marutamachi : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap30$
冪等性を確認
y_mrok@ctrl:~/code/chap30$ ansible-playbook -i hosts.yml register_account.yml
PLAY [Register a user account.] **************************************************************************************************************
TASK [Register an account.] ******************************************************************************************************************
ok: [marutamachi] => (item=zoe)
ok: [marutamachi] => (item=alice)
ok: [marutamachi] => (item=matilda)
ok: [marutamachi] => (item=steve)
ok: [marutamachi] => (item=justin)
PLAY RECAP ***********************************************************************************************************************************
marutamachi : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap30$
Q2. 作成したアカウントのいずれかで管理対象ノードに ssh 接続してください。。
解答
y_mrok@ctrl:~/code/chap30$ ssh 192.168.111.101 -l alice
Warning: Permanently added '192.168.111.101' (ECDSA) to the list of known hosts.
alice@192.168.111.101's password:
[alice@marutamachi ~]$ whoami
alice
[alice@marutamachi ~]$ logout
Connection to 192.168.111.101 closed.
y_mrok@ctrl:~/code/chap30$