説明
- play 内で使用する変数を YAML 記法で定義する
- 変数に使用できるのは 英字・数字・アンダースコア(下線) の 3 種類であり、英字から始める
- 変数は vars セクション内に直接定義する
- 変数を参照するときは “{{ 変数 }}”と表記する
- ファイルに変数を定義し vars_files: で取り込む
構文
- vars:
- 変数名
- vars_files:
- - ファイル名
定義
- ハッシュ
- 定義
- 変数名: 値
- 配列
- 定義
- 配列名:
- - 値1
- - 値2
- - 値3
- 配列内の要素を参照するときは [ ] で参照する位置を指定する
- 要素番号はゼロから始まる
- リスト(配列とハッシュのネスト)
- 定義
- リスト名 :
- - 変数名1-1: 値1-1
- 変数名1-2: 値1-2
- 変数名1-3: 値1-3
- - 変数名2-1: 値2-1
- 変数名2-2: 値2-2
- 変数名2-3: 値2-3
- - 変数名3-1: 値3-1
- 変数名3-2: 値3-2
- 変数名3-3: 値3-3
- リスト内の変数を参照するときは “リスト名.変数名” のように “.” でつないで指定する
例
■ inventory ファイル : hosts.ymlall:
hosts:
node1:
■ ハッシュ
---
- hosts: all
vars:
country: Japan
capital: tokyo
tasks:
- debug:
var: country
- debug:
msg: "{{ capital }}"
実行結果[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ***************************************************************************************
TASK [Gathering Facts] ***************************************************************************
ok: [node1]
TASK [debug] *************************************************************************************
ok: [node1] => {
"country": "Japan"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"msg": "tokyo"
}
PLAY RECAP ***************************************************************************************
node1 : ok=3 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
■ 配列
---
- hosts: all
vars:
keihanshin:
- Kyoto
- Osaka
- Kobe
tasks:
- debug:
var: keihanshin
- debug:
var: keihanshin[0]
- debug:
var: keihanshin[1]
- debug:
var: keihanshin[2]
- debug:
msg: "{{ keihanshin[0] }}"
実行結果[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ***************************************************************************************
TASK [Gathering Facts] ***************************************************************************
ok: [node1]
TASK [debug] *************************************************************************************
ok: [node1] => {
"keihanshin": [
"Kyoto",
"Osaka",
"Kobe"
]
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"keihanshin[0]": "Kyoto"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"keihanshin[1]": "Osaka"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"keihanshin[2]": "Kobe"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"msg": "Kyoto"
}
PLAY RECAP ***************************************************************************************
node1 : ok=6 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
■ リスト
---
- hosts: all
vars:
shikoku:
- prefectural: Kagawa
city: Takamatsu
- prefectural: Tokushima
city: Tokushima
- prefectural: Ehime
city: Matsuyama
- prefectural: Kochi
city: Kochi
tasks:
- debug:
var: shikoku
- debug:
var: shikoku[0].prefectural
- debug:
var: shikoku[3].city
- debug:
msg: "{{ shikoku }}"
実行結果
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ***************************************************************************************
TASK [Gathering Facts] ***************************************************************************
ok: [node1]
TASK [debug] *************************************************************************************
ok: [node1] => {
"shikoku": [
{
"city": "Takamatsu",
"prefectural": "Kagawa"
},
{
"city": "Tokushima",
"prefectural": "Tokushima"
},
{
"city": "Matsuyama",
"prefectural": "Ehime"
},
{
"city": "Kochi",
"prefectural": "Kochi"
}
]
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"shikoku[0].prefectural": "Kagawa"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"shikoku[3].city": "Kochi"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"msg": [
{
"city": "Takamatsu",
"prefectural": "Kagawa"
},
{
"city": "Tokushima",
"prefectural": "Tokushima"
},
{
"city": "Matsuyama",
"prefectural": "Ehime"
},
{
"city": "Kochi",
"prefectural": "Kochi"
}
]
}
PLAY RECAP ***************************************************************************************
node1 : ok=5 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
■ vars_files:
---
- hosts: all
vars_files:
- account.yml
tasks:
- debug:
var: account_list
- debug:
var: account_list[0].uname
- debug:
var: account_list[0].uid
- debug:
var: account_list[0].ugroup
- debug:
msg: "account : {{ account_list[2].uname }}"
- debug:
msg: "id : {{ account_list[2].uid }}"
- debug:
msg: "group : {{ account_list[2].ugroup }}"
・ account.ymlaccount_list:
- uname: sato
uid: 1101
ugroup: admins
- uname: ito
uid: 1102
ugroup: admins
- uname: kato
uid: 1103
ugroup: operators
実行結果[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ***************************************************************************************
TASK [Gathering Facts] ***************************************************************************
ok: [node1]
TASK [debug] *************************************************************************************
ok: [node1] => {
"account_list": [
{
"ugroup": "admins",
"uid": 1101,
"uname": "sato"
},
{
"ugroup": "admins",
"uid": 1102,
"uname": "ito"
},
{
"ugroup": "operators",
"uid": 1103,
"uname": "kato"
}
]
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"account_list[0].uname": "sato"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"account_list[0].uid": "1101"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"account_list[0].ugroup": "admins"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"msg": "account : kato"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"msg": "id : 1103"
}
TASK [debug] *************************************************************************************
ok: [node1] => {
"msg": "group : operators"
}
PLAY RECAP ***************************************************************************************
node1 : ok=8 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$