Software >> Automation >> Ansible >> Modules >> apt >> Examples

 

apt


## DOCUMENTATION

online help: https://docs.ansible.com/ansible-core/devel/collections/ansible/builtin/apt_module.html
offline help: ansible-doc apt


## EXAMPLE 1
## PLAYBOOK

---
- name: ansible apt module - example 1
  hosts: all
  become: True
  tasks:
  - name: Install apache2 package
    apt:
       name: apache2
       state: present
    register: results1
  - debug: var=results1.stdout_lines



## OUTPUT

PLAY [ansible apt module - example 1] *******************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************
ok: [debian11_int]
ok: [ubuntu20_int]

TASK [Install apache2 package] **************************************************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [debug] ********************************************************************************************************************************
ok: [debian11_int] => {
    "results1.stdout_lines": [
        "Reading package lists...",
        "Building dependency tree...",
        "Reading state information...",
        "The following package was automatically installed and is no longer required:",
        "  analog",
        "Use 'sudo apt autoremove' to remove it.",
        "Suggested packages:",
        "  apache2-suexec-pristine | apache2-suexec-custom",
        "The following NEW packages will be installed:",
        "  apache2",
        "0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.",
        "Need to get 0 B/270 kB of archives.",
        "After this operation, 632 kB of additional disk space will be used.",
        "Selecting previously unselected package apache2.",
        "(Reading database ... ",
        "(Reading database ... 5%",
        "(Reading database ... 10%",
        "(Reading database ... 15%",
        "(Reading database ... 20%",
        "(Reading database ... 25%",
        "(Reading database ... 30%",
        "(Reading database ... 35%",
        "(Reading database ... 40%",
        "(Reading database ... 45%",
        "(Reading database ... 50%",
        "(Reading database ... 55%",
        "(Reading database ... 60%",
        "(Reading database ... 65%",
        "(Reading database ... 70%",
        "(Reading database ... 75%",
        "(Reading database ... 80%",
        "(Reading database ... 85%",
        "(Reading database ... 90%",
        "(Reading database ... 95%",
        "(Reading database ... 100%",
        "(Reading database ... 165392 files and directories currently installed.)",
        "Preparing to unpack .../apache2_2.4.51-1~deb11u1_amd64.deb ...",
        "Unpacking apache2 (2.4.51-1~deb11u1) ...",
        "Setting up apache2 (2.4.51-1~deb11u1) ...",
        "apache-htcacheclean.service is a disabled or a static unit not running, not starting it.",
        "Processing triggers for man-db (2.9.4-2) ..."
    ]
}
ok: [ubuntu20_int] => {
    "results1.stdout_lines": [
        "Reading package lists...",
        "Building dependency tree...",
        "Reading state information...",
        "The following packages were automatically installed and are no longer required:",
        "  libevent-core-2.1-7 libevent-pthreads-2.1-7 libhtml-template-perl libllvm11",
        "  linux-headers-5.11.0-36-generic linux-hwe-5.11-headers-5.11.0-36",
        "  linux-image-5.11.0-36-generic linux-modules-5.11.0-36-generic",
        "  linux-modules-extra-5.11.0-36-generic",
        "Use 'sudo apt autoremove' to remove them.",
        "Suggested packages:",
        "  apache2-doc apache2-suexec-pristine | apache2-suexec-custom",
        "The following NEW packages will be installed:",
        "  apache2",
        "0 upgraded, 1 newly installed, 0 to remove and 19 not upgraded.",
        "Need to get 95.6 kB of archives.",
        "After this operation, 542 kB of additional disk space will be used.",
        "Get:1 http://sg.archive.ubuntu.com/ubuntu focal-updates/main amd64 apache2 amd64 2.4.41-4ubuntu3.7 [95.6 kB]",
        "Fetched 95.6 kB in 1s (68.0 kB/s)",
        "Selecting previously unselected package apache2.",
        "(Reading database ... ",
        "(Reading database ... 5%",
        "(Reading database ... 10%",
        "(Reading database ... 15%",
        "(Reading database ... 20%",
        "(Reading database ... 25%",
        "(Reading database ... 30%",
        "(Reading database ... 35%",
        "(Reading database ... 40%",
        "(Reading database ... 45%",
        "(Reading database ... 50%",
        "(Reading database ... 55%",
        "(Reading database ... 60%",
        "(Reading database ... 65%",
        "(Reading database ... 70%",
        "(Reading database ... 75%",
        "(Reading database ... 80%",
        "(Reading database ... 85%",
        "(Reading database ... 90%",
        "(Reading database ... 95%",
        "(Reading database ... 100%",
        "(Reading database ... 250536 files and directories currently installed.)",
        "Preparing to unpack .../apache2_2.4.41-4ubuntu3.7_amd64.deb ...",
        "Unpacking apache2 (2.4.41-4ubuntu3.7) ...",
        "Setting up apache2 (2.4.41-4ubuntu3.7) ...",
        "Processing triggers for systemd (245.4-4ubuntu3.13) ...",
        "Processing triggers for man-db (2.9.1-1) ...",
        "Processing triggers for gitweb (1:2.25.1-1ubuntu3.2) ...",
        "apache2_invoke gitweb: already enabled",
        "Processing triggers for ufw (0.36-6) ..."
    ]
}

PLAY RECAP **********************************************************************************************************************************
debian11_int               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu20_int               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0