How to use Ansible over Meshnet

Learn how to configure Ansible for remote connections over Meshnet on macOS and Linux.

Introduction

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.

Before you begin

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.

Enable host SSH access

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.

Prepare SSH key authentication

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.

Install Ansible

With the prerequisites taken care of, you can proceed with installing the Ansible software.

  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.

Add Meshnet host devices

After installing, you need to point Ansible to the host devices in Ansible’s hosts file.

  1. Open the hosts file in the Nano text editor using this command:

    sudo nano /etc/ansible/hosts

    If the hosts file or the ansible 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. Example

      meshUbuntu 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.

Enable passwordless sudo access

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:ALL

    This command will disable the root password requirement for the specified user. Example

  4. Press Ctrl + X, Y, and Return to save changes and exit.

Test connection to Meshnet hosts

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.

Execute an Ansible command

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.

Last updated

© 2024 Nord Security. All Rights Reserved.