Bobcares

Ansible Error Unreachable | How To Fit It?

by | Mar 4, 2023

While using Ansible Playbook, have you encountered the error “unreachable?” then this article is for you. Bobcares, as a part of our Server Management Services, offers solutions to every query that comes our way.

Ansible Playbook Error “Unreachable”

Ansible identifies a host as “UNREACHABLE” and removes it from the list of active hosts for the run if it is unable to establish a connection. We can ignore a task failure due to the host instance being ‘UNREACHABLE’ with the ignore_unreachable keyword. The ignore_unreachable the keyword is a boolean that enables us to continue the play despite task failures brought on by an unreachable host. This is helpful for groups of volatile/ephemeral hosts but has no effect on other task errors. Ansible disregards the task failures but keeps running subsequent tasks against the unavailable host. As an example, at the task level:

- name: This executes, fails, and the failure is ignored
  ansible.builtin.command: /bin/true
  ignore_unreachable: true

- name: This executes, fails, and ends the play for this host
  ansible.builtin.command: /bin/true

At the Ansible playbook level:

- hosts: all
  ignore_unreachable: true
  tasks:
  - name: This executes, fails, and the failure is ignored
    ansible.builtin.command: /bin/true

  - name: This executes, fails, and ends the play for this host
    ansible.builtin.command: /bin/true
    ignore_unreachable: false

We can use meta: clear_host_errors to reactivate all hosts, so subsequent tasks can try to reach them again. Now we will see some solutions to fix the issue

Troubleshooting Methods

1. User can connect to his Rasberry Pi through ssh through an ethernet cable using SSH running Ansible with this IP address as a host fails. The user configures Rasberry Pi with ansible through wifi (using the wifi IP address). When trying to use ansible via the direct ethernet connection, the following error is received.

The user can successfully connect to this pi using that IP address through ssh from the terminal. He thinks it is a bug in Ansible. The codes tried to run the role are as follows:

ansible-playbook ansible-pi/playbook.yml -i ansible-pi/hosts --ask-pass --sudo -c paramiko -vvvv
ansible-playbook ansible-pi/playbook.yml -i ansible-pi/hosts --ask-pass --sudo -vvvv

The result was the same. Ansible is expected to connect to pi and run the role. However, instead, the unreachable error is displayed.

The solution to fix the issue is by bypassing this by providing ansible_password in the inventory.

2. We can try the code $ ansible-playbook --user=remoteuser -vvv ansible-playbook-test.yml and check the SSH arguments in the output.

3. Another method is by saving the key under the name of id_rsa and then copying the entire key, and paste in the file as follows:

$ cd /root/.ssh
$ ssh-keygen -t rsa

$ cat id_rsa.pub

$ sudo nano authorized_keys

 

To check, run the below code:

$ ansible all -m ping -u root

The result will be like this:

master-node | SUCCESS => {
"changed": false,
"ping": "pong"
}

4. We can try adding the user designation also.

ansible-playbook [...yml] -u deploy

5. A customer has a .ssh/config entry for his user to match the remote hostname. And he can SSH directly to the server with ssh servername.servername

Host servername  
    User username

With Ansible, it is needed to add -u parameter to deploy the code:

ansible-playbook -vvvv -i poc book_deploy.yml --ask-vault-pass --ask-become-pass -u username

The deployment worked with this solution.

[Need help with another issue? We’re just a click away.]

Conclusion

The article provides some of the troubleshooting methods from our Tech team to fix the Ansible error “Unreachable”.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.