シナリオ
CentOS 7.6.1810 を minimal インストールした対象ホストに以下の初期設定を行います。- 管理者用グループの作成
- 管理者用グループに管理者アカウントを登録(作成)
- 追加パッケージのインストール
- すべてのインストール済みパッケージを最新版に更新
- 再起動
管理者関係と対象ホストの仕様
- 管理者グループ
- グループ名: staff
- gid: 1000
- 管理者アカウント
- ユーザー名: workman
- uid: 1001
- パスワード: workman@node-c0706
- その他: パスワードの入力なしで sudo を可能にする
- 対象ホスト
- ホスト名: node-c0706.exam.local
- ユーザー名: root
- パスワード: root@node-c0706
- IP アドレス: 192.168.101.21
- 追加パッケージ
- open-vm-tools
対象ホストの状態確認
inventory ファイルのチェックと対象ホストが ansible で管理可能か確認します。具体的には ping モジュールを使用して、対象ホストに ping を打つことで確認できます。■ hosts.yml
all:
hosts:
node-c0706:
■ host_vars/node-c0706.yml
ansible_host: 192.168.101.21 ansible_user: root ansible_password: root@node-c0706■ 実行結果
[ansibleman@ansiblesv ansible]$ ansible all -i hosts.yml -m ping
node-c0706 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[ansibleman@ansiblesv ansible]$
playbook ファイルと関連ファイル
グループ/ユーザーアカウントと関連パッケージは複数でも対応できるようにしています。■ sudoers.j2
{{ item.uname }} ALL=(ALL:ALL) NOPASSWD: ALL
■ accounts.yml
accounts:
- uname: workman
uid: 1001
password: workman@node_c0706
gname: staff
gid: 1000
■ packages.yml
packages: - open-vm-tools■ site.yml
---
- hosts: all
gather_facts: no
vars_files:
- accounts.yml
- packages.yml
tasks:
- name: グループを作成
group:
name: "{{ item.gname }}"
gid: "{{ item.gid }}"
state: present
loop:
"{{ accounts }}"
- name: ユーザーの作成
user:
name: "{{ item.uname }}"
uid: "{{ item.uid }}"
group: "{{ item.gname }}"
password: "{{ item.password | password_hash('sha512') }}"
state: present
loop:
"{{ accounts }}"
- name: sudoers の設定
template:
src: ./sudoers.j2
dest: /etc/sudoers.d/{{ item.uname }}
mode: 0440
owner: root
group: root
validate: '/usr/sbin/visudo -cf %s'
loop:
"{{ accounts }}"
- name: 追加パッケージのインストール
yum:
name: "{{ item }}"
state: latest
loop:
"{{ packages }}"
- name: 既存パッケージの更新
yum:
name: '*'
state: latest
- name: 再起動
reboot:
■ 実行結果
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] *****************************************************************************************
TASK [グループを作成] *************************************************************************************
changed: [node-c0706] => (item={u'uname': u'workman', u'password': u'workman@node_c0706', u'gid': 1000, u'uid': 1001, u'gname': u'staff'})
TASK [ユーザーの作成] *************************************************************************************
changed: [node-c0706] => (item={u'uname': u'workman', u'password': u'workman@node_c0706', u'gid': 1000, u'uid': 1001, u'gname': u'staff'})
TASK [sudoers の設定] *********************************************************************************
changed: [node-c0706] => (item={u'uname': u'workman', u'password': u'workman@node_c0706', u'gid': 1000, u'uid': 1001, u'gname': u'staff'})
TASK [追加パッケージのインストール] ******************************************************************************
changed: [node-c0706] => (item=open-vm-tools)
TASK [既存パッケージの更新] **********************************************************************************
changed: [node-c0706]
TASK [再起動] *****************************************************************************************
changed: [node-c0706]
PLAY RECAP *****************************************************************************************
node-c0706 : ok=6 changed=6 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$