issue with stopping the playbook

19 views
Skip to first unread message

itssi...@gmail.com

unread,
Oct 25, 2018, 5:28:30 PM10/25/18
to Ansible Project
hello,

I have playbook that imports multiple sub play books. Basically, i want the main playbook to shut down the servers sequentially..

main play book:

---
- hosts: all
  remote_user: test
  become: yes
  connection: ssh
  gather_facts: false
  #ignore_errors: no
  #any_errors_fatal: true

  tasks:
    - import_tasks: first.yml
      when: inventory_hostname in groups['ma_group']

    - import_tasks: second.yml
      when: inventory_hostname in groups['eg_group'] and

    - import_tasks: third.yml
      when: inventory_hostname in groups['si_group']

    - pause: seconds=20

    - import_tasks: fourth.yml
      when: inventory_hostname in groups['ai_group']
==============================================================================
below are the sub playbooks:


   first.yml 

-     name: execute stopbridges.sh
      shell: /home/test/stopBridges.sh > /home/test/output.log

( The playbook logins to the first server, executes a script)

===============================================================================
   second.yml

    - name: execute stopEG.sh and shutdown
      shell: /home/test/stopEG.sh
      register: script_output

    - debug:
        var: script_output

    - name: shutdown the server now
      command: shutdown -h now
      when: not script_output.failed

(logins to the second server if the script has a execute code of 1, it will not shutdown the server.)
==================================================================================      

 third.yml

    - name: execute stopAI.sh and shutdown
      shell: /home/test/stopAI.sh > /home/test/output1.log
      register: result

    - pause: seconds=10

    - name: shutdown the server now
      command: shutdown -h now
(login to the first server again and executes a different script and shuts down the server)
===================================================================================
 
fourth.yml

    - name: execute stopAI.sh and shutdown
      shell: /home/test/stopAI.sh > /home/test/output.log
      register: result

    - pause: seconds=10

    - name: shutdown the server now
      command: shutdown -h now
login to the third server, executes the script and shuts it down.

=====================================================================================

My issue is with the second.yml file i have an exit code of 1, i want the main playbook to be stopped at the end of second.yml but instead it is just skipping the shutdown in second.yml and shutting down the servers in third and fourth playbooks.

All i want is playbook to be stopped if there is an error and not shut the other servers down!

Please help me!

Kai Stian Olstad

unread,
Oct 26, 2018, 2:37:41 AM10/26/18
to ansible...@googlegroups.com
On 25.10.2018 23:28, itssi...@gmail.com wrote:
> main play book:
>
> ---
> - hosts: all
> remote_user: test
> become: yes
> connection: ssh
> gather_facts: false
> #ignore_errors: no
> #any_errors_fatal: true

You need to enable any_errors_fatal.


> ===============================================================================
> second.yml
>
> - name: execute stopEG.sh and shutdown
> shell: /home/test/stopEG.sh
> register: script_output
>
> - debug:
> var: script_output

You can use the fail module to stop.

- fail:
msg: "Stopping because of a failure"
when: script_output.failed

>
> - name: shutdown the server now
> command: shutdown -h now
> when: not script_output.failed
>
> (logins to the second server if the script has a execute code of 1, it
> will
> not shutdown the server.)

<snip />

> My issue is with the second.yml file i have an exit code of 1, i want
> the
> main playbook to be stopped at the end of second.yml but instead it is
> just
> skipping the shutdown in second.yml and shutting down the servers in
> third
> and fourth playbooks.
>
> All i want is playbook to be stopped if there is an error and not shut
> the
> other servers down!

With the proposed changes above this should be the desire effect.

--
Kai Stian Olstad

itssi...@gmail.com

unread,
Oct 26, 2018, 9:03:14 AM10/26/18
to Ansible Project
if i enable the any_errors_fatal, if there is no error in the script and if it executes second.yml, it shut downs the server. 
when the server is unreachable. because of the any_errors_fatal the script stops at second.yml.

i want the script to run third.yml and fouth.yml if there is no error in the second.yml.

 
Reply all
Reply to author
Forward
0 new messages