我想使用Ansible shell功能编写条件

时间:2020-07-10 11:42:54

标签: ansible

如果Nginx已停止,则打印“ Nginx已停止,正在修补”
如何编写Nginx停止的条件Nginx==0

---
- hosts: backend
  become: true
  become_user: root
  become_method: sudo

  tasks: 
  - name: Patching the back-end servers 
    shell: if nginx == 0 echo "patching has started" fi

1 个答案:

答案 0 :(得分:1)

您必须在conditionnal时使用它:

---
- hosts: backend
  become: true
  become_user: root
  become_method: sudo

  tasks: 
    - name: Patching the back-end servers 
      shell: echo "patching has started"
      when: nginx == 0 

如果您还正在寻找服务状态本身,那么您正在寻找service_facts

---
- hosts: backend
  become: true
  become_user: root
  become_method: sudo

  tasks: 
    - name: Populate service facts
      service_facts:

    - name: Patching the back-end servers 
      shell: echo "patching has started"
      when: "services_state.ansible_facts.services['ngnix'].state == 'stopped'"