Ansibleでホストごとに処理内容を変更する

Ansibleでホストごとに処理内容を変更するためには、いろいろなやり方がある模様
今回は、ホスト変数 (host_vars,group_vars)での制御と、whenモジュールを使った条件式での制御を記載

host変数での制御

ディレクトリ構成

[root@localhost ~]# マネージャー側
[root@localhost ~]# tree
.
├── group_vars
│   └── group01
├── host_vars
│   ├── host01
│   └── host02
└── test_playbook

2 directories, 4 files

構成内容

・group_vars
グループで使う変数を定義したファイルを配置するディレクトリ。配置するファイル名はインベントリファイルに記載したグループ名にする
・group01
group01で繰り返し処理を行う際に使う変数を定義する。ファイル名はインベントリファイルに記載してあるグループ名
・host_vars
各ホストで使う変数を定義したファイルを配置するディレクトリ。配置するファイル名はインベントリファイルに記載したホスト名にする
・host01
host01で繰り返し処理を行う際に使う変数を定義する。ファイル名はインベントリファイルに記載してあるホスト名
・host02
host02で繰り返し処理を行う際に使う変数を定義する。ファイル名はインベントリファイルに記載してあるホスト名
・test_playbook
使用するプレイブック

group_vars内のファイル

[root@localhost ~]# マネージャー側
[root@localhost ~]# cat /root/group_vars/group01
file_path: "group01_file"
content: "group01_content01\n"

オプション内容

対象グループで使う変数を記載する

・file_path
Playbookで呼び出す任意のキー
・content
Playbookで呼び出す任意のキー

host_vars内のファイル

[root@localhost ~]# マネージャー側
[root@localhost ~]# cat /root/host_vars/host01
file_path: "host01"
content: "host01_content01\n"
[root@localhost ~]#
[root@localhost ~]# cat /root/host_vars/host02
file_path: "host02"
content: "host02_content02\n"

オプション内容

対象ホストで使う変数を記載する

・file_path
Playbookで呼び出す任意のキー
・content
Playbookで呼び出す任意のキー

インベントリファイル

[root@localhost ~]# マネージャー側
[root@localhost ~]# cat /etc/ansible/hosts
[all]
host01
host02
host03

[group01]
host02
host03
[root@localhost ~]#

host02はgroup01に所属しており
host_vars内の変数とgroup_vars内の変数のどちらが優先されるか検証する
host03はhost_vars内で変数定義しない

実行するPlaybook

[root@localhost ~]# マネージャー側
[root@localhost ~]# cat /root/test_playbook
- hosts: all
  name: "host_vars test"
  become: yes
  tasks:
    - copy:
        dest: "/tmp/{{ file_path }}"
        content: "{{ content }}"

実行前の状態

[root@localhost ~]# クライアント1
[root@localhost ~]# ll /tmp
合計 0
[root@localhost ~]# クライアント2
[root@localhost ~]# ll /tmp
合計 0
[root@localhost ~]# クライアント3
[root@localhost ~]# ll /tmp
合計 0

Playbookの実行

[root@localhost ~]# マネージャー側
[root@localhost ~]# ansible-playbook /root/test_playbook -D -k
SSH password:

PLAY [host_vars test] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [host02]
ok: [host01]
ok: [host03]

TASK [copy] *********************************************************************************************************************************************************************************
--- before
+++ after: /root/.ansible/tmp/ansible-local-1533u2YQv2/tmpypGsvh
@@ -0,0 +1 @@
+host02_content02

changed: [host02]
--- before
+++ after: /root/.ansible/tmp/ansible-local-1533u2YQv2/tmpd7sLVp
@@ -0,0 +1 @@
+host01_content01

changed: [host01]
--- before
+++ after: /root/.ansible/tmp/ansible-local-1533u2YQv2/tmp7PA3Pc
@@ -0,0 +1 @@
+group01_content01

changed: [host03]

PLAY RECAP **********************************************************************************************************************************************************************************
host01                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host02                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host03                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

実行後の状態

[root@localhost ~]# クライアント1
[root@localhost ~]# ll /tmp
合計 4
-rw-r--r-- 1 root root 17  1月  8 02:22 host01
[root@localhost ~]#
[root@localhost ~]# cat /tmp/host01
host01_content01
[root@localhost ~]# クライアント2
[root@localhost ~]# ll /tmp
合計 4
-rw-r--r-- 1 root root 17  1月  8 02:22 host02
[root@localhost ~]#
[root@localhost ~]# cat /tmp/host02
host02_content02
[root@localhost ~]# クライアント3
[root@localhost ~]# ll /tmp
合計 4
-rw-r--r-- 1 root root 18  1月  8 02:22 group01_file
[root@localhost ~]#
[root@localhost ~]# cat /tmp/group01_file
group01_content01

まとめ

host_vars > group_vars
host02はhost_vars内で定義した変数でファイルが作成された
同じ変数がgroup_varshost_varsの両方に定義されている場合、host_varsに定義された値が優先される
ホストが複数のグループに所属している場合など、細かい調整をhost_varsで行うといい

whenモジュールでの制御

実行するPlaybook

[root@localhost ~]# マネージャー側
[root@localhost ~]# cat /root/test_playbook
- hosts: all
  name: "when test"
  become: yes
  tasks:
    - copy:
        dest: "/tmp/file01"
        content: "content01\n"
      when: inventory_hostname == 'host01'
    - copy:
        dest: "/tmp/file02"
        content: "content02\n"
      when: inventory_hostname == 'host02'

オプション内容

・when
インベントリファイルに記載したホスト名を指定することで、対象ホストを絞れる

実行前の状態

[root@localhost ~]# クライアント1
[root@localhost ~]# ll /tmp
合計 0
[root@localhost ~]# クライアント2
[root@localhost ~]# ll /tmp
合計 0

Playbookの実行

[root@localhost ~]# ansible-playbook /root/test_playbook -D -k
SSH password:

PLAY [when test] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [host02]
ok: [host01]

TASK [copy] *********************************************************************************************************************************************************************************
skipping: [host02]
--- before
+++ after: /root/.ansible/tmp/ansible-local-1679zz4iBl/tmp51CyYQ
@@ -0,0 +1 @@
+content01

changed: [host01]

TASK [copy] *********************************************************************************************************************************************************************************
skipping: [host01]
--- before
+++ after: /root/.ansible/tmp/ansible-local-1679zz4iBl/tmpvDFMkH
@@ -0,0 +1 @@
+content02

changed: [host02]

PLAY RECAP **********************************************************************************************************************************************************************************
host01                     : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
host02                     : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

実行後の状態

[root@localhost ~]# クライアント1
[root@localhost ~]# ll /tmp
合計 4
-rw-r--r-- 1 root root 10  1月  8 02:59 file01
[root@localhost ~]#
[root@localhost ~]# cat /tmp/file01
content01
[root@localhost ~]# クライアント2
[root@localhost ~]# ll /tmp
合計 4
-rw-r--r-- 1 root root 10  1月  8 02:59 file02
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat /tmp/file02
content02

コメント

タイトルとURLをコピーしました