Install a Package
In this step, you will use the Ansible Apt module to install a package on a target system.
First, create a new Ansible playbook file called /home/labex/project/apt-module-playbook.yaml
and open it in a text editor.
Add the following content to the playbook file:
- hosts: localhost
tasks:
- name: Install a package
become: yes
apt:
name: docker-compose
state: present
This is an Ansible playbook for installing a package called docker-compose
on a local host. The use of privileged access to perform the task is specified via become: yes
. In the apt
module, name
specifies that the package to be installed is named docker-compose
, and state: present
ensures that the package is in the installed state.
Then, run the playbook using the following command:
ansible-playbook apt-module-playbook.yaml
Example output:
[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'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Install a package] *******************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finally, check if docker-compose
was successfully installed and see the docker-compose
version.
docker-compose --version
Example output:
docker-compose version 1.29.2, build unknown
Successfully install a package on the target system using the Apt module.