仅在满足x任务条件的情况下运行yz任务

时间:2020-04-24 19:47:01

标签: ansible ansible-2.x

团队, 我有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

.. .. ..

1 个答案:

答案 0 :(得分:1)

您必须在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 moduleblock usage