Skip to content

Instantly share code, notes, and snippets.

@SJrX
Created November 21, 2020 22:23
Show Gist options
  • Save SJrX/079836233cf982e543fa17a588fadc41 to your computer and use it in GitHub Desktop.
Save SJrX/079836233cf982e543fa17a588fadc41 to your computer and use it in GitHub Desktop.
# The following is a workaround for https://github.com/ansible/ansible/issues/61688
# It users the add_host module to change the host (https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html)
- name: "Determine IP Address"
hosts: localhost
tasks:
- name: "Check if myhost is at location A"
wait_for:
host: 192.168.0.100
port: 22
timeout: 1
register: myhost_vpn
ignore_errors: yes
tags:
- always
- name: "Add host for myhost at location A"
add_host:
name: "myhost"
hostname: "myhost"
ansible_ssh_host: "192.168.0.100"
ansible_ssh_user: user
ansible_become: yes
ansible_become_user: root
ansible_become_method: sudo
when: myhost_vpn is succeeded
tags:
- always
- name: "Add host for myhost at location B"
add_host:
name: "myhost"
hostname: "myhost"
ansible_ssh_host: "10.20.30.40"
ansible_ssh_port: 22
ansible_ssh_user: user
ansible_become: yes
ansible_become_user: root
ansible_become_method: sudo
when: myhost_vpn is failed
tags:
- always
- name: Setup System
hosts: "myhost"
@SJrX
Copy link
Author

SJrX commented Nov 21, 2020

This is a workaround for: ansible/ansible#61688

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment