How to use Ansible over Meshnet
Learn how to configure Ansible for remote connections over Meshnet on macOS and Linux.
Ansible is free, open-source software available on macOS and Linux that is used for cross-platform automation. It allows you to control numerous host devices at the same time. You can automate virtual machine creation, operating system installation, software configuration, and much more.
While you can use Ansible with remote devices, you must ensure that the host device’s network allows outside connections by opening ports in the firewall. This configuration may leave the host device vulnerable to malicious intrusion.
By using Meshnet in your Ansible configuration, you can connect devices to virtual private networks. All commands and playbooks are executed via an encrypted tunnel using the NordLynx technology. This results in a fast and secure data exchange between the devices.
In this article, you will find instructions on how to prepare an Ansible control node on macOS and Linux for remote connections over Meshnet.
Note
For the purpose of this article, Ansible host devices will be Linux machines.
Ansible uses a secure shell (SSH) connection between devices. While username and password authentication can be used, SSH keys are preferred. SSH key-based authentication is more secure due to its asymmetric encryption. Additionally, no user input is required for authentication, which makes it easier to set up and execute automated workflows.
Ensure that your Ansible host devices accept SSH connections by following instructions from the Enable SSH access section of the "How to access remote systems over Meshnet using SSH" article.
For this configuration, you need to generate an SSH key on your control node device and copy it to your Ansible host devices. For instructions on setting up SSH keys, see the Configuring SSH keys section of the "How to access remote systems over Meshnet using SSH" article.
With the prerequisites taken care of, you can proceed with installing the Ansible software.
Linux
macOS
- 1.Open Terminal.
- 2.Execute this command to update your repositories and install Ansible:sudo apt update && sudo apt install ansible -y
Ansible and its required packages are now installed on your system and ready for use.
- 1.Open Terminal.
- 2.Enter the following command to install Homebrew:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 3.Follow the on-screen instructions to finish the installation.
- 4.Install Ansible by running the following command:brew install ansible
Ansible and its required packages are now installed on your system and ready for use.
After installing, you need to point Ansible to the host devices in Ansible’s
hosts
file.Linux
macOS (Intel)
macOS (Apple silicon)
- 1.Open the
hosts
file in the Nano text editor using this command:sudo nano /etc/ansible/hostsIf thehosts
file or theansible
directory are not present on your system, you can create them using the following command:sudo mkdir /etc/ansible && sudo nano /etc/ansible/hosts - 2.Append a line for your Meshnet device as shown:<alias> ansible_host=<NordName> ansible_user=<username>Where:
<alias>
is a custom alias for the host device.<NordName>
is the host device's Nord name or Meshnet IP address.<username>
is the username of the host device's account used for SSH. This line creates an alias for a specified IP address or hostname. The alias can then be used in Ansible commands or playbooks for controlling the specified device. ExamplemeshUbuntu ansible_host=secret.meerkat-altai.nord ansible_user=ubuntu
- 3.Press Ctrl + X, Y, and Enter to save changes and exit.
For each additional host device, you need to append a new line in the
hosts
file.You can also assign host devices into groups. This allows you to control all devices in a group with a single command by addressing them via the group name. The group name must be written inside square brackets before any of the host devices in that group.
For example, to create a group called
meshDevices
, you need to append a [meshDevices]
line above all device lines.
- 1.Create and open the
hosts
file using the Nano text editor by running this command in the terminal:mkdir /usr/local/etc/ansible && nano /usr/local/etc/ansible/hosts - 2.Append a line for your Meshnet device as shown:<alias> ansible_host=<NordName> ansible_user=<username>Where:
<alias>
is a custom name for the host device.<NordName>
is the host device's Nord name or Meshnet IP address.<username>
is the username of the host device's account used for SSH. This line creates an alias for a specified IP address or hostname. The alias can then be used in Ansible commands or playbooks for controlling the specified device.ExamplemeshUbuntu ansible_host=secret.meerkat-altai.nord ansible_user=ubuntu
- 3.Press Control + X, Y, and Return to save changes and exit.
- 4.Create an Ansible configuration file and point it to the
hosts
file using this command:echo "[defaults]\ninventory = /usr/local/etc/ansible/hosts" >> /usr/local/etc/ansible/ansible.cfg - 5.Check if the
ansible.cfg
file has been created with the correcthosts
file path by running this command:cat /usr/local/etc/ansible/ansible.cfg
For each additional host device, you need to append a new line in the
hosts
file.You can also assign host devices into groups. This allows you to control all devices in a group with a single command by addressing them via the group name. The group name must be written inside square brackets before any of the host devices in that group.
For example, to create a group called
meshDevices
, you need to append a [meshDevices]
line above all device lines.
- 1.Create and open the
hosts
file using Nano by running this command in the terminal:mkdir /opt/homebrew/etc/ansible && nano /opt/homebrew/etc/ansible/hosts - 2.Append a line for your Meshnet device as shown:<alias> ansible_host=<NordName> ansible_user=<username>Where:
<alias>
is a custom name for the host device.<NordName>
is the host device's Nord name or Meshnet IP address.<username>
is the username of the host device's account used for SSH. This line creates an alias for a specified IP address or hostname. The alias can then be used in Ansible commands or playbooks for controlling the specified device.ExamplemeshUbuntu ansible_host=secret.meerkat-altai.nord ansible_user=ubuntu
- 3.Press Control + X, Y, and Return to save changes and exit.
- 4.Create an Ansible configuration file and point it to the
hosts
file via this command:echo "[defaults]\ninventory = /opt/homebrew/etc/ansible/hosts" >> /opt/homebrew/etc/ansible/ansible.cfg - 5.Check if the
ansible.cfg
file has been created with the correcthosts
file path by running this command:cat /opt/homebrew/etc/ansible/ansible.cfg
For each additional host device, you need to append a new line in the
hosts
file.You can also assign host devices into groups. This allows you to control all devices in a group with a single command by addressing them via the group name. The group name must be written inside square brackets before any of the host devices in that group.
For example, to create a group called
meshDevices
, you need to append a [meshDevices]
line above all device lines.
To make Ansible more efficient, you want to minimize the number of times you need user input. By default, you are prompted to enter the root password to perform many tasks, for example, installing software or performing updates.
To avoid this, you can disable the sudo password prompt for a specific user on your Ansible host devices.
- 1.Open Terminal.
- 2.Run the following command to safely edit the
sudoers
file:sudo visudo - 3.At the end of the file, append a line, replacing
<username>
with the account’s username:<username> ALL=(ALL) NOPASSWD:ALLThis command will disable the root password requirement for the specified user. Example - 4.Press Ctrl + X, Y, and Return to save changes and exit.
Check if a connection between the control node and host devices can be established by using the following command:
ansible -m ping all
The response you receive should be
SUCCESS
.
With the Ansible
hosts
file configured for your Meshnet devices and the connection verified, you can use standard Ansible commands or playbooks. For detailed instructions, refer to the Ansible documentation.Consider the following example:
ansible meshUbuntu -bm apt -a "name=cowsay state=latest"
In this example, the cowsay application is installed on a remote Ubuntu server, which is specified under the
meshUbuntu
alias in the hosts
file.Once the process finishes,
cowsay
commands can be used on the remote server.
Note
If you choose not to configure SSH keys, you will need to use the
--ask-pass
or the -k
option with your Ansible commands and enter the SSH password.