要開始了我在YAML定義我所有的變量Ansible,遍歷多個寄存器,並在方法長時間使用積累的
app_dir: "/mnt/{{ item.name }}"
app_dir_ext: "/mnt/{{ item.0.name }}"
workstreams:
- name: tigers
service_workstream: tigers-svc
app_sub_dir:
- inbound
- inbound/incoming
- inbound/error
- inbound/error/processed
- name: lions
service_workstream: lions-svc
app_sub_dir:
- inbound
- inbound/incoming
- inbound/error
- inbound/error/processed
你可能注意到app_dir: "/mnt/{{ item.name }}"
和app_dir_ext: "/mnt/{{ item.0.name }}"
看着奇怪,所以我本來有我的變量在YAML中設置如下,但是當我有大量工作流時,決定使用上述內容主要是由於YAML中的行數較少。
workstreams:
- name: tigers
service_workstream: tigers-svc
app_dir: /mnt/tigers
...
我再有Ansible代碼來檢查目錄存在,如果不能創建和應用權限(!注意,採取這種方法由於對操作的SSH超時使用上一個數字的file:
模塊時非常大的NFS掛載份額)。
- name: Check workstreams app_dir
stat:
path: "{{ app_dir }}"
register: app_dir_status
with_items:
- "{{ workstreams }}"
- name: Check workstreams app_sub_dir
stat:
path: "{{ app_dir_ext }}/{{ item.1 }}/"
register: app_sub_dir_status
with_subelements:
- "{{ workstreams }}"
- app_sub_dir
- name: create workstreams app_dir
file:
path: "/mnt/{{ item.0.name }}"
state: directory
owner: "ftp"
group: "ftp"
mode: '0770'
recurse: yes
with_nested:
- '{{ workstreams }}'
- app_dir_status.results
when:
- '{{ item.1.stat.exists }} == false'
這是一個小哈克,但不過工作,我有第二,第三,第四路在這裏check..etc
我的問題是,如何我更新/重構上面的代碼中使用<register_name>.stat.exists == false
從app_dir_status
和app_sub_dir_status
來控制我的任務?