Ansible how to ignore unreachable hosts before ansible 2.7.x

11,503

Solution 1

I found a good solution here. You ping each host locally to see if you can connect and then run commands against the hosts that passed:

---
- hosts: all
  connection: local
  gather_facts: no
  tasks:
    - block:
        - name: determine hosts that are up
          wait_for_connection:
            timeout: 5
          vars:
            ansible_connection: ssh
        - name: add devices with connectivity to the "running_hosts" group
          group_by:
            key: "running_hosts"
      rescue:
        - debug: msg="cannot connect to {{inventory_hostname}}"

- hosts: running_hosts
  gather_facts: no
  tasks:
  - command: date

Solution 2

With current version on Ansible (2.8) something like this is possible:

- name: identify reachable hosts
  hosts: all
  gather_facts: false
  ignore_errors: true
  ignore_unreachable: true
  tasks:
    - block:
        - name: this does nothing
          shell: exit 1
          register: result
      always:
        - add_host:
            name: "{{ inventory_hostname }}"
            group: reachable

- name: Converge
  hosts: reachable
  gather_facts: false
  tasks:
    - debug: msg="{{ inventory_hostname }} is reachable"
Share:
11,503
Alex Cohen
Author by

Alex Cohen

Looking for part-time adjunct computer science professor positions. ● AWS, GCP, Azure, kvm/bare metal, Vsphere ● Golang, Bash, Ruby, Ansible, HTTP API scripting, C++, C#, Java, MySQL, JavaScript, HTML ● Terraform, Vagrant, Virtualbox, Amazon Web Services (EC2, ELB, RDS, S3, Route 53, CloudWatch, resource tagging, IAM user management, Cloudformation, AWS CLI tools and APIs, ruby AWS-SDK) ● Docker, GitHub, Gitlab CI/CD, Apache, Nginx, Splunk, Nagios, Container Service Discovery and Orchestration, Kubernetes ● Alpine, Ubuntu, Red Hat (RHEL), CentOS, Mac OS X F5 Inc. Software Engineer Level III 4/2021 - present Volterra Inc. Site Reliability Engineer 3/2020 - 4/2021 Illumio Inc. Site Reliability Engineer 9/2016 - 3/2020 ● Working with concourse CI pipelines managing per branch unit tests. ● AWS administration of EC2, EBS, EIP, Route53 and health checks, Cloudwatch, S3, IAM user control, SQS, GuardDuty. ● Automated AWS IAM management with cloudformation. ● Worked with the “Illumio for Containers” service to secure containerized applications running in OpenShift and Kubernetes using Illumio's Adaptive Security Platform. ● Creation and maintenance of developer-facing slack bot running the ruby lita-slack repository codebase to manage multiple AWS applications through stop, start, reboot, tagging, instance creation, developer workgroups, ssh access control, stash repository management, and Illumio software installation functions in combination with SQS and concourse. ● In addition, the bot also performed AWS cost analysis and told jokes. ● Cluster deployment using Ansible and Chef. ● Advanced programming in Ruby, Bash and Python. ● Assisting in maintaining and releasing code to a production SAAS cluster. ● Engaged in on-call rotations to monitor SAAS and production resources. ● Created an automated multi-region multi-account pen-testing tool utilizing NMap and Edgescan. ● Created an academic research project and paper on the software lifecycle of the automated pen-testing tool for my master's degree at Rochester Institute of Technology. ● High level of administration and deployment of Illumio product clusters on workloads, labels, policy, rules, rulesets, vulnerability maps, CLI tools and HTTP API control over resources on Illumio systems. ● Gained Illumio associate product certification. ● Regular use of Jira, Stash, Slack, and Confluence. Created multiple concourse pipelines automating processes of creating Jira tickets, updating stash repositories, updating confluence wiki pages, automated messaging and bot features on Slack. ● Creation of full testing suite for said DevOps oriented chatbot through RSpec codebase function tests, staging servers and smoke tests run through RSpec. ● Taught in internal 3-day bootcamp training courses on the Illumio product, while responsible for the administration, upgrades maintenance and deployment of the training production SAAS clusters.

Updated on July 28, 2022

Comments

  • Alex Cohen
    Alex Cohen almost 2 years

    I'm using ansible to run a command against multiple servers at once. I want to ignore any hosts that fail because of the '"SSH Error: data could not be sent to remote host \"1.2.3.4\". Make sure this host can be reached over ssh"' error because some of the hosts in the list will be offline. How can I do this? Is there a default option in ansible to ignore offline hosts without failing the playbook? Is there an option to do this in a single ansible cli argument outside of a playbook?

    Update: I am aware that the ignore_unreachable: true works for ansible 2.7 or greater, but I am working in an ansible 2.6.1 environment.

  • Marco
    Marco over 2 years
    Just a heads up that the link in this A'er is broken, I looked for a replacement on archive.org but found none.