2017-08-08 4 views
0

私はYAMLで定義されているすべての私の変数を持ってオフに開始するには、複数のレジスタを反復処理および方法におけるビルドの延長利用するAnsibleは、

あなたが app_dir: "/mnt/{{ item.name }}"app_dir_ext: "/mnt/{{ item.0.name }}"が奇数見て気づくかもしれませんので、私はもともと私の変数を持っていた
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 

YAMLで以下のように設定しましたが、私は大量のワークストリームを持っているときにYAMLの行数が少ないために上記を使用することに決めました。

私は、それらを作成して、注意してください!(権限が適用されない場合は、ディレクトリが存在するかどうかを確認するためにAnsibleコードを持っているの数に file:モジュールを使用する際に伴う操作上のsshのタイムアウトにこのアプローチをとっている
workstreams: 
    - name: tigers 
    service_workstream: tigers-svc 
    app_dir: /mnt/tigers 
    ... 

非常に大きな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する第二、第三、第四のパスを持っているが、作品はI、更新/ <register_name>.stat.exists == falseを使用するために上記のコードをリファクタリングする方法です私の仕事を制御するためにapp_dir_statusapp_sub_dir_statusの両方から?

答えて

0

ネストループを作成する必要はありません! app_sub_dir_statusの中に必要なすべてのデータがあります。不要なアイテムを取り除くだけです。ここで

は、例を単純化しています:

--- 
- hosts: localhost 
    gather_facts: no 
    vars: 
    my_list: 
     - name: zzz1 
     sub: 
      - aaa 
      - ccc 
     - name: zzz2 
     sub: 
      - aaa 
      - bbb 
    tasks: 
    - stat: 
     path: /tmp/{{ item.0.name }}/{{ item.1 }} 
     register: stat_res 
     with_subelements: 
     - "{{ my_list }}" 
     - sub 
    - debug: 
     msg: "path to create '{{ item }}'" 
     with_items: "{{ stat_res.results | rejectattr('stat.exists') | map(attribute='invocation.module_args.path') | list }}" 

あなたにもstat_res.results | rejectattr('stat.exists') | listを反復処理することができますが、/tmp/{{ item.item.0.name }}/{{ item.item.1 }}として再度パスを構築する必要があります - 最初itemとして別のitemが含まれているstat_res.resultsの要素であるので、二重のitemの点に注意してください。 statタスクの元のループの要素。

P.S.また、サブディレクトリタスクはすべての不足しているディレクトリを検出できるので、私はあなたの最初のタスクの理由を参照してください。