How to Install Ansible on Ubuntu 22.04

How to Install Ansible on Ubuntu 22.04

Posted on

Set up Ansible on Ubuntu 22.04 and configure it to connect with distant servers to automate a number of duties. Ansible is a configuration administration device which presents an structure that doesn’t require particular software program to be put in on nodes, utilizing SSH to execute the automation duties and YAML recordsdata to outline provisioning particulars.

On this information you’re going to discover ways to set up Ansible on a Ubuntu 22.04 server and configure it to attach with distant servers on Google cloud and AWS situations.

Conditions

Root entry to your server or consumer entry with sudo privileges

Step 1: Preliminary server setup

Begin by updating the packages to the most recent model accessible.

sudo apt replace
sudo apt improve

Step 2: Set up Ansible

As soon as all packages are up to date you may proceed to put in Ansible.

sudo apt set up ansible

As soon as the set up is accomplished you may examine the Ansible model put in utilizing the next command.

ansible --version

You’ll get an output much like the one beneath.

Output
ansible 2.10.8
  config file = None
  configured module search path = ['/home/pappinvijak/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python model = 3.10.4 (essential, Apr  2 2022, 09:04:19) [GCC 11.2.0]

Step 3: Setup Stock File

The stock file comprises all details about the hosts that you’ll handle with Ansible.

To configure stock file you may open the /and so on/ansible/hosts file and alter the configurations.

sudo nano /and so on/ansible/hosts

Right here you may configure a number of teams and their very own variables.

Supplied a pattern configuration of distant server connection particulars to connect with Google cloud and AWS situations.

[google_cloud]
gcp_instance_1 ansible_host=EXTERNAL_IP

[google_cloud:vars]
ansible_ssh_user=username
ansible_ssh_private_key_file=path_to_private-key

[aws]
aws_instance_1 ansible_host=EXTERNAL_IP

[aws:vars]
ansible_ssh_user=username
ansible_ssh_private_key_file=path_to_private-key-or-pem-key

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Right here we’ve got created two teams google_cloud and aws and there personal corresponding variables with SSH username and the personal keys.

You may examine the stock utilizing the next command.

ansible-inventory --list -y

You’ll obtain and output much like the one beneath with the respective variables within the corresponding group.

Output
all:
  youngsters:
    google_cloud:
      hosts:
        gcp_instance_1:
          ansible_host: EXTERNAL_IP
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_user=username
          ansible_ssh_private_key_file=path_to_private-key
    aws:
      hosts:
        aws_instance_1:
          ansible_host: EXTERNAL_IP
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_user=username
          ansible_ssh_private_key_file=path_to_private-key-or-pem-key
  ungrouped: {}

You could add the general public key to the distant host and configure the personal key to make a profitable connection.

After you have configured all required particulars you may proceed to check the connection.

Step 4: Take a look at Connection

Now Ansible ought to be capable to connect with the servers listed within the stock file utilizing SSH.

To examine connection on all servers you should utilize the next command.

ansible all -m ping

To examine connection on a selected group you should utilize this command

ansible google_cloud -m ping

The ping module will take a look at

  • if hosts are accessible;
  • when you have legitimate SSH credentials;
  • if hosts are in a position to run Ansible modules utilizing Python.

You’ll get an output much like the one beneath.

Output
gcp_instance_1 | SUCCESS => {
  "modified": false,
  "ping": "pong"
}

If the configuration just isn’t made correctly you’ll get this message.

Output
gcp_instance_1 | UNREACHABLE! => {
  "modified": false,
  "msg": "Failed to connect with the host through ssh: [email protected]: Permission denied (publickey).",
  "unreachable": true
}

In case you have any connection issues it is advisable present correct SSH credentials and be sure you can join utilizing that credentials.

Conclusion

Now you have got realized the right way to set up Ansible on Ubuntu 22.04 and configure it to connect with distant servers on Google cloud and AWS.

Thanks in your time. In case you face any drawback or any suggestions, please go away a remark beneath.

offshore vps