在循环中使用with_items的Ansible 2条件 [英] Ansible 2 conditionals using with_items in loop

查看:82
本文介绍了在循环中使用with_items的Ansible 2条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据布尔值的存在,根据任务中条件检查的结果来运行一些Ansible命令,并试图将其束缚起来以使其正常工作.

I'm trying to run a few Ansible commands depending on the outcome of a conditional check in my tasks based on the presence of a boolean and am tying myself up in knots trying to get it to work.

期望的结果如下,并且应该为hosts数组中的每个项目运行 :

The desired outcome is as follows, and should be run for each item in the hosts array:

  • 检查lynchburg变量是true还是false
  • 如果lynchburg变量是true:
    • 设置文件夹结构(如果尚不存在)
    • 从随附的模板文件创建gulpfile.js(如果尚不存在)
    • Check if the lynchburg variable is true or false
    • If the lynchburg variable is true:
      • Setup the folder structure if it doesn't already exist
      • Create gulpfile.js from the included template file if it doesn't already exist
      • 不设置文件夹结构
      • 不要创建gulpfile.js

      从下面包含的代码中,我看到以下结果:

      From the code included below, I'm seeing the following results:

      • lynchburgtrue时,设置按预期进行 第一次以及以后的任何重新配置​​也可以作为 预期的.
      • lynchburgfalse时,会创建inc文件夹结构 ,但是不会创建gulpfile.js .
      • When lynchburg is true, the provision runs through as expected the first time around and any subsequent reprovisions also work as expected.
      • When lynchburg is false, the inc folder structure is created, however gulpfile.js isn't.

      任务应运行的主要循环如下:

      The main loop the task should run through is the following:

      vhosts:
        - site_name: sample
          lynchburg: true
      

      ,任务如下:

      # Create variable to check whether Lynchburg has already been installed
      - name: Check if Lynchburg assets folders have been previously created
        stat:
          path: /var/www/{{ item.site_name }}/inc
        with_items: "{{ vhosts }}"
        when: item.lynchburg == true
        register: lynchburg_assets
      
      - name: Check if Lynchburg gulpfile.js has been previously created
        stat:
          path: /var/www/{{ item.site_name }}/gulpfile.js
        with_items: "{{ vhosts }}"
        when: item.lynchburg == true
        register: lynchburg_gulpfile
      
      - name: Create inc folder
        with_items: "{{ lynchburg_assets.results }}"
        file:
          path: /var/www/{{ item.item.site_name }}/inc
          state: directory
        when: item.stat.isdir is undefined and item.item.lynchburg == true
      
      - name: Create scss folder
        with_items: "{{ lynchburg_assets.results }}"
        file:
          path: /var/www/{{ item.item.site_name }}/inc/scss
          state: directory
        when: item.stat.isdir is undefined and item.item.lynchburg == true
      
      - name: Create js folder
        with_items: "{{ lynchburg_assets.results }}"
        file:
          path: /var/www/{{ item.item.site_name }}/inc/js
          state: directory
        when: item.stat.isdir is undefined and item.item.lynchburg == true
      
      - name: Create img folder
        with_items: "{{ lynchburg_assets.results }}"
        file:
          path: /var/www/{{ item.item.site_name }}/inc/img
          state: directory
        when: item.stat.isdir is undefined and item.item.lynchburg == true
      
      - name: Create fonts folder
        with_items: "{{ lynchburg_assets.results }}"
        file:
          path: /var/www/{{ item.item.site_name }}/inc/fonts
          state: directory
        when: item.stat.isdir is undefined and item.item.lynchburg == true
      
      - name: Create gulpfile.js
        with_items: "{{ lynchburg_gulpfile.results }}"
        template:
          src: gulpfile.js
          dest: /var/www/{{ item.item.site_name }}/gulpfile.js
        when: item.stat.exists is defined and item.stat.exists == false and item.item.lynchburg == true
      

      NB 如果还有其他方法可以轻松地创建完整的文件夹结构,而无需运行五个不同的任务-每个文件夹/子文件夹一个-有人可以提出建议,那也将不胜感激!

      N.B. If there's any other way to create the full folder structure easily without running five different tasks - one for each folder/subfolder - and someone could advise what that is, that'd also be appreciated!

      推荐答案

      lynchburgfalse时,会创建inc文件夹结构 ,但是不会创建gulpfile.js .

      When lynchburg is false, the inc folder structure is created, however gulpfile.js isn't.

      我知道如何解决它,但我还不知道如何解释它:

      I know how to solve it, I don't know (yet) how to explain it:

      在条件中更改表达式的顺序:

      Change the order of expressions in the conditional:

      when: item.item.lynchburg == true and item.stat.isdir is undefined
      


      由于某些原因:


      For some reason:

      when: dummy.key is undefined and false
      

      when: false and dummy.key is undefined
      

      给出不同的结果.

      仅当检查顶级变量(dummy is undefined)时,它才能按预期工作.

      It works as expected only when checking the top-level variable (dummy is undefined).

      这篇关于在循环中使用with_items的Ansible 2条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆