Ansible Apt

Ansible Apt
Written by Sagar RabidasDecember 20, 2021
6 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Sagar Rabidas

Software Developer

In this blog, we will discuss Ansible Apt.

Ansible Apt

Ansible Apt is one of the modules of Ansible that is used to manage packages on Debian or Ubuntu-based OS. For example, installing packages to the hosts, removing packages from the hosts, and updating packages. It works similar to the ‘yum’ package manager that is used for RedHat and it has some advanced capability however if we want to use apt-get, we can use that using attribute ‘force_apt_get’ in the task. It needs python-apt and python3-apt to be installed on the hosts on which the module executes.

Examples to Implement Ansible Apt

Below are the examples mentioned:

Example #1

Installing packages to the hosts using the apt module

To install any package using apt-module, we need just provide the name of the package, optionally we can set the ‘state’ attribute to ‘present’ as below Ansible playbook. We can also set the ‘state’ attribute to ‘latest’ to install the latest version of the package.

Code:

- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Install Nginx
apt:
name: Nginx
state: present

blogpost

Explanation: In the above example, the playbook is running under user ‘user1’ and it requires sudo privilege to manage packages so also set the ‘become’ attribute to ‘yes’ and it installed ‘Nginx’ on the host ‘ubuntu-k8s-node01’.

We can also install the specific version of any package as below:

- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Installing specific version of nginx package
apt:
name: nginx=1.14.0-0ubuntu1.7

ansible-playbook apt-nginx.yml

blogpost

  • Equivalent Ansible ad-command for above playbooks:
  • ansible ubuntu-k8s-node01 -m apt -a “name=nginx” -b
blogpost

blogpost

ansible ubuntu-k8s-node01 -m apt -a “name=nginx=1.14.0-0ubuntu1.7” -b

blogpost

blogpost

Example #2

Installing multiple packages to the host using the apt module

There are different ways to install multiple packages using apt, the first one is to use the ‘pkg’ attribute and write all the packages that need to be installed on the hosts and the second one is to use ‘Ansible Loop’ and pass the value to the ‘name’ attribute one by one and the last one is to define a list of packages and directly pass it to the ‘name’ attribute. We can also use ‘with_items’ instead of a loop.

Code:

- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Install mulitple packages
apt:
pkg:
- nginx
- curl
state: present

ansible-playbook apt-nginx.yml

blogpost
- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Install mulitple packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- curl

ansible-playbook apt-nginx.yml

blogpost

Code:

- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
vars:
list_of_pkgs:
- nginx
- curl
tasks:
- name: Install mulitple packages
apt:
name: "{{ list_of_pkgs }}"
state: present

ansible-playbook apt-nginx.yml

blogpost
  • Equivalent Ansible ad-hoc command for the above playbooks
  • ubuntu-k8s-node01 -m apt -a “pkg=nginx,curl state=present” –b

blogpost

blogpost

ansible ubuntu-k8s-node01 -m apt -e list_of_pkgs=nginx,curl -a ‘name=”{{ list_of_pkgs }}” state=present’ -b

blogpost

Explanation: In the above example, the output is green as there are no changes made to the host since it is already installed.

Conclusion

The apt module is capable of managing packages on only Debian or Ubuntu-based OS. There are few more attributes like purge, force, install_recommends, upgrade, etc. We can use the apt module as an ad-hoc command or in a playbook. We can use some of the attributes together as per our requirements.

ansible
ansible apt
vagrant
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Sagar Rabidas
Software Developer
162 Blog Posts
15 Discussion Threads
Trending Categories
15
Software39
DevOps45
Frontend Development24
Backend Development20
Server Administration17
Linux Administration24
Data Center24
Sentry24
Terraform21
Ansible29
Docker28
Penetration Testing14
Kubernetes16
NGINX19
JenkinsX17
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan