Ansible [关于变量定义的内容]~定期更新
定義在库存文件中。
- inventoryファイルではシーケンス・マッピングを使用できない
[target-1]
10.0.0.90
[target-2]
10.0.0.22
[target:children]
target-1
target-2
[target-1:vars]
ansible_port=22
ansible_user=ec2-user
ansible_ssh_private_key_file=/home/ec2-user/.ssh/ansible-id_rsa
[target-2:vars]
ansible_port=22
ansible_user=centos
ansible_ssh_private_key_file=/home/ec2-user/.ssh/ansible-id_rsa
- 結果
10.0.0.90 | SUCCESS => {
"changed": false,
"ping": "pong"
}
在Playbook中的定义
-
- 外部ファイルからの読み込み①
group_varsに接続情報変数を置く
Playbook内でvars_filesディレクティブを使用する
---
- name: target-1_vars
ansible_port: 22
ansible_user: ec2-user
ansible_ssh_private_key_file: /home/ec2-user/.ssh/ansible-id_rsa
---
- name: vars-files test
hosts: target-1
become: True
vars_files:
- group_vars/target-1_vars.yml
tasks:
- name: ping test
ping:
- 結果
PLAY [vars-files test] *********************************************************
TASK [setup] *******************************************************************
ok: [10.0.0.90]
TASK [ping test] ***************************************************************
ok: [10.0.0.90]
PLAY RECAP *********************************************************************
10.0.0.90 : ok=2 changed=0 unreachable=0 failed=0
外部ファイルからの読み込み②
group_varsにfact毎にPlayを置く
Playbook内でinclude_varsを使用する
distribution_versionでそれぞれのPlayを実行させる
---
- name: different os family
hosts: all
become: True
tasks:
- include_vars: /etc/ansible/group_vars/{{ ansible_distribution_version }}_task.yml
- Amazon 2016.03用
tasks:
- name: ping test
ping:
- CentOS 6.7用
tasks:
- name: ping test
ping:
- 結果
PLAY [different distribution_version] *****************************************************
TASK [setup] *******************************************************************
ok: [10.0.0.22]
ok: [10.0.0.90]
TASK [include_vars] ************************************************************
ok: [10.0.0.90]
ok: [10.0.0.22]
PLAY RECAP *********************************************************************
10.0.0.22 : ok=2 changed=0 unreachable=0 failed=0
10.0.0.90 : ok=2 changed=0 unreachable=0 failed=0