首页 > 解决方案 > 仅当在 ansible 中满足 x 任务条件时才运行 yz 任务

问题描述

团队,我有 10 个任务,只有满足 task1 中的条件,我才想运行 2-10 个。

- name: 1Check if the node needs to be processed
  stat: /tmp/fscache-cleaned-up1.log
  register: dc_status
- name: 2Check if the node needs to be processed
  stat: /tmp/fscache-cleaned-up2.log
  register: dc_status
  failed_when: dc_status.stat.exists

.. .. ..

标签: ansibleansible-2.x

解决方案


您必须在 stat 模块调用中使用“路径”,并在组合相关任务时使用“块”,如下所示:

- name: Check if the node needs to be processed
  stat:
    path: /tmp/fscache-cleaned-up1.log
  register: dc_status

- name: Run this only when the log file exists
  block:
    - name: Install something
      yum:
        name:
        - somepackage
        state: present

     - name: Apply a config template
      template:
        src: templates/src.j2
        dest: /etc/foo.conf

    - name: Start a service and enable it
      service:
        name: bar
        state: started
        enabled: True
  when: 
    - dc_status.stat.exists
    - dc_status.stat.is_file

附加信息:ansible stat 模块块使用


推荐阅读