ansibleにおいての変数定義はいくつもパターンがある模様
Playbook内で変数を定義する方法
実行するPlaybook
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /root/test_playbook
- hosts: client01
name: "loop test"
become: yes
vars:
test_array:
- file_path: "file1"
content: "content1\n"
- file_path: "file2"
content: "content2\n"
- file_path: "file3"
content: "content3\n"
tasks:
- copy:
dest: "/tmp/{{ item.file_path }}"
content: "{{ item.content }}"
loop: "{{ test_array }}"
オプション内容
vars
繰り返し処理で使う配列、変数を定義するので宣言しておく
・test_array
繰り返し処理で呼び出す任意の配列名
・file_path
繰り返し処理で呼び出す任意のキー
・content
繰り返し処理で呼び出す任意のキー
※copyモジュールでファイルの末尾に改行を入れるため”○○○\n”の形式で記載
tasks
varsで宣言した配列、キーをtask内で呼ぶ
・loop
varsで宣言した配列を”{{ 配列名 }}”の形式で記載する
・dest,content
varsで宣言したキーバリューを”{{ item.キー }}”の形式で記載する
実行前の状態
[root@localhost ~]# クライアント側
[root@localhost ~]# ll /tmp/file*
ls: /tmp/file* にアクセスできません: そのようなファイルやディレクトリはありません
Playbookの実行
[root@localhost ~]# ansible-playbook /root/test_playbook -D -k
SSH password:
PLAY [loop test] ****************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [copy] *********************************************************************************************************************************************************************************
--- before
+++ after: /root/.ansible/tmp/ansible-local-2919uieZIj/tmpEhudJv
@@ -0,0 +1 @@
+content1
changed: [192.168.0.21] => (item={u'content': u'content1\n', u'file_path': u'file1'})
--- before
+++ after: /root/.ansible/tmp/ansible-local-2919uieZIj/tmpSgMIpC
@@ -0,0 +1 @@
+content2
changed: [192.168.0.21] => (item={u'content': u'content2\n', u'file_path': u'file2'})
--- before
+++ after: /root/.ansible/tmp/ansible-local-2919uieZIj/tmpKROSZb
@@ -0,0 +1 @@
+content3
changed: [192.168.0.21] => (item={u'content': u'content3\n', u'file_path': u'file3'})
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
実行後の状態
[root@localhost ~]# クライアント側
[root@localhost ~]# ll /tmp/file*
-rw-r--r-- 1 root root 9 1月 6 03:02 /tmp/file1
-rw-r--r-- 1 root root 9 1月 6 03:02 /tmp/file2
-rw-r--r-- 1 root root 9 1月 6 03:02 /tmp/file3
[root@localhost ~]#
[root@localhost ~]# cat /tmp/file1
content1
[root@localhost ~]#
[root@localhost ~]# cat /tmp/file2
content2
[root@localhost ~]#
[root@localhost ~]# cat /tmp/file3
content3
include_varsで変数ファイルを指定する方法
実行するPlaybook
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /root/test_playbook
- hosts: client01
name: "loop test"
become: yes
tasks:
- include_vars:
file: /root/test_vars
- copy:
dest: "/tmp/{{ item.file_path }}"
content: "{{ item.content }}"
loop: "{{ test_array }}"
オプション内容
・include_varsモジュール
fileモジュールで使用する変数ファイルのパスを記載する
指定する変数ファイル
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /root/test_vars
test_array:
- file_path: "file1"
content: "content1\n"
- file_path: "file2"
content: "content2\n"
- file_path: "file3"
content: "content3\n"
Playbookの実行
実行コマンド以降は、「Playbook内で変数を定義する方法」と同じ
host_varsディレクトリを使う方法
ディレクトリ構成
[root@localhost ~]# マネージャ側
[root@localhost ~]# tree
.
├── host_vars
│ ├── host01
│ └── host02
├── test_inventory
└── test_playbook
1 directory, 4 files
構成ファイル内容
・host_vars
各ホストで使う変数を定義したファイルを配置するディレクトリ。配置するファイル名はインベントリファイルに記載したホスト名にする
・host01
host01で繰り返し処理を行う際に使う変数を定義する。ファイル名はインベントリファイルに記載してあるホスト名
・host02
host02で繰り返し処理を行う際に使う変数を定義する。ファイル名はインベントリファイルに記載してあるホスト名
・test_inventory
使用するインベントリファイル
・test_playbook
使用するプレイブック
インベントリファイル
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /etc/ansible/hosts
[all]
host01
host02
host_vars内のファイル
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /root/host_vars/host01
test_array:
- file_path: "variable_path01"
content: "host01_content01\n"
- file_path: "variable_path02"
content: "host01_content02\n"
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /root/host_vars/host02
test_array:
- file_path: "variable_path03"
content: "host02_content03\n"
- file_path: "variable_path04"
content: "host02_content04\n"
オプション内容
それぞれのファイルに繰り返し処理で使う配列、変数を定義するので宣言しておく
・test_array
繰り返し処理で呼び出す任意の配列名
・file_path
繰り返し処理で呼び出す任意のキー
・content
繰り返し処理で呼び出す任意のキー
※copyモジュールでファイルの末尾に改行を入れるため”○○○\n”の形式で記載
実行するPlaybook
[root@localhost ~]# マネージャ側
[root@localhost ~]# cat /root/test_playbook
- hosts: all
name: "host_vars test"
become: yes
tasks:
- copy:
dest: "/tmp/{{ item.file_path }}"
content: "{{ item.content }}"
loop: "{{ test_array }}"
オプション内容
host_vars配下のファイルで宣言した配列、キーをtask内で呼ぶ
・loop
host_vars配下のファイルで宣言した配列を”{{ 配列名 }}”の形式で記載する
・dest,content
host_vars配下のファイルで宣言したキーバリューを”{{ item.キー }}”の形式で記載する
実行前の状態
[root@localhost ~]# クライアント01側
[root@localhost ~]# ll /tmp/variable_path*
ls: /tmp/variable_path* にアクセスできません: そのようなファイルやディレクトリはありません
[root@localhost ~]# クライアント02側
[root@localhost ~]# ll /tmp/variable_path*
ls: /tmp/variable_path* にアクセスできません: そのようなファイルやディレクトリはありません
Playbookの実行
[root@localhost ~]# マネージャ側
[root@localhost ~]# ansible-playbook /root/test_playbook -D -k
SSH password:
PLAY [host_vars test] ***********************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [host01]
ok: [host02]
TASK [copy] *********************************************************************************************************************************************************************************
--- before
+++ after: /root/.ansible/tmp/ansible-local-2168t4_BnJ/tmpH_Ynak
@@ -0,0 +1 @@
+host01_content01
changed: [host01] => (item={u'content': u'host01_content01\n', u'file_path': u'variable_path01'})
--- before
+++ after: /root/.ansible/tmp/ansible-local-2168t4_BnJ/tmpxhiHIb
@@ -0,0 +1 @@
+host02_content03
changed: [host02] => (item={u'content': u'host02_content03\n', u'file_path': u'variable_path03'})
--- before
+++ after: /root/.ansible/tmp/ansible-local-2168t4_BnJ/tmpP7QhGH
@@ -0,0 +1 @@
+host01_content02
changed: [host01] => (item={u'content': u'host01_content02\n', u'file_path': u'variable_path02'})
--- before
+++ after: /root/.ansible/tmp/ansible-local-2168t4_BnJ/tmpVZz_Z2
@@ -0,0 +1 @@
+host02_content04
changed: [host02] => (item={u'content': u'host02_content04\n', u'file_path': u'variable_path04'})
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
実行後の状態
[root@localhost ~]# クライアント01側
[root@localhost ~]# ll /tmp/variable_path*
-rw-r--r-- 1 root root 17 1月 7 00:54 /tmp/variable_path01
-rw-r--r-- 1 root root 17 1月 7 00:54 /tmp/variable_path02
[root@localhost ~]#
[root@localhost ~]# cat /tmp/variable_path01
host01_content01
[root@localhost ~]#
[root@localhost ~]# cat /tmp/variable_path02
host01_content02
[root@localhost ~]# クライアント02側
[root@localhost ~]# ll /tmp/variable_path*
-rw-r--r-- 1 root root 17 1月 7 00:54 /tmp/variable_path03
-rw-r--r-- 1 root root 17 1月 7 00:54 /tmp/variable_path04
[root@localhost ~]#
[root@localhost ~]# cat /tmp/variable_path03
host02_content03
[root@localhost ~]#
[root@localhost ~]# cat /tmp/variable_path04
host02_content04
コメント