10

We use ansible to configure some hosts, including IP configuration. We do not want to use a DHCP server. Sometimes we add new hosts to the network. Those new hosts are in one ip address range and the existing production hosts are in another. We just put new hosts in the first network and then let ansible configure them, test them and change the IP to the production range. Ansible is run regularly via cron.

However, when there a no new hosts, ansible will report an unreachable error. No new hosts is the usual situation. How can I suppress that or make it less prominent?

Basically our playbook looks like this:

---
#  configure existing hosts
- hosts: production
  tasks:
    - name: do regular maintenance
      # ...

- hosts: new
  # Does not seem to do anything
  ignore_errors: True 
  tasks: 
    - name: configure freshly discovered host
        # ...
    - name: test freshly discovered host
        # ...
    - name: change ip config to production network
        # ...

The /etc/ansible/hosts looks like this:

[production]
192.168.2.[11:255]

[new]
# those are firewalled
192.168.2.[1:10]

When I run this I see a big red

PLAY RECAP   ****************************************************
192.168.2.1              : ok=0    changed=0    unreachable=1    failed=0   
[...]

at the end, which is confusing for my colleagues.

I have seen this question, and I figured that if I use ignore_errors but don't set a flag it should silently skip the unreachable hosts, but it does not seem to have any effect.

icehawk
  • 245

5 Answers5

6

Going to add this answer: a fix was added in Ansible 2.7: https://github.com/ansible/ansible/blob/stable-2.7/changelogs/CHANGELOG-v2.7.rst#major-changes

New keyword ignore_unreachable for plays and blocks. Allows ignoring tasks that fail due to unreachable hosts, and check results with is unreachable test.

EDIT: From personal experience, I need to add ignore_errors along with it for the playbook to actually continue.

KdgDev
  • 5,758
4

I found, in order for a playbook to "succeed" (with exit code 4, see https://jwkenney.github.io/ansible-return-codes/), although some hosts were not reachable, you can do this:

- name: My playbook
  pre_tasks:
    # for windows
    - win_ping:
      ignore_errors: true
    # for linux
    - ping:
      ignore_errors: true

This will remove the hosts failing the task from any subsequent task or role execution.

If you use ignore_unreachable: true, then the subsequent tasks are still executed for the host and this is not what I wanted, because it failed on several variables etc.

Gitlab-ci requires this to not fail:

rc=0
ansible-playbook -i $INVENTORY $PLAYBOOK || rc=$?
if [ $rc -eq 4 -o $rc -eq 0 ]; then echo "ok"; else echo "failure"; exit 1; fi
1

Append to your oneline ansible |grep -A1 'rc=0'.
This will only show the passing plays / servers.

zx485
  • 2,337
0

You'd need to create a callback plugin to modify the way Ansible produces its output.

You can start with default.py provided as an example with Ansible. Search for the unreachable string and modify the script to suit your needs.

For example, to make it less prominent, you can for example override the value of COLOR_UNREACHABLE variable.

Save the modified code to callback_plugins in your project directory, point to the directory, and enable the plugin in the ansible.cfg configuration file.

techraf
  • 4,952
0

One way I found was to run this script against my hosts file first, and then edit the list accordingly. Then run the Ansible script.

It's a work around but still better than having to keep an eye on Ansible all the time waiting for it to stop again.

warhansen
  • 389