How to install Ansible on Ubuntu Server 22.04

Uncategorized


update concept, software upgrade icon on virtual screen Image: Song_about_summer/ Adobe Stock Ansible makes it much easier for hectic admins to manage a big collection of servers. Rather of needing to remote into each server to handle a job, you can look after much of it from a single point of entry. One reason why I prefer Ansible over similar tools is that Ansible doesn’t require you to set up customers on remote nodes. Instead, Ansible utilizes SSH to execute all tasks, and YAML files hold the meanings of the tasks to be run.

SEE: 40+ open source and Linux terms you need to understand (TechRepublic Premium)

In other words, Ansible makes a job that could be incredibly difficult, easy enough that any admin no matter ability level can look after.

I wish to stroll you through the actions of getting Ansible up and running on Ubuntu Server 22.04 (Jammy Jellyfish). You’ll walk away from this surprised at how simple it can be.

What you’ll require to install Ansible

In order to get Ansible up and running, you’ll need a minimum of two hosts: One to serve as a controller and one host used to check the setup. I’ll be demonstrating with two Ubuntu Server 22.04 circumstances. You’ll likewise require a user with sudo privileges and an SSH essential produced on the controller.

Open source: Must-read coverage

How to install Ansible

Since Ansible is found in the standard repositories, the setup is as basic as logging in to your controller node and releasing the command:

sudo apt-get install ansible -y

The installation will pick up a large number of dependencies and will take anywhere from 2-10 minutes to finish.

We’ll likewise need to install a 2nd piece of software, called SSHpass, which is a non-interactive password company– otherwise you ‘d have difficulty with SSH authentication. Set up SSHPass with:

sudo apt-get set up sshpass -y

How to develop an SSH secret

If you haven’t currently done so, develop an SSH key on the controller with the command:

ssh-keygen

As soon as the key is complete, you’ll require to copy it to your remote host with the command:

ssh-copy-id NODE

Where NODE is the IP address of your remote hosts.

How to produce an inventory file

On your controller, you’ll need to develop an inventory file, which includes all the needed information of your remote hosts. First, develop a directory to house the files with:

sudo mkdir/ etc/ansible

Produce the hosts submit with:

sudo nano/ etc/ansible/hosts

We’ll note our remote host under [servers] with an IP address of 192.168.1.66. That file will look like this:

[servers] 192.168.1.66

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

Conserve and close the file.

How to check if Ansible is working

To evaluate if Ansible is working correctly, provide the command (from the controller):

ansible all -m ping

You must be prompted for your remote user password and, upon successfully pinging the remote host, Ansible will report back:

192.168.1.66|SUCCESS => >”ping”: “pong”

That suggests it’s working.

Let’s evaluate our setup.

How to run an ad-hoc command for testing

Let’s run the ls command on our remote host. To do this, release the command:

ansible all -a “ls -l”

You must exist with a listing of the root directory site of the user related to the SSH secret you sent out to the host, showing all is well.

How to develop and run a playbook

At the heart of Ansible is the playbook. You create these playbooks which draw up the states you want your remote hosts to be in. For example, you can produce a playbook that copies a file, includes a brand-new user, and updates all apt plans on your remote host. Let’s do simply that.

Let’s first produce a directory to house our playbooks with:

mkdir ~/ playbooks

Modification into that directory site with:

cd ~/ playbooks

Produce the brand-new playbook with the command:

nano test.yaml

Because file, paste the following:

name: Test Playbook
hosts: all
jobs:

– name: Copy file hosts with permissions
ansible.builtin.copy:
src:/ etc/ansible/hosts
dest:/ tmp/hosts _ backup
mode: ‘0644’

– name: Add the user ‘olivia’ansible. builtin.user: name:
olivia
ended up being: yes
become_method: sudo

– name: Upgrade all apt packages
apt:
force_apt_get: yes
upgrade: dist
ended up being: yes

Conserve and close the file.

A quick rundown of the playbook appears like this:

  • We run the playbook on all hosts listed in our hosts file.
  • We copy the hosts file to the remote.
  • We add the user “olivia” to the host. One thing to remember with this task is that it only develops the user, not the password.
  • We run apt upgrade on the host.

Run the test playbook with:

ansible-playbook test.yaml — user=USER– extra-vars ansible_sudo_pass=”PASSWORD”

Where USER is the remote user and PASSWORD is the password for that user.

Due to the fact that we’re running an apt upgrade on the system, it might spend some time to finish. When it finishes, your remote host will not just have a new user but will have all of its software upgraded.

And that’s how easy it is to set up and utilize Ansible on Ubuntu Server 22.04. We’ll go back to this topic later to craft more playbooks to see how you can get the most out of Ansible.

Sign up for TechRepublic’s How To Make Tech Work on YouTube for all the latest tech suggestions for company pros from Jack Wallen.

Source

Leave a Reply

Your email address will not be published. Required fields are marked *