Package Module
To install packages on a target host, use the module that corresponds to the host’s package manager. For instance, use theyum module for CentOS or Red Hat Enterprise Linux and the apt module for Ubuntu. Starting with Ansible 2.0, you can also use the unified package module which automatically selects the correct package manager based on the host. However, keep in mind that package names might differ between operating systems—for example, the web server package is known as “httpd” on CentOS but “apache2” on Ubuntu. In such scenarios, managing package names with variables or conditionals becomes crucial.
Using the
package module with a single package name (e.g., httpd) might not work across different operating systems. Consider advanced techniques such as variables and conditionals to handle such variations.Service Module
Theservice module is used to manage system services. Whether you need to start, stop, or restart a service, or ensure it is enabled to start at boot time, this module has you covered.
Firewalld Module
For firewall configuration on CentOS or Red Hat systems, thefirewalld module is the go-to tool. Configure rules based on port, protocol, service, or source address, and specify the appropriate zone. By default, Ansible applies changes immediately; however, if you need rules to persist after reboots, set the permanent option to yes. Keep in mind that with permanent enabled, rules will not take immediate effect unless the immediate option is also set to yes.
LVM Modules
The LVM modules allow you to manage Logical Volume Management efficiently. Start by creating a volume group using thelvg module, then create logical volumes within the group using the lvol module. The example below creates a volume group named “vg1” on specified physical devices, followed by the creation of a logical volume “lvol1” with a size of 2GB.
File System and File Modules
The filesystem and file management modules are essential for creating filesystems, mounting devices, and managing files or directories. Use the filesystem module to create filesystems on devices and the mount module for mounting. Thefile module comes in handy for creating files, directories, and symbolic links with specific permissions and ownership settings.
For example, the following playbook creates an application directory and an empty HTML file with defined ownership and permissions:
Archive and Unarchive Modules
Thearchive module is useful for compressing files or directories. By default, it creates a GZ archive, though you can specify different formats using the format option. Conversely, the unarchive module decompresses files and can transfer archives from the Ansible controller to target systems. When working with files already existing on the remote host, set remote_src: yes.
Cron Module
Thecron module automates the configuration of scheduled tasks (cron jobs) on managed nodes. With this module, you can specify details such as the job name, command, and exact schedule parameters like month, day, hour, minute, and even weekday.
Example: Specific Date and Time
Schedule a job to execute on February 19 at 08:10.Example: Using Wildcards
Run the task every day at 08:10 using wildcards for the month and day values.Example: Using Step Values
Configure a task to run every two minutes using step values in the minute field:User, Group, and Authorized Keys Modules
Managing user accounts and groups is made simple with theuser and group modules. The user module allows you to set properties such as user ID, default group, login shell, and more. Additionally, the group module is used to create or manage user groups.
authorized_keys module. This module distributes public SSH keys to specified user accounts on managed nodes.
That concludes the overview of these additional Ansible modules. In upcoming labs, you’ll have plenty of practical exercises to deepen your understanding and enhance your skills with these modules. Happy automating!