Getting Server Facts in Ansible

From bib. source

Use ansible with the -a argument ‘hostname’ to run hostname against all the servers:

$ ansible multi -a "hostname"

That is, one can use the a option to get information about a server. More generally, the a option for the ansible command allows one to make use of Ansible module arguments in the command line (find this here and see it also in use here). In this case, one was acquiring the hostname of the relevant servers or server groups under the multi group in the relevant ansible inventory file. Rewritten, this could equivalently be:

ansible -a "hostname" ${INVENTORY_FILE_GROUP}

Other information that one can get about servers by using Ansible is storage information, by running (Geerling 2023, 25):

ansible multi -a "df -h"

For memory information, instead (Geerling 2023, 26-27):

ansible multi -a "free -m"

Command Argument Termination

Notice that the name of the server group or suptragroup of server groups can be a command argument placed either at the beginning or end of the ansible command line.

And finally, for date and time information about our servers (Geerling 2023, 27):

ansible multi -a "free -m"

For an exhaustive list of environment details, called server facts in Ansible, one can run (Geerling 2023, 28):

ansible <host-or-group> -m setup

Wherein host-or-group is the inventory file group name for which server facts are sought.

command_line command_line_interface bash bash_script bash_scripting Bourne_Again_Shell inventory_file configuration_management Bourne_Again_Shell_script Bourne_Again_Shell_scripting argument modules server_facts ad-hoc_command ad-hoc_commands


bibliography

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