Packages
Manage packages. There are two modules maintained by the Ansible Core Team that manage Linux packages (package, yum). In addition to this, we also use here the module apt
Synopsis
There are four tasks that can be selected by tags:
Debug. Display variables.
Install automatically enabled packages
Install packages listed in lp_packages_install
Remove packages listed in lp_packages_remove
Both tasks which install packages import the task install_package.yml from the role vbotka.linux_lib. Red Hat ansible_os_family will use yum, Debian ansible_os_family will use apt, and others will use package.
See also
Quick guide
Install automatically enabled packages. This includes packages listed in the variables
lp_<name>_packages
wherelp_<name>_install
istrue
shell> ansible-playbook lp.yml -t lp_packages_auto -e lp_packages_auto=true
Install packages listed in lp_packages_install
shell> ansible-playbook lp.yml -t lp_packages_install
Remove packages listed in lp_packages_remove
shell> ansible-playbook lp.yml -t lp_packages_remove
Best practice
Create a playbook and configure variables
shell> cat lp.yml
- hosts: test_01
become: true
roles:
- vbotka.linux_postinstall
Check the syntax of the playbook
shell> ansible-playbook lp.yml --syntax-check
Display variables
shell> ansible-playbook lp.yml -t lp_packages_debug -e lp_packages_debug=true
Don’t make any changes, predict and show differences
shell> ansible-playbook lp.yml -t lp_packages -e lp_packages_auto=true --check --diff
Optionally, limit the execution by tags
shell> ansible-playbook lp.yml -t lp_packages_auto -e lp_packages_auto=true --check --diff
shell> ansible-playbook lp.yml -t lp_packages_install --check --diff
shell> ansible-playbook lp.yml -t lp_packages_remove --check --diff
If all seems right manage the packages and display the results
shell> ansible-playbook lp.yml -t lp_packages -e lp_packages_auto=true -e lp_packages_debug=true
Optionally, limit the execution by tags
shell> ansible-playbook lp.yml -t lp_packages_auto -e lp_packages_auto=true -e lp_packages_debug=true
shell> ansible-playbook lp.yml -t lp_packages_install -e lp_packages_debug=true
shell> ansible-playbook lp.yml -t lp_packages_remove -e lp_packages_debug=true
To minimize both the execution and output, disable skipped hosts and sanity check
shell> ANSIBLE_DISPLAY_SKIPPED_HOSTS=false ansible-playbook lp.yml -t lp_packages -e lp_packages_auto=true -e ll_ipkg_sanity=false
...
TASK [vbotka.linux_postinstall : packages: Instantiate dynamic variables] ***********************
ok: [test_01]
TASK [vbotka.linux_lib : ll install_package: Install (apt)] *************************************
ok: [test_01]
TASK [vbotka.linux_lib : ll install_package: Install (apt)] *************************************
ok: [test_01]
See also
source code packages.yml to learn details.
examples below.
Note
By default, automatic installation of packages is turned off
lp_packages_auto=false
Hint
Use yaml callback
Debug
By default, the debug is turned off lp_package_debug=false
. If
enabled the debug task will display the variables. For example,
1shell> ansible-playbook lp.yml -t lp_packages_debug -e lp_packages_debug=True
2
3 ...
4
5TASK [vbotka.linux_postinstall : packages: Debug] ***********************************************
6ok: [test_01] =>
7 msg: |-
8 ansible_os_family: Debian
9 lp_packages_auto: False
10 lp_package_state: present
11 lp_packages_autoremove: True
12 lp_packages_rescue_end_host: True
13
14 lp_packages_selections_preinstall:
15 - {name: ca-certificates, selection: install}
16
17 lp_packages_install:
18 - ansible
19
20 lp_packages_remove:
21 []
22
23 lp_packages_selections_postinstall:
24 []
25
26 my_packages_install:
27 apparmor: false
28 autofs: true
29 chrony: true
30 debsums: true
31 dnsmasq: false
32 gpg: true
33 gpsd: false
34 kvm: false
35 latex: true
36 libvirt: false
37 logrotate: true
38 networkd: false
39 nfsd: false
40 nm: false
41 packages: [ansible]
42 passwordstore: true
43 postfix: true
44 resolvconf: true
45 smart: true
46 snap: false
47 systemd: true
48 tlp: true
49 ufw: false
50 virtualbox: false
51 wpagui: true
52 wpasupplicant: true
53 xen: false
54 zeitgeist: true
55 zfs: true
56
57 my_packages_lists:
58 apparmor: [apparmor, apparmor-utils, snapd, jq]
59 autofs: [autofs]
60 chrony: [chrony]
61 debsums: [debsums]
62 dnsmasq: [dnsmasq]
63 gpg: [gnupg, gpg, gpg-agent]
64 gpsd: [gpsd, gpsd-clients]
65 kvm: [qemu, qemu-kvm, libvirt-daemon, libvirt-daemon-system, libvirt-clients, bridge-utils]
66 latex: [texlive]
67 libvirt: [libvirt0, libvirt-daemon, libvirt-daemon-driver-storage-rbd, libvirt-daemon-system,
68 libvirt-clients, virtinst]
69 logrotate: [logrotate]
70 networkd: []
71 nfsd: [nfs-kernel-server]
72 nm: [network-manager]
73 passwordstore: [pass]
74 postfix: [postfix]
75 resolvconf: [resolvconf]
76 smart: [smartmontools]
77 systemd: []
78 tlp: [tlp, tlp-rdw]
79 ufw: [ufw]
80 virtualbox: [virtualbox-5.2]
81 wpagui: [wpasupplicant, wpagui, net-tools, ifupdown, wireless-tools]
82 wpasupplicant: [wpasupplicant]
83 xen: [xen-hypervisor-4.16-amd64, xen-utils-4.16, xen-utils-common, xenstore-utils]
84 zfs: [zfsutils-linux]
85
86 my_packages_auto:
87 - [autofs]
88 - [chrony]
89 - [debsums]
90 - [gnupg, gpg, gpg-agent]
91 - [texlive]
92 - [logrotate]
93 - [pass]
94 - [postfix]
95 - [resolvconf]
96 - [smartmontools]
97 - []
98 - [tlp, tlp-rdw]
99 - [wpasupplicant, wpagui, net-tools, ifupdown, wireless-tools]
100 - [wpasupplicant]
101 - [zfsutils-linux]
Note
See defaults/main/packages.yml how the dynamic variables
my_packages_*
are declared.The dictionary
my_packages_install
collects the variableslp_*_install
The dictionary
my_packages_lists
collects the variableslp_*_packages
The list of lists
my_packages_auto
is used to automatically install the listed packages. Flatten the list and remove duplicates by the filter unique if you want to iterate it.
Install packages automatically
By default, the automatic installation of packages is turned off
lp_packages_auto=false
. If enabled the dynamically created list
my_packages_auto is used to install packages. The list
my_packages_auto keeps lists of packages from the dictionary
my_packages_lists enbled in my_packages_install.
See the tasks what lists of packages are required (apparmor, autofs, chrony, …, zfs). For each task, there might be two variables of the form
lp_<name>_packages .... list of packages
lp_<name>_install ..... install packages if true (default=false)
where name is typically a name of a subsystem. See Display subsystems. Install the packages:
shell> ansible-playbook lp.yml -t lp_packages_auto -e lp_packages_auto=true
Install packages
Install packages listed in lp_packages_install. For example,
lp_packages_install:
- ansible
- ansible-lint
- ara-client
Install the packages:
shell> ansible-playbook lp.yml -t lp_packages_install
Hint
This list of packages will be included in the automatic installation if you declare:
lp_ansible_misc_install: true
lp_ansible_misc_packages:
- ansible
- ansible-lint
- ara-client
The name of the list, ansible_misc in this case, must be unique. See Display subsystems.
See also
Remove packages
Remove packages listed in lp_packages_remove. For example,
lp_packages_remove:
- zeitgeist
Remove the packages:
shell> ansible-playbook lp.yml -t lp_packages_remove
See also