stat: path="{{ '/etc/sysconfig/network-scripts/ifcfg-' + item }}" with_items: "{{ ansible_interfaces }}"
"{{ ('/etc/sysconfig/network-scripts/ifcfg-' + item) | dirname }}"
- hosts: 127.0.0.1 gather_facts: False vars: test_list: [ aaa, bbb, ccc ] test_path: /tmp/parent1/child1/child2/test.txt tasks: - name: join debug: var="{{ test_list | join(' ') }}" - name: basename debug: var={{ test_path | basename }} - name: dirname debug: var={{ test_path | dirname }} - name: expanduser debug: var={{ '~/test' | expanduser }}
ansible-playbook test.yml TASK: [join] ****************************************************************** ok: [127.0.0.1] => { "aaa bbb ccc": "{{ aaa bbb ccc }}" } TASK: [basename] ************************************************************** ok: [127.0.0.1] => { "test.txt": "{{ test.txt }}" } TASK: [dirname] *************************************************************** ok: [127.0.0.1] => { "/tmp/parent1/child1/child2": "{{ /tmp/parent1/child1/child2 }}" } TASK: [expanduser] ************************************************************ ok: [127.0.0.1] => { "/home/CURRENT_USER/test": "{{ /home/CURRENT_USER/test }}" }
x_val1: default1
- debug: msg: "{{ data|ansible.utils.remove_keys(target=['note', 'comment']) }}"
keys(), values()が使えるが、python2/3で挙動が異なるため、listに変換する。
"{{ var1.keys() | list }}" "{{ var1.values() | list }}"
vars_files: - vars/external_vars.yml
動的にvars_filesを指定する
- hosts: localhost vars: - var_file1: example1.yml vars_files: - "vars/{{ var_file1 }}" tasks: - debug: var=foo
ansible-playbook playbook.yml -e "var_file1=example2.yml"
defaults/ main/ example1.yml example2.yml vars/ main/ example1.yml example2.yml
group_vars/ <group1>/ example1.yml example2.yml host_vars/ <host1>/ example1.yml example2.yml
long_dict1_key2: key1: val1 key2: val2 long_dict1: key1: key1: val1 key2: val2 key2: "{{ long_dict1_key2 }}"
ある変数を元に別の変数を参照したい場合。
{{ somevar_{{other_var}} }} # NG {{ hostvars[inventory_hostname]['somevar_' + other_var] }} # OK
- hosts: localhost gather_facts: true become: no connection: local vars: os_var: default: /path/to/default RedHat: /path/to/centos RedHat-7: /path/to/centos7 tasks: - debug: msg="{{ os_var[ansible_os_family ~ '-' ~ ansible_distribution_major_version] | default(os_var[ansible_os_family] | default(os_var['default'])) }}"
- hosts: localhost gather_facts: true become: no connection: local vars: os_var: RedHat: "6": /path/to/centos6 "7": /path/to/centos7 tasks: - debug: msg="{{ os_var[ansible_os_family][ansible_distribution_major_version] }}"
vars: - array1: - aaa - bbb - array2: - ccc - key_name: key1 - hash1: key1: "{{ array1 }} + {{ array2 }}" tasks: - debug: var=hash1 - debug: msg="{{ hash1[key_name] }}"
TASK [debug] ********************************** ok: [localhost] => { "changed": false, "hash1": { "key1": [ "aaa", "bbb", "ccc" ] } }
- hosts: localhost gather_facts: False connection: local vars: var_def: - name: name1 options: - opt1 - name: name2 options: - opt2 var_opt1: # - "{{ var_def | flatten() }}" # これは二重配列になる - "{{ var_def.0 }}" - name: name3 options: - opt3 var_opt2: - name: name4 options: - opt4 tasks: - name: var_def + var_opt2 debug: msg: "{{ var_def + var_opt2 }}" - name: var_opt1 debug: var: var_opt1
ansible-playbook playbbook.yml ... TASK [var_def + var_opt2] **************************************************************************************************************************** ok: [localhost] => { "msg": [ { "name": "name1", "options": [ "opt1" ] }, { "name": "name2", "options": [ "opt2" ] }, { "name": "name4", "options": [ "opt4" ] } ] } TASK [var_opt1] ************************************************************************************************************************************** ok: [localhost] => { "var_opt1": [ { "name": "name1", "options": [ "opt1" ] }, { "name": "name3", "options": [ "opt3" ] } ] }
hash型変数の場合、通常はreplaceなので全て置き換わる。
example_conf: var1: foo var2: bar
example_conf: var1: hoge
- hosts: localhost connection: local gather_facts: false vars_files: - default.yml - overwrite.yml tasks: - debug: var: example_conf
vi ./ansible.cfg ---- [defaults] hash_behaviour = merge ----