Ansible Apt

Ansible Apt: APT means "Advanced Packaging Tool" is the favored bundle of the management toolset in Ubuntu. It permits us to install new bundles, update them, and eliminate Ubuntu or Debian framework's bundles or packages. The following are the most used APT related command-line tools:

APT-get: All the essential bundles of the management tasks would be possible by utilizing this operation. 

APT-add-repository: It is utilized for adding another archive to the repository list. The default repository might not have the most recent version of all the packages. So, we need to add extra repositories for maintaining software. Hence we can say that this module gives the usefulness of adding new repositories.

APT-key: It is utilized to deal with the list of keys for authenticating apt bundles.

Prerequisites (on a host that executes module)

  • python-apt (python 2)
  • python3-apt (python 3)
  • aptitude

Parameters

parameterrequireddefaultchoicescomments
allow_unauthenticated (included in 2.1)noNoyesnoOverlook if bundles can't be verified. It is valuable for bootstrapping conditions that deal with the apt-key arrangement.
autoremove (included in 2.1)No yesnoIndeed, in the event, eliminate new reliance bundles for all module states aside from manufacture dep. It can similarly be utilized as the main alternative.   aliases: auto-clean
cache_valid_timeNo  Update the apt store if it's more established than the cache_valid_time. This alternative is set in short order.    
deb (included in 1.6)No  It’s a way to a .deb bundle on the remote machine.   On the off chance that://in the way, Ansible will endeavor to download deb before installing. (Variant added 2.1)
default_releaseNo  Compares to the - t alternative for apt and sets pin needs
dpkg_optionsNoforce_confdef, force_confold Add dpkg choices to apt order. Defaults to '- o "Dpkg::Options::=- - force confdef" - o "Dpkg::Options::=- - force confold"' Alternatives ought to be provided as comma isolated list
forceNoNoyesnoIn the event that truly power installs/eliminates.
install_recommendsNo yesnoRelates to the - no- install suggests an alternative for adept. Truly introduces recommended bundles. No, it doesn't introduce suggested bundles. Of course, Ansible will utilize similar defaults as the working framework. Proposed bundles are never introduced.
nameNo  A bundle name, as foo, or bundle specifier with a variant, as foo=1.0. Name trump cards (fnmatch) like apt* and variant special cases like foo=1.0* are likewise upheld. Note that the well-suited get command line upholds understood regex coordinates here, yet it can let mistakes through simpler (If you error foo as fo well-suited get would introduce bundles with "fo" in their name with a notice and a brief for the client. Since we don't have warnings and prompts before introducing, we deny this. Utilize an unequivocal fnmatch design in the event that you need wildcarding). false names: pkg, bundle
only_upgrade (included in 2.1)No  Possibly overhaul a bundle on the off chance.  
purgeNo yesnoWill drive cleansing of setup documents if the module state is set to missing.  
stateNopresentlatestabsentpresentbuild-depShows the ideal bundle state. Most recent guarantees that the most recent adaptation is installed. fabricate dep guarantees the bundle assembles conditions are installed.
update_cacheNo yesnoRun what could be compared to well-suited get updates before the activity. It can be run as a feature of the bundle establishment or as a different advance.  
upgradeNoNoyesnosafefulldistIn the event that yes or safe plays out an inclination safe-update.   Assuming full plays out an inclination full-overhaul.   In the event dist, plays out an able get dist-update.   Note: This doesn't overhaul a particular bundle, use state=latest for that.

Putting in new Apt Packages/bundles

If you want to install the new bundles or packages, you need to give the package's name in the name parameter and the ideal condition of the parameter.

The default condition of a particular package is "available." Additionally, it is smarter to set the update_cache to valid. Along these lines, you can guarantee that the lists are synchronized with the source list. It is equivalent to running the well-suited get update order before installing a required package.

The underneath model will do a repository update to synchronize the list. Check if the 'zip' bundle is installed on the objective server or not. Also, on the off chance that it isn't introduced, the bundle will be introduced. On the off chance that the bundle is introduced, it won't be redesigned at that point.

 - hosts: loc
 tasks:
 - name: Ansible apt introduce bundles
 apt:
 name: zip
 state: present
 update_cache: true 

1. Installing the most recent version of a package

In the event that you set the condition of the bundles to "present", at that point, Ansible will possibly check the bundle is available or not. So, if the new bundle is accessible, it won't have the option to introduce it. 

In the event that you need to introduce the most recent well-suited bundles, at that point, you need to set the state boundary to the most recent.

It will guarantee the bundle with the most recent form is installed. The beneath model will refresh the repository first, and then you should install the most recent zip bundle, for example,

 - hosts: loc
 tasks:
 - name: ansible apt install most recent rendition
 apt:
 name: zip
 state: present
 update_cache: true 

2. Installing various packages in ansible

Rather than composing various objects to install packages, you can utilize with_items and consolidate those assignments.

In the following model, we will install three packages: docker-ce, Nginx, and git.

 - hosts: loc
 assignments:
 - name: ansible apt with_items
 apt:
 name: "{{ item}}"
 update_cache: true
 state: present
 with_items:
 - 'docker-ce'
 - 'nginx'
 - 'git' 

3. Ansible Apt ad-hoc

You can likewise utilize the ad-hoc strategy to install new bundles utilizing the apt module, for example,

ansible all - m apt - a "name=nginx state=absent" - I inventory.ini

Eliminating Apt Packages

Likewise, you can eliminate the bundles utilizing the apt module by setting the state boundary to "missing".

The underneath model will eliminate the zip bundle. Since the module is idempotent, it won't experience a mistake if the bundle is absent.

 - hosts: loc
 tasks:
 - name: ansible able eliminate bundle
 apt:
 name: zip
 state: missing 

Return Values

Basic return esteems archived here are Return Values; coming up next are the field novel to this module:

namedescriptionreturnedtype
cache_updatedin the event that the cache was refreshed or notachievement, in some casesboolean
stdoutOutput from aptSuccess, when requiredstring
stderrerror yield from aptSuccess, when requiredstring
cache_update_timetime of last cache update (0 if obscure)Achievement, in some casesdatetime