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

查看:21
本文介绍了在循环中使用 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
      

      注意如果有任何其他方法可以轻松创建完整的文件夹结构而无需运行五个不同的任务 - 每个文件夹/子文件夹一个 - 并且有人可以建议那是什么,那也将不胜感激!

      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天全站免登陆