1

I set up my servers with non-standard ssh port. I would like to find a way to build an indempotent master playbook, but I seem to have trouble with an UNREACHABLE error while gathering facts.

I have been trying to use this post as a guide: https://dmsimard.com/2016/03/15/changing-the-ssh-port-with-ansible/

However, I am not even able to get to that first step if my port is configured differently.

My master playbook looks like this:

# master playbook
---
- name: Install all the packages for a server
  hosts: test-collection-01
  vars_files:
    - vars/test_collection.yml
  remote_user: foo_user
  roles:
    - debugger

Then, the debug playbook contains pretty much what the aforementioned blog post contains.

---
- name: DEBUG FACTS
  debug:
    msg: 'ansible port: {{ ansible_port }}\,Configured Hostname: {{ inventory_hostname }}'


- name: Try initial ssh port
  set_fact:
    ansible_port: 22

...

However, If my server has already had its ssh port changed to something else, the playbook wont even run and it fails in the "Gathering Facts" step.

TASK [Gathering Facts] 
***
fatal: [198.199.x.x]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 198.199.x.x port 22: Connection refused\r\n", "unreachable": true}

Is there a way to run the port checking code during "Gathering Facts" or before?

4
  • Are you supposed to know the SSH port if it was already changed? Or do you want to find out which port is the SSH and assign that to ansible_port? Jun 28, 2018 at 21:23
  • I know the port it should be. If its not 22, its 2222 Jun 28, 2018 at 22:57
  • You can mention custom port in your inventory file. "/etc/ansible/hosts " like xxx.xxx.xxx.xxx:2222
    – Sunil Bhoi
    Jun 29, 2018 at 8:32
  • @sunil bhoi i do that, but its not indempotent Jul 1, 2018 at 3:10

1 Answer 1

1

Before any of your tasks, Ansible by default, implicitly runs a setup task (gathering facts) which uses the default connection parameters and this is where your play fails.

Add gather_facts: false to your play to avoid that:

---
- name: Install all the packages for a server
  hosts: test-collection-01
  gather_facts: false
  vars_files:
    - vars/test_collection.yml
  remote_user: foo_user
  roles:
    - debugger

Then, if you need the facts, explicitly add setup task after you established the correct port.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .