How to install and use Ansible on Ubuntu 22.04
To Install And Use Ansible On Ubuntu 22.04
Introduction:
Ansible offers open-source automation solutions that simplify processes and operate across diverse environments. By utilizing Ansible, organizations can automate a wide range of tasks effectively. Ansible employs straightforward, human-readable scripts referred to as playbooks for task automation. Additionally, Ansible adheres to key principles, including agent-less architecture, simplicity, scalability, flexibility, idempotence, and predictability.
Procedure:
Step1: Check the OS version using the below command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy
Step 2: Check for updates using the below command
root@linuxhelp:~# sudo apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu jammy-updates/main i386 Packages [674 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1,902 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [339 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [17.8 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [2,259 kB]
Get:10 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1,682 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [388 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1,110 kB]
Get:24 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [18.9 kB]
Fetched 14.6 MB in 5s (3,060 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Step 3: Upgrade the package list using the below command
root@linuxhelp:~# sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Use 'sudo apt autoremove' to remove them.
The following packages have been kept back:
python3-update-manager update-manager update-manager-core
The following packages will be upgraded:
libpython3.10 libpython3.10-minimal libpython3.10-stdlib libssl3 openssl python3.10 python3.10-minimal snapd
8 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
8 standard LTS security updates
Need to get 36.3 MB of archives.
After this operation, 28.7 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libpython3.10 amd64 3.10.12-1~22.04.5 [1,950 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libssl3 amd64 3.0.2-0ubuntu1.17 [1,905 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 python3.10 amd64 3.10.12-1~22.04.5 [509 kB]
snapd.failure.service is a disabled or a static unit not running, not starting it.
snapd.snap-repair.service is a disabled or a static unit not running, not starting it.
Failed to restart snapd.mounts-pre.target: Operation refused, unit snapd.mounts-pre.target may be requested by dependency only (it is configured to refus
e manual start/stop).
See system logs and 'systemctl status snapd.mounts-pre.target' for details.
Could not execute systemctl: at /usr/bin/deb-systemd-invoke line 142.
Setting up libpython3.10-minimal:amd64 (3.10.12-1~22.04.5) ...
Setting up openssl (3.0.2-0ubuntu1.17) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Step 4: Install software properties using the below command
root@linuxhelp:~# sudo apt-get install software-properties-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
software-properties-common is already the newest version (0.99.22.9).
software-properties-common set to manually installed.
The following packages were automatically installed and are no longer required:
libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Step 5: To install ansible before that we need to add repository using the below command
root@linuxhelp:~# sudo apt-add-repository --yes -update ppa:ansible/ansible
Repository: 'deb https://ppa.launchpadcontent.net/ansible/ansible/ubuntu/ jammy-date main'
Description:
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.
http://ansible.com/
If you face any issues while installing Ansible PPA, file an issue here:
https://github.com/ansible-community/ppa/issues
More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Adding repository.
Adding deb entry to /etc/apt/sources.list.d/ansible-ubuntu-ansible-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/ansible-ubuntu-ansible-jammy.list
Adding key to /etc/apt/trusted.gpg.d/ansible-ubuntu-ansible.gpg with fingerprint 6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367
Hit:1 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease
Ign:5 https://ppa.launchpadcontent.net/ansible/ansible/ubuntu jammy-date InRelease
Err:6 https://ppa.launchpadcontent.net/ansible/ansible/ubuntu jammy-date Release
404 Not Found [IP: 185.125.190.80 443]
Reading package lists... Done
E: The repository 'https://ppa.launchpadcontent.net/ansible/ansible/ubuntu jammy-date Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
Step 6: After adding repository, Install Ansible using the below command
root@linuxhelp:~# sudo apt-get install ansible
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
ieee-data python-babel-localedata python3-argcomplete python3-babel python3-distutils python3-jinja2 python3-jmespath python3-kerberos
python3-libcloud python3-netaddr python3-ntlm-auth python3-packaging python3-pycryptodome python3-requests-kerberos python3-requests-ntlm
python3-selinux python3-simplejson python3-winrm python3-xmltodict
Suggested packages:
cowsay sshpass python-jinja2-doc ipython3 python-netaddr-docs
The following NEW packages will be installed:
ansible ieee-data python-babel-localedata python3-argcomplete python3-babel python3-distutils python3-jinja2 python3-jmespath python3-kerberos
python3-libcloud python3-netaddr python3-ntlm-auth python3-packaging python3-pycryptodome python3-requests-kerberos python3-requests-ntlm
python3-selinux python3-simplejson python3-winrm python3-xmltodict
0 upgraded, 20 newly installed, 0 to remove and 3 not upgraded.
Need to get 28.0 MB of archives.
After this operation, 271 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 python-babel-localedata all 2.8.0+dfsg.1-7 [4,982 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 python3-babel all 2.8.0+dfsg.1-7 [85.1 kB]
Setting up python3-argcomplete (1.8.1-1.5) ...
Setting up python3-requests-ntlm (1.1.0-1.1) ...
Setting up python3-babel (2.8.0+dfsg.1-7) ...
update-alternatives: using /usr/bin/pybabel-python3 to provide /usr/bin/pybabel (pybabel) in auto mode
Setting up python3-libcloud (3.2.0-2) ...
Setting up python3-jinja2 (3.0.3-1ubuntu0.2) ...
Setting up python3-netaddr (0.8.0-2) ...
Setting up python3-winrm (0.3.0-2) ...
Setting up ansible (2.10.7+merged+base+2.10.8+dfsg-1) ...
Processing triggers for man-db (2.10.2-1) ...
Step 7: Check the Ansible version use the below command
root@linuxhelp:~# ansible --version
ansible 2.10.8
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]
Step 8: Ansible uses an inventory file to keep track of the servers it manages. Create a file named inventory dot ini using the below command and add the following content
root@linuxhelp:~# nano inventory.ini
[local]
192.168.6.138 ansible_connection=local
Step 9: Playbooks are YAML files that define a series of tasks to be executed. Create a file named playbook.yml using the below command.Add the following content to playbook.yml, which performs a basic task such as installing the vim text editor.
root@linuxhelp:~# nano playbook.yml
---
- name: Install Vim
hosts: local
become: yes
tasks:
- name: Install vim
apt:
name: vim
state: present
Step 10: To Check Syntax and Verify your playbook syntax before running it using the below command
root@linuxhelp:~# ansible-playbook --syntax-check playbook.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: local
playbook: playbook.yml
Step 11: Execute the playbook using the below command
root@linuxhelp:~# ansible-playbook -i inventory.ini playbook.yml
PLAY [Install Vim] **************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.6.133]
TASK [Install vim] **************************************************************************************************************************************
ok: [192.168.6.133]
PLAY RECAP **********************************************************************************************************************************************
192.168.6.133 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Step 12: To check Ansible is working or not , we can remove the vim package use the below command
root@linuxhelp:~# apt remove vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1 vim-runtime
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
vim
0 upgraded, 0 newly installed, 1 to remove and 3 not upgraded.
After this operation, 4,025 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 245702 files and directories currently installed.)
Removing vim (2:8.2.3995-1ubuntu2.17) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/rview (rview) in auto mode
Step 13: Again to execute use the below same execute command:
root@linuxhelp:~# ansible-playbook -i inventory.ini playbook.yml
PLAY [Install Vim] **************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.6.133]
TASK [Install vim] **************************************************************************************************************************************
changed: [192.168.6.133]
PLAY RECAP **********************************************************************************************************************************************
192.168.6.133 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Step 14: To check whether the vim package is installed or not use the below command
root@linuxhelp:~# sudo apt install vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
vim is already the newest version (2:8.2.3995-1ubuntu2.17).
The following packages were automatically installed and are no longer required:
libflashrom1 libftdi1-2 libllvm13 libwpe-1.0-1 libwpebackend-fdo-1.0-1
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
Conclusion :
We have reached the end of this article. In this guide, we have walked you through the steps required to install and use Ansible on Ubuntu 22.04.Your feedback is much welcome.
• The hosts that should receive the playbook
• The tasks to be executed on each host
• Any dependencies or prerequisites for the tasks
• Agentless architecture, which means no software needs to be installed on target nodes
• Easy to learn and use, even for those without extensive scripting experience
• Fast and efficient, with parallel execution capabilities
• Supports multiple platforms and protocols (e.g., Linux, Windows, Solaris, etc.)
• Extensive community support and thousands of pre-built modules