Manny
Manny

Reputation: 135

I'd like to write a condition using Ansible shell feature

If Nginx stopped, than print "Nginx stopped, patching in progress".
How to write a condition that Nginx stopped, 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

Upvotes: 0

Views: 52

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39324

You have to use when conditional for this:

---
- 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 

If you are also looking for the service status itself, you are then looking for 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'"

Upvotes: 1

Related Questions