При наличии инвентаря
shell> cat hosts
[test]
10.1.0.51 serverid='1'
10.1.0.52 serverid='2'
Плейбук
shell> cat pb.yml
- hosts: test
vars:
devangtest:
- ['1','adsdsdasd']
- ['2','kafka2sda']
- ['2','fggfdfgdf']
tasks:
- name: Check directory exists or not.
stat:
path: "/tmp/{{ item.1 }}"
register: alarm_details
when: serverid|int == item.0|int
loop: "{{ devangtest }}"
- set_fact:
dir_stat: "{{ alarm_details.results|
json_query('[].{dir: item[1], stat: stat.exists}') }}"
- debug:
msg: "/tmp/{{ item }} exists"
loop: "{{ dir_stat|json_query('[?stat].dir') }}"
- name: Create a directory if it does not exist
file:
path: "/tmp/{{ item }}/test2"
state: directory
loop: "{{ dir_stat|json_query('[?stat].dir') }}"
выдает, когда ни один из каталогов не существует
shell> ansible-playbook pb.yml
PLAY [test] ***
TASK [Check directory exists or not.] ***
skipping: [10.1.0.52] => (item=[u'1', u'adsdsdasd'])
ok: [10.1.0.51] => (item=[u'1', u'adsdsdasd'])
skipping: [10.1.0.51] => (item=[u'2', u'kafka2sda'])
skipping: [10.1.0.51] => (item=[u'2', u'fggfdfgdf'])
ok: [10.1.0.52] => (item=[u'2', u'kafka2sda'])
ok: [10.1.0.52] => (item=[u'2', u'fggfdfgdf'])
TASK [set_fact] ***
ok: [10.1.0.51]
ok: [10.1.0.52]
TASK [debug] ***
TASK [Create a directory if it does not exist] ***
PLAY RECAP ***
10.1.0.51: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
10.1.0.52: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
С существующим каталогом /tmp/adsdsdasd
на хосте test_01
пьеса дает
shell> ansible-playbook pb.yml
PLAY [test] ***
TASK [Check directory exists or not.] ***
skipping: [10.1.0.52] => (item=[u'1', u'adsdsdasd'])
ok: [10.1.0.52] => (item=[u'2', u'kafka2sda'])
ok: [10.1.0.51] => (item=[u'1', u'adsdsdasd'])
skipping: [10.1.0.51] => (item=[u'2', u'kafka2sda'])
skipping: [10.1.0.51] => (item=[u'2', u'fggfdfgdf'])
ok: [10.1.0.52] => (item=[u'2', u'fggfdfgdf'])
TASK [set_fact] ***
ok: [10.1.0.51]
ok: [10.1.0.52]
TASK [debug] ***
ok: [10.1.0.51] => (item=adsdsdasd) => {
"msg": "/tmp/adsdsdasd exists"
}
TASK [Create a directory if it does not exist] ***
changed: [10.1.0.51] => (item=adsdsdasd)
PLAY RECAP ***
10.1.0.51: ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.1.0.52: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0