Updated April 10, 2023
Introduction to 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 ‘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 module executes.
Examples to Implement Ansible Apt
Below are the examples mentioned:
Example #1
Installing packages to the hosts using the apt module
In order 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’ in order 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
Output:
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 itinstalled ‘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
- Equivalent Ansible ad-command for above playbooks:
- ansible ubuntu-k8s-node01 -m apt -a “name=nginx” -b
ansible ubuntu-k8s-node01 -m apt -a “name=nginx=1.14.0-0ubuntu1.7” -b
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 ‘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
- 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
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
- Equivalent Ansible ad-hoc command for the above playbooks
- ubuntu-k8s-node01 -m apt -a “pkg=nginx,curl state=present” –b
ansible ubuntu-k8s-node01 -m apt -e list_of_pkgs=nginx,curl -a ‘name=”{{ list_of_pkgs }}” state=present’ -b
Explanation: In the above example, the output is green as there are no changes made to the host since it is already installed.
Example #3
Removing or uninstalling package from the hosts using the apt module
We need to just change the ‘state’ attribute to ‘absent’ in order to remove or uninstall any package from the hosts as below Ansible playbook.
Code:
- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Uninstall/removenginx
apt:
name: Nginx
state: absent
ansible-playbook apt-nginx.yml
Explanation: In the above example, the playbook uninstalled the ‘nginx’ from the host ‘ubuntu-k8s-node01’ however, there is not much difference in the console output as compare to the output of ‘Install nginx’ task except the name of the task.
- Equivalent Ansible ad-hoc command to remove the packages: –
- ubuntu-k8s-node01 -m apt -a “name=nginx state=absent” -b
Example #4
Update repositories cache and install the package
We use the ‘update_cache’ attribute to update the repositories cache and the name of the package to install, here we don’t need to use the ‘state’ attribute. We can also use the ‘cache_vlaid_time’ attribute to update the cache only if the last update is 3600 seconds ago.
- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: update repositories cache and install nginx package
apt:
name: nginx
update_cache: yes
cache_valid_time: 3600
playbook apt-nginx.yml
Example #5
Installing Debian package using the apt-module
We can install .deb package to the host using ‘deb’ attribute and pass the path of the Debian package. Debian package must be available on the host on which we need to install the deb package and we provide the path where the package is available locally.
- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Installing debian package
apt:
deb: /home/ssingh/curl_7.58.0-2ubuntu3.8_amd64.deb
ansible-playbook apt-nginx.yml.
We can also install the .deb package directly from the internet. We need to pass the ‘URL’ of the Debian package instead of the local path.
Code:
- hosts: ubuntu-k8s-node01
become: yes
remote_user: user1
tasks:
- name: Installing debian package from the internet
apt:
deb: http://archive.ubuntu.com/ubuntu/pool/main/c/curl/curl_7.58.0-2ubuntu3.8_amd64.deb
ansible-playbook apt-nginx.yml
- The equivalent ad-hoc command to install Debian packages on the host: –
- ubuntu-k8s-node01 -m apt -a “deb=/home/ssingh/curl_7.58.0-2ubuntu3.8_amd64.deb” -b
ubuntu-k8s-node01 -m apt -a “deb=http://archive.ubuntu.com/ubuntu/pool/main/c/curl/curl_7.58.0-2ubuntu3.8_amd64.deb” -b
Example #6
Removing useless packages from the cache
We need to set ‘autoclean’ attribute to ‘yes’ to remove useless packages from the cache as below: –
Code:
- hosts: ubuntu-k8s-node01
become: yes
remote_user: ssingh
tasks:
- name: Removing useless packages from the cache
apt:
autoclean: yes
ansible-playbook apt-auticlean.yml
- The equivalent ad-hoc command to remove useless packages is as below: –
- ubuntu-k8s-node01 -m apt -a “autoclean=yes” –b
Example #7
Removing unwanted dependencies from the hosts
We use the ‘autoremove’ attribute to remove the dependencies that are no longer required from the hosts. Playbook for the same is as below: –
Code:
- hosts: ubuntu-k8s-node01
become: yes
remote_user: ssingh
tasks:
- name: Removing old dependencies that is no longer required
apt:
autoremove: yes
ansible-playbook apt-autoremove.yml.
- The equivalent ad-hoc command to delete useless dependencies as below: –
- ubuntu-k8s-node01 -m apt -a “autoremove=yes” –b
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.
Recommended Articles
This is a guide to Ansible Apt. Here we discuss an introduction Ansible Apt with top 7 examples to implement Apt in detail. You can also go through our other related articles to learn more –