Выполнение по условию в ANSIBLE - PullRequest
1 голос
/ 03 мая 2019

У меня есть сценарий, если первое условие успешно, затем выполнить условие два, а если условие два успешно, то выполнить условие три.

Ниже приведено то, что я пробовал.

vi testme.yml
---
- hosts: all

  tasks:
    - name: run this command and ignore the result
      shell: "grep -ci Hello /tmp/data.out"
      register: pingout
      ignore_errors: yes

    - debug: msg="{{ pingout.rc }}"

    - name: run the server if Hello is found in the above task
      command: echo "The server is UP since `uptime`"
      register: output1
      when:  pingout.rc == 0
    - debug: "{{ output1.stdout }}"

КогдаОбнаружена строка, которую я ожидал увидеть выполненной и показанной в выводе: Сервер работает, так как uptime

Однако я не вижу этого напечатанного в выводе.

ansible-playbook -i / tmp / myhost /root/testme.yml

Вывод:

PLAY [all] ******************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [95.76.121.113]

TASK [run this command and ignore the result] *******************************************************************************************************************************
changed: [95.76.121.113]

TASK [debug] ****************************************************************************************************************************************************************
ok: [95.76.121.113] => {
    "msg": "0"
}

TASK [run the server if Hello is found in the above task] *******************************************************************************************************************
changed: [95.76.121.113]

TASK [debug] ****************************************************************************************************************************************************************
ok: [95.76.121.113] => {
    "msg": "Hello world!"
}

PLAY RECAP ******************************************************************************************************************************************************************
95.76.121.113               : ok=5    changed=2    unreachable=0    failed=0

Ответы [ 2 ]

1 голос
/ 03 мая 2019

Вам не нужно проверять rc. Ansible знает, когда команда завершилась неудачей.

---
- hosts: localhost
  connection: local

  tasks:
    - name: First
      shell: "true"
      register: first
      ignore_errors: yes

    - name: Second
      shell: "true"
      register: second
      ignore_errors: yes
      when: first is not failed

    - name: Third
      shell: "true"
      register: third
      ignore_errors: yes
      when: second is not failed
0 голосов
/ 03 мая 2019

Правильный синтаксис:

- debug: msg="{{ output1.stdout }}"
...