変数¶
Ansible はプレイ内で値を格納するための変数をサポートしています。変数はプレイ内、インベントリ内、コマンドラインなどで定義でき、定義する場所に基づいて優先順位が設定されています。
変数をプレイ内で使用する場合、{{
と}}
でくくります。
変数の使い方¶
インベントリ内で変数 stage を定義し、この変数の値によりプレイ内で管理対象ホストにコピーするファイルを変更する例で変数の使い方を学びます。
変数の定義¶
インベントリの内容です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ---
all:
vars:
ansible_user: vagrant
hosts:
node1:
ansible_host: 192.168.1.161
ansible_ssh_private_key_file: ~/.ssh/node1_key
node2:
ansible_host: 192.168.1.162
ansible_ssh_private_key_file: ~/.ssh/node2_key
node3:
ansible_host: 192.168.1.163
ansible_ssh_private_key_file: ~/.ssh/node3_key
stage: prod
children:
web:
vars:
stage: dev
hosts:
node2:
node3:
|
変数 satege を15 行目で管理対象ホスト node3 に、18-19 行目で web グループ全体に適用するよう定義しています。変数が管理対象ホストにどの様に影響するのかansible-inventory
コマンドでインベントリの内容を確認します。
[vagrant@ansible ansible-files]$ ansible-inventory -i hosts.yml --list --yaml
all:
children:
ungrouped:
hosts:
node1:
ansible_host: 192.168.1.161
ansible_ssh_private_key_file: ~/.ssh/node1_key
ansible_user: vagrant
web:
hosts:
node2:
ansible_host: 192.168.1.162
ansible_ssh_private_key_file: ~/.ssh/node2_key
ansible_user: vagrant
stage: dev
node3:
ansible_host: 192.168.1.163
ansible_ssh_private_key_file: ~/.ssh/node3_key
ansible_user: vagrant
stage: prod
[vagrant@ansible ansible-files]$
プレイの作成¶
管理対象ホストにコピーするファイルを 2 つ作成します。
prod_index.html
ファイルの内容です。
注意してください。これは製品版の Web サーバーです。<br>
dev_index.html
ファイルの内容です。
これは開発中の Web server です。<br>
変数 stage に設定した値により、管理対象ホストにコピーするファイルを変更するプレイです。9 行目の管理対象ホストにコピーするファイルを指定する部分で変数を使用しています。
1 2 3 4 5 6 7 8 9 10 | ---
- name: Apache server installed
hosts: web
become: yes
tasks:
- name: copy index.html
copy:
src: ~/ansible-files/{{ stage }}_index.html
dest: /var/www/html/index.html
|
プレイの実行¶
作成したプレイを実行します。
[vagrant@ansible ansible-files]$ ansible-playbook -i hosts.yml apache.yml
PLAY [Apache server installed] ******************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node3]
ok: [node2]
TASK [copy index.html] **************************************************************************************************************************************
changed: [node2]
changed: [node3]
PLAY RECAP **************************************************************************************************************************************************
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[vagrant@ansible ansible-files]$
変数 stage の値にもとづき所定のファイルが正しくコピーされたかブラウザーで確認します。


変数 stage の値にもとづき、管理対象ホスト node2 にdev_index.html
ファイルが、 node3 にprod_index.html
ファイルが正しくコピーされました。
マジック変数¶
Ansible の定義済み変数を「マジック変数」と呼びます。定義済みなので、前段の変数 stage のような定義は不要です。主にインベントリの内容や実行環境情報を保持します。
主なマジック変数¶
- inventory_hostname
- インベントリ内の管理対象ホスト名
- group_names
- インベントリ内で管理対象ホストが属するグループの一覧
- groups
- インベントリ内の全グループと管理対象ホストの一覧
- inventory_dir
- インベントリファイルのディレクトリパス(インベントリファイル名は含まれない)
- inventory_file
- インベントリファイルのパス
- playbook_dir
- プレイブックファイルのディレクトリパス(プレイブックファイル名は含まれない)
- host_vars
- 管理対象ホストのファクト変数を集めたもの
主なマジック変数の確認¶
次の内容で主なマジック変数の値を確認します。
■ インベントリファイル
---
all:
vars:
ansible_user: vagrant
hosts:
node1:
ansible_host: 192.168.1.161
ansible_ssh_private_key_file: ~/.ssh/node1_key
node2:
ansible_host: 192.168.1.162
ansible_ssh_private_key_file: ~/.ssh/node2_key
node3:
ansible_host: 192.168.1.163
ansible_ssh_private_key_file: ~/.ssh/node3_key
stage: prod
children:
web:
vars:
stage: dev
hosts:
node2:
node3:
■ プレイブックファイル
- name: 主なマジック変数の確認
hosts: all
tasks:
- name: inventory_hostname の値
debug:
var: inventory_hostname
- name: group_names の値
debug:
var: group_names
- name: groups の値
debug:
var: groups
- name: inventory_dir の値
debug:
var: inventory_dir
- name: inventory_file の値
debug:
var: inventory_file
- name: playbook_dir の値
debug:
var: playbook_dir
■ 確認
[vagrant@ansible ansible-files]$ ansible-playbook -i hosts.yml showmagcivars.yml
PLAY [主なマジック変数の確認] ******************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node3]
ok: [node1]
ok: [node2]
TASK [inventory_hostname の値] ********************************************************************************************************************************
ok: [node1] => {
"inventory_hostname": "node1"
}
ok: [node3] => {
"inventory_hostname": "node3"
}
ok: [node2] => {
"inventory_hostname": "node2"
}
TASK [group_names の値] ***************************************************************************************************************************************
ok: [node1] => {
"group_names": [
"ungrouped"
]
}
ok: [node3] => {
"group_names": [
"web"
]
}
ok: [node2] => {
"group_names": [
"web"
]
}
TASK [groups の値] ********************************************************************************************************************************************
ok: [node1] => {
"groups": {
"all": [
"node1",
"node3",
"node2"
],
"ungrouped": [
"node1"
],
"web": [
"node3",
"node2"
]
}
}
ok: [node3] => {
"groups": {
"all": [
"node1",
"node3",
"node2"
],
"ungrouped": [
"node1"
],
"web": [
"node3",
"node2"
]
}
}
ok: [node2] => {
"groups": {
"all": [
"node1",
"node3",
"node2"
],
"ungrouped": [
"node1"
],
"web": [
"node3",
"node2"
]
}
}
TASK [inventory_dir の値] *************************************************************************************************************************************
ok: [node1] => {
"inventory_dir": "/home/vagrant/ansible-files"
}
ok: [node3] => {
"inventory_dir": "/home/vagrant/ansible-files"
}
ok: [node2] => {
"inventory_dir": "/home/vagrant/ansible-files"
}
TASK [inventory_file の値] ************************************************************************************************************************************
ok: [node1] => {
"inventory_file": "/home/vagrant/ansible-files/hosts.yml"
}
ok: [node3] => {
"inventory_file": "/home/vagrant/ansible-files/hosts.yml"
}
ok: [node2] => {
"inventory_file": "/home/vagrant/ansible-files/hosts.yml"
}
TASK [playbook_dir の値] **************************************************************************************************************************************
ok: [node1] => {
"playbook_dir": "/home/vagrant/ansible-files"
}
ok: [node3] => {
"playbook_dir": "/home/vagrant/ansible-files"
}
ok: [node2] => {
"playbook_dir": "/home/vagrant/ansible-files"
}
PLAY RECAP **************************************************************************************************************************************************
node1 : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[vagrant@ansible ansible-files]$
ファクト変数¶
管理対象ホストのシステム情報が格納された変数を「ファクト変数」と呼びます。
ファクト変数の内容¶
ファクト変数はアドホックコマンドでsetup
モジュールで確認できます。次の実行結果は管理対象ホスト node1 のファクト変数の内容です。
[vagrant@ansible ansible-files]$ ansible node1 -i hosts.yml -m setup
node1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.0.2.15",
"192.168.1.161"
],
"ansible_all_ipv6_addresses": [
"fe80::5054:ff:fe8a:fee6",
"fe80::20d:58ff:fe00:161"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "12/01/2006",
"ansible_bios_version": "VirtualBox",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-1062.18.1.el7.x86_64",
"LANG": "en_US.UTF-8",
"biosdevname": "0",
"console": "ttyS0,115200n8",
"crashkernel": "auto",
"elevator": "noop",
"net.ifnames": "0",
"no_timer_check": true,
"ro": true,
"root": "UUID=8ac075e3-1124-4bb6-bef7-a6811bf8b870"
},
"ansible_date_time": {
"date": "2020-04-26",
"day": "26",
"epoch": "1587867902",
"hour": "11",
"iso8601": "2020-04-26T02:25:02Z",
"iso8601_basic": "20200426T112502105081",
"iso8601_basic_short": "20200426T112502",
"iso8601_micro": "2020-04-26T02:25:02.105340Z",
"minute": "25",
"month": "04",
"second": "02",
"time": "11:25:02",
"tz": "JST",
"tz_offset": "+0900",
"weekday": "Sunday",
"weekday_number": "0",
"weeknumber": "16",
"year": "2020"
},
"ansible_default_ipv4": {
"address": "10.0.2.15",
"alias": "eth0",
"broadcast": "10.0.2.255",
"gateway": "10.0.2.2",
"interface": "eth0",
"macaddress": "52:54:00:8a:fe:e6",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "10.0.2.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {
"sda": [
"ata-VBOX_HARDDISK_VB3958e045-138cceec"
],
"sda1": [
"ata-VBOX_HARDDISK_VB3958e045-138cceec-part1"
]
},
"labels": {},
"masters": {},
"uuids": {
"sda1": [
"8ac075e3-1124-4bb6-bef7-a6811bf8b870"
]
}
},
"ansible_devices": {
"sda": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"links": {
"ids": [
"ata-VBOX_HARDDISK_VB3958e045-138cceec"
],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VBOX HARDDISK",
"partitions": {
"sda1": {
"holders": [],
"links": {
"ids": [
"ata-VBOX_HARDDISK_VB3958e045-138cceec-part1"
],
"labels": [],
"masters": [],
"uuids": [
"8ac075e3-1124-4bb6-bef7-a6811bf8b870"
]
},
"sectors": "83884032",
"sectorsize": 512,
"size": "40.00 GB",
"start": "2048",
"uuid": "8ac075e3-1124-4bb6-bef7-a6811bf8b870"
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "noop",
"sectors": "83886080",
"sectorsize": "512",
"size": "40.00 GB",
"support_discard": "0",
"vendor": "ATA",
"virtual": 1
}
},
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.7",
"ansible_dns": {
"nameservers": [
"10.0.2.3"
],
"search": [
"flets-west.jp"
]
},
"ansible_domain": "",
"ansible_effective_group_id": 1000,
"ansible_effective_user_id": 1000,
"ansible_env": {
"HOME": "/home/vagrant",
"LANG": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "vagrant",
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:",
"MAIL": "/var/mail/vagrant",
"PATH": "/usr/local/bin:/usr/bin",
"PWD": "/home/vagrant",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "192.168.1.151 50822 22",
"SSH_CONNECTION": "192.168.1.151 50822 192.168.1.161 22",
"SSH_TTY": "/dev/pts/0",
"TERM": "xterm",
"USER": "vagrant",
"XDG_RUNTIME_DIR": "/run/user/1000",
"XDG_SESSION_ID": "9",
"_": "/usr/bin/python"
},
"ansible_eth0": {
"active": true,
"device": "eth0",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "off [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off",
"rx_checksumming": "off",
"rx_fcs": "off",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "on",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipip_segmentation": "off [fixed]",
"tx_lockless": "off [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "off [fixed]",
"tx_sctp_segmentation": "off [fixed]",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "off [fixed]",
"tx_tcp_ecn_segmentation": "off [fixed]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "on [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off [fixed]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "10.0.2.15",
"broadcast": "10.0.2.255",
"netmask": "255.255.255.0",
"network": "10.0.2.0"
},
"ipv6": [
{
"address": "fe80::5054:ff:fe8a:fee6",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "52:54:00:8a:fe:e6",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:00:03.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"ansible_eth1": {
"active": true,
"device": "eth1",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "off [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off",
"rx_checksumming": "off",
"rx_fcs": "off",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "on",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipip_segmentation": "off [fixed]",
"tx_lockless": "off [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "off [fixed]",
"tx_sctp_segmentation": "off [fixed]",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "off [fixed]",
"tx_tcp_ecn_segmentation": "off [fixed]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "on [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off [fixed]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "192.168.1.161",
"broadcast": "192.168.1.255",
"netmask": "255.255.255.0",
"network": "192.168.1.0"
},
"ipv6": [
{
"address": "fe80::20d:58ff:fe00:161",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0d:58:00:01:61",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:00:08.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"ansible_fibre_channel_wwn": [],
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "node1",
"ansible_hostname": "node1",
"ansible_hostnqn": "",
"ansible_interfaces": [
"lo",
"eth1",
"eth0"
],
"ansible_is_chroot": true,
"ansible_iscsi_iqn": "",
"ansible_kernel": "3.10.0-1062.18.1.el7.x86_64",
"ansible_kernel_version": "#1 SMP Tue Mar 17 23:49:17 UTC 2020",
"ansible_lo": {
"active": true,
"device": "lo",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "on [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "on [fixed]",
"netns_local": "on [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "off [fixed]",
"rx_vlan_offload": "off [fixed]",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on [fixed]",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "on [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipip_segmentation": "off [fixed]",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off [fixed]",
"tx_scatter_gather": "on [fixed]",
"tx_scatter_gather_fraglist": "on [fixed]",
"tx_sctp_segmentation": "on",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "on",
"vlan_challenged": "on [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "127.0.0.1",
"broadcast": "host",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 65536,
"promisc": false,
"timestamping": [
"rx_software",
"software"
],
"type": "loopback"
},
"ansible_local": {},
"ansible_lsb": {},
"ansible_machine": "x86_64",
"ansible_machine_id": "a6398579871c2d45a1d70a9a863281d3",
"ansible_memfree_mb": 3477,
"ansible_memory_mb": {
"nocache": {
"free": 3615,
"used": 174
},
"real": {
"free": 3477,
"total": 3789,
"used": 312
},
"swap": {
"cached": 0,
"free": 2047,
"total": 2047,
"used": 0
}
},
"ansible_memtotal_mb": 3789,
"ansible_mounts": [
{
"block_available": 9383586,
"block_size": 4096,
"block_total": 10480385,
"block_used": 1096799,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 20907909,
"inode_total": 20971008,
"inode_used": 63099,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 38435168256,
"size_total": 42927656960,
"uuid": "8ac075e3-1124-4bb6-bef7-a6811bf8b870"
}
],
"ansible_nodename": "node1",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_proc_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-1062.18.1.el7.x86_64",
"LANG": "en_US.UTF-8",
"biosdevname": "0",
"console": [
"tty0",
"ttyS0,115200n8"
],
"crashkernel": "auto",
"elevator": "noop",
"net.ifnames": "0",
"no_timer_check": true,
"ro": true,
"root": "UUID=8ac075e3-1124-4bb6-bef7-a6811bf8b870"
},
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) i7-4930K CPU @ 3.40GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "VirtualBox",
"ansible_product_serial": "NA",
"ansible_product_uuid": "NA",
"ansible_product_version": "1.2",
"ansible_python": {
"executable": "/usr/bin/python",
"has_sslcontext": true,
"type": "CPython",
"version": {
"major": 2,
"micro": 5,
"minor": 7,
"releaselevel": "final",
"serial": 0
},
"version_info": [
2,
7,
5,
"final",
0
]
},
"ansible_python_version": "2.7.5",
"ansible_real_group_id": 1000,
"ansible_real_user_id": 1000,
"ansible_selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 31,
"status": "enabled",
"type": "targeted"
},
"ansible_selinux_python_present": true,
"ansible_service_mgr": "systemd",
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGE6rOPnLm36McURf9DCfyixsu99TtX9Qzxi+zliKO+qAv5V2d2XCpj7YxwUN97T6SMwbBc7FwHNrJcfJr9nqOg=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIKSgMmdTobNNa9V+v+NGivckMV1PJ31DksJ2Ap6MfcjE",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDWX9AKFBfFjQCiQR408nt8VB7RVFwPKwpUgKsMaYKDE9VSeFgsh+oVr+RXjEVxIFgfXeTjGmYA3DmVyIeSpLcyp78tVXqfk0X1czc5AM1kYZJ5LZ/y0Tr0h7z8yVgYYmkpvfSHyHJ7WdbdkxJtHaz+TDDEG8bnv1Z6qmZK2LqRwhNXCX8xO5/uG0oJWFF3o5sMFFzDjGPJkVty760PDHjAS5sTamtrLKHJySJfyNUFAN3BpnS7gCFjB/aTYdXfvt2rH86WaySS95oskyHx6wxd1CK0hYbAY14Sn8qba3QvMpNDZqQ8RQxOwvjp5n7ufRjkcRxb/h0dRGHoVzKr1HV3",
"ansible_swapfree_mb": 2047,
"ansible_swaptotal_mb": 2047,
"ansible_system": "Linux",
"ansible_system_capabilities": [
""
],
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "innotek GmbH",
"ansible_uptime_seconds": 11544,
"ansible_user_dir": "/home/vagrant",
"ansible_user_gecos": "vagrant",
"ansible_user_gid": 1000,
"ansible_user_id": "vagrant",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 1000,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "virtualbox",
"discovered_interpreter_python": "/usr/bin/python",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}
[vagrant@ansible ansible-files]$
ファクト変数の収集タイミング¶
プレイの実行時にファクト変数の収集は次のメッセージが表示されたタイミングで実施します。
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node3]
ok: [node2]
ファクト変数が不要なとき¶
プレイ内でファクト変数を使用しないときは、 5 行目のように gather_facts ディレクティブに no を設定するとファクト変数を収集しません。
1 2 3 4 5 | ---
- name: Apache server installed
hosts: web
become: yes
gather_facts: no
|
ファクト変数の参照方法¶
ファクト変数は次のように参照します。
ansible_facts['変数名']
ファクト変数と参照の例です。
"ansible_distribution": "CentOS",
ansible_facts['distribution']
"ansible_default_ipv4": {
"address": "10.0.2.15",
ansible_facts['default_ipv4']['address']
"ansible_all_ipv4_addresses": [
"10.0.2.15",
"192.168.1.161"
プレイ内で参照する場合、他の変数と同様に{{
と}}
でくくって使用します。
---
- name: show fact variables
hosts: web
tasks:
- name: show ansble_distribution
debug:
msg: "{{ inventory_hostname }} のディストリビューションは {{ ansible_facts['distribution'] }} です。"
- name: show ansible_default_ipv4.address
debug:
msg: "デフォルトの IPv4 アドレスは {{ ansible_facts['default_ipv4']['address'] }} です。"
- name: show all_ipv4_addresses
debug:
msg: "{{ inventory_hostname }} の IP アドレスは {{ ansible_facts['all_ipv4_addresses'][0] }} と {{ ansible_facts['all_ipv4_addresses'][1] }} です。"
実行結果です。
[vagrant@ansible ansible-files]$ ansible-playbook -i hosts.yml showfactvars.yml
PLAY [show fact variables] **********************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node2]
ok: [node3]
TASK [show ansble_distribution] *****************************************************************************************************************************
ok: [node3] => {
"msg": "node3 のディストリビューションは CentOS です。"
}
ok: [node2] => {
"msg": "node2 のディストリビューションは CentOS です。"
}
TASK [show ansible_default_ipv4.address] ********************************************************************************************************************
ok: [node3] => {
"msg": "デフォルトの IPv4 アドレスは 10.0.2.15 です。"
}
ok: [node2] => {
"msg": "デフォルトの IPv4 アドレスは 10.0.2.15 です。"
}
TASK [show all_ipv4_addresses] ******************************************************************************************************************************
ok: [node3] => {
"msg": "node3 の IP アドレスは 10.0.2.15 と 192.168.1.163 です。"
}
ok: [node2] => {
"msg": "node2 の IP アドレスは 192.168.1.162 と 10.0.2.15 です。"
}
PLAY RECAP **************************************************************************************************************************************************
node2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[vagrant@ansible ansible-files]$
参考までに gather_facts ディレクティブに no を設定し、ファクト変数を収集しなかった場合です。
---
- name: show fact variables
hosts: web
gather_facts: no
tasks:
- name: show ansble_distribution
debug:
msg: "{{ inventory_hostname }} のディストリビューションは {{ ansible_facts['distribution'] }} です。"
- name: show ansible_default_ipv4.address
debug:
msg: "デフォルトの IPv4 アドレスは {{ ansible_facts['default_ipv4']['address'] }} です。"
- name: show all_ipv4_addresses
debug:
msg: "{{ inventory_hostname }} の IP アドレスは {{ ansible_facts['all_ipv4_addresses'][0] }} と {{ ansible_facts['all_ipv4_addresses'][1] }} です。"
この場合、ファクト変数自体が定義されないため、ファクト変数を参照しているタスクでエラーが発生します。
[vagrant@ansible ansible-files]$
[vagrant@ansible ansible-files]$ ansible-playbook -i hosts.yml showfactvars.yml
PLAY [show fact variables] **********************************************************************************************************************************
TASK [show ansble_distribution] *****************************************************************************************************************************
fatal: [node3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'distribution'\n\nThe error appears to be in '/home/vagrant/ansible-files/showfactvars.yml': line 7, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: show ansble_distribution\n ^ here\n"}
fatal: [node2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'distribution'\n\nThe error appears to be in '/home/vagrant/ansible-files/showfactvars.yml': line 7, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: show ansble_distribution\n ^ here\n"}
PLAY RECAP **************************************************************************************************************************************************
node2 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
node3 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[vagrant@ansible ansible-files]$
変数の演習¶
- 管理対象ホスト node1 のホスト名と OS のバージョンを持つファクト変数を見つけてください。
ヒント
- ホスト名 → hostname
- OS のバージョン → distribution_version
アドホックコマンドで node1 のホスト名とバージョンを確認してください。
ヒント
- ホスト名 →hostnameコマンド
- OS のバージョン →
/etc/redhat-release
ファイルの内容を確認
ファクト変数を使用して管理対象ホスト node1 のホスト名と OS のバージョンを表示するプレイを作成してください。
ヒント
プレイ内でファクト変数の使用する方法は「ファクト変数の参照方法」を参照してください。
作成したプレイを実行し、実際に管理対象ホスト node1 のホスト名と OS のバージョンを確認してください。
解答¶
[vagrant@ansible ansible-files]$ ansible node1 -i hosts.yml -m setup | grep -i hostname
"ansible_hostname": "node1",
[vagrant@ansible ansible-files]$ ansible node1 -i hosts.yml -m setup | grep -i distribution_version
"ansible_distribution_version": "7.8",
[vagrant@ansible ansible-files]$
[vagrant@ansible ansible-files]$ ansible node1 -i hosts.yml -m command -a hostname
node1 | CHANGED | rc=0 >>
node1
[vagrant@ansible ansible-files]$ ansible node1 -i hosts.yml -m command -a 'cat /etc/redhat-release'
node1 | CHANGED | rc=0 >>
CentOS Linux release 7.8.2003 (Core)
[vagrant@ansible ansible-files]$
[vagrant@ansible ansible-files]$ vim show-info.yml
[vagrant@ansible ansible-files]$ cat show-info.yml
- name: show node1 information
hosts: node1
tasks:
- name: show information
debug:
msg: "管理対象ホスト {{ ansible_facts['hostname'] }} の OS のバージョンは {{ ansible_facts['distribution_version'] }} です。"
[vagrant@ansible ansible-files]$
[vagrant@ansible ansible-files]$ ansible-playbook -i hosts.yml show-info.yml
PLAY [show node1 information] *******************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [node1]
TASK [show information] *************************************************************************************************************************************
ok: [node1] => {
"msg": "管理対象ホスト node1 の OS のバージョンは 7.8 です。"
}
PLAY RECAP **************************************************************************************************************************************************
node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[vagrant@ansible ansible-files]$