Run Ansible through a remote server to other servers

Linux Logo

Run Ansible through a remote server to other servers

Problem Scenario

The VPN connection/service is down for a remote server. Therefore you are unable to use Ansible to restart the service.
However there is another node on the remote network that you can access.

Solution

Run the ansible command or playbook through another server that does have access.

Using ansible_ssh_common_args

This setting is always appended to the default command line for
sftp, scp, and ssh. Useful to configure a ProxyCommand for a
certain host (or group).

Inventory File

invenrory.ini


[unreachablehosts]
unreachablehost

[unreachablehosts:vars]
ansible_ssh_common_args: ‘-o ProxyCommand=”ssh -W %h:%p -q user-to-reachablehost@reachablehost’


Sample Ansible Ad-Hoc Command

ansible unreachablehosts -i inventory.ini -m systemd -a "name=wg-quick@wg0 starte=restarted"

Sample Ansible PlayBook

fix-unreachablehosts.yml


---
- hosts: unreachablehosts
  become: yes
  gather_facts: no
  tasks:
    - name: Restart WireGuard
    systemd:
      name: wg-quick@wg0
      state: restarted

Sample Ansible PlayBook Command

ansible-playbook -i inventory.ini fix-unreachablehosts.yml
  • https://www.ansible.com/
  • https://www.wireguard.com/