Ansible Modules via CLI

From bib. source

We want to install the chrony daemon on the server to keep the time in sync. Instead of running the command dnf install -y chrony on each of the servers, we’ll use ansible’s dnf module to do the same […].

$ ansible multi -b -m dnf -a “name=chrony state=present”

It seems clear from the example here that the dnf module is accessed via the Ansible command line interface by using the m option for the ansible command, as it is this option that takes dnf as an argument. The a option then takes as argument, a string with the arguments for that module argued in the m option (for background, see 20250322141340-Getting_Server_Facts_in_Ansible).

As for the b option for the ansible command (Geerling 2023, 28):

From bib. source

The -b option (alias for --become) tells Ansible to run the command with become (basically, run commands with ‘sudo’). […] but if you’re running commands against a server where your user account requires a password for privilege escalation, you should also pass in -K (alias for --ask-become-pass), so you can enter your password when Ansible needs it.

In other words, b option allows you to run the remote commands as the superuser (or administrator / supervisor) on the target server.

Another example of using an Ansible server module through the command line would be the following, wherein the installed service from the previous example is then enabled (i.e., started and set to autorun on boot for the server) (Ibid):

For bib. source

We could use two separate commands, systemcl start chronyd and systemctl enable chronyd, but we’ll use Ansible’s server module instead.

$ ansible multi -b -m service -a “name=chronyd state=started enabled=yes”

command_line_interface command_line modules bash bash_script bash_scripting Bourne_Again_Shell Bourne_Again_Shell_script Bourne_Again_Shell_scripting privilege_escalation commands interfaces options command_line_interfaces command_lines arguments configuration_management modules


bibliography

  • “Ad-Hoc Commands.” In Ansible for DevOps: Server and Configuration Management for Humans, 2nd ed., 18–46. Leanpub, 2023.