Ansible APT Module Examples

Ansible APT Module

ansible apt module is used to install or remove the packages from ubuntu or debian systems. and ansible apt module is used to update the installed packages and to upgrade the system.

Ansible APT Module Examples

lets see the differnet examples of ansible apt module.

ansible apt module command line

ansible all -m apt -a “name=apache2 state=present”

by using above ansible ad-hoc command we can install apache2 in all the target servers.

update cache install packge using ansible apt moule

 tasks:
 - name: Update cache and install "apache2" package
   apt:
    update_cache: yes
    name: apache2

it will update the cache i,e it will update all the packges in /etc/apt/sources.list to new versions. And install the latest version apache2 from sources.list file.

install the package with ansible apt module

 tasks:
 - name: Install apache2 httpd
   apt:
    name: apache2
    state: present

To install any package using ansible apt module we use state argument as present. Present represents,install that particular package.

ansible apt module to remove package or uninstall the pacakge

 tasks:
 - name: Remove "apache2" package
   apt:
    name: apache2
    state: absent

State absent represents remove that particular package

ansible apt module to install multiple packages using with_items

 tasks:
 - name: Install a list of packages
   apt:
    name: "{{ item }}"
    update_cache: yes
    state: present
   with_items:
    - 'git'
    - 'apache2'
    - 'docker-ce'

updates the list of available packages

 tasks:
 - name: Only run "update_cache=yes" if the last one is more than 3600 seconds ago
   apt:
    update_cache: yes
    cache_valid_time: 3600

Here Ansible will not update the cache if it’s been updated in the last 3600 seconds. Ansible will compare last updated time with this task executed time. If the time gap is less than 3600 seconds it will skip this task. If the time gap is greater than 3600 seconds it will update the cache.

update_cache will updates the list of available packages and their versions, i,e it will update apt’s database /etc/apt/sources.ist. Fetches the list of available updates.

install a debian package using ansible apt module

 tasks:
 - name: Install a debian package
   apt:
    deb: /tmp/mypackage.deb

At deb argument mention the path of Debian package in your system. So it will take that debian package from that location and installs it.

Install a debian package package from the internet

 tasks:
 - name: Install a .deb package from the internet
   apt:
    deb: https://example.com/python-ppq_0.1-1_all.deb

At deb argument mention the path of Debian package. So it will download that package from that path  and install it on your system.

ansible apt module upgrade

 tasks:
 - name: Update and upgrade apt packages
   apt:
    update_cache: yes
    upgrade: yes
   become: true

update_cache will fetches the list of available packages and their versions, i,e it will update apt’s database /etc/apt/sources.list. But it does not install or upgrade any packages. After updating the packages in /etc/apt/sources.ist to the new versions, Upgrade installs that newer versions of the packages you have.

Leave a Reply

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