0
votes

I'm running a ansible playbook with several tasks and hosts. In this playbook I'm trying to rerun tasks to failed hosts. I'll try to rebuild the situation:

Inventory:

[hostgroup_1]
host1 ansible_host=1.1.1.1
host2 ansible_host=1.1.1.2

[hostgroup_2]
host3 ansible_host=1.1.1.3
host4 ansible_host=1.1.1.4

The hosts from "hostgroup_1" are supposed to fail, so I can check the error-handling on the two hosts.

Playbook:

---
- name: firstplaybook
  hosts: all
  gather_facts: false
  connection: network_cli
  vars:
    - ansible_network_os: ios

tasks:

- name: sh run
  cisco.ios.ios_command:
    commands: show run

- name: sh run
  cisco.ios.ios_command:
    commands: show run

As expected the fist two hosts (1.1.1.1 & 1.1.1.2) are failing and won't be considered for the second task. After looking to several Ansible documentations I found the meta clear_host_errors task. So I tried to run the playbook like this:

---
- name: firstplaybook
  hosts: all
  gather_facts: false
  connection: network_cli
  vars:
    - ansible_network_os: ios

tasks:

- name: sh run
  cisco.ios.ios_command:
    commands: show run

- meta: clear_host_errors

- name: sh run
  cisco.ios.ios_command:
    commands: show run

Sadly the meta input did not reset the hosts and the Playbook went on without considering the failed hosts again.

Actually I would just like to know how Ansible considers failed hosts in a run again, so I can go on with these.

Thank y'all in advance

Regards, Lucas

2

2 Answers

0
votes

Do you get any different results when using:

ignore_errors: true 

or

ignore_unreachable: yes

with the first task?

0
votes

Q: "How Ansible considers failed hosts in a run again?"

A: Use ignore_unreachable (New in version 2.7.). For example, in the play below the host test_99 is unreachable

- hosts: test_11,test_12,test_99
  gather_facts: false
  tasks:
    - ping:
    - debug:
        var: inventory_hostname

As expected, the debug task omit the unreachable host

PLAY [test_11,test_12,test_99] ********************************************

TASK [ping] ***************************************************************
fatal: [test_99]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh: ssh: Could not resolve
        hostname test_99: Name or service not known'
  unreachable: true
ok: [test_11]
ok: [test_12]

TASK [debug] ***************************************************************
ok: [test_11] => 
  inventory_hostname: test_11
ok: [test_12] => 
  inventory_hostname: test_12

PLAY RECAP *****************************************************************

If you set ignore_unreachable: true the host will be skipped and included in the next task

- hosts: test_11,test_12,test_99
  gather_facts: false
  tasks:
    - ping:
      ignore_unreachable: true
    - debug:
        var: inventory_hostname
PLAY [test_11,test_12,test_99] ********************************************

TASK [ping] ***************************************************************
fatal: [test_99]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh: ssh: Could not resolve
        hostname test_99: Name or service not known'
  skip_reason: Host test_99 is unreachable
  unreachable: true
ok: [test_11]
ok: [test_12]

TASK [debug] ***************************************************************
ok: [test_11] => 
  inventory_hostname: test_11
ok: [test_12] => 
  inventory_hostname: test_12
ok: [test_99] => 
  inventory_hostname: test_99

PLAY RECAP *****************************************************************