Using Ansible for Remote Commands

From bib. source

Lets run a more useful command: $ ansible -i hosts.ini example -a "free -h" -u [username]

The -a option seems capable of running commands in the SSH session established by Ansible.

Note

A more generalized version with environment variables: $ ansible -i $INVENTORY_FILEPATH $INVENTORY_FILE_SECTION_NAME -a "${BASH_COMMAND}" -u $SSH_SERVER_USERNAME.

The example, though, is useful in that (Geerling 2023, 8):

From bib. source

[…], we quickly see memory usage (in a human-readable format) on all the servers (for now just one) in the example group.

Meanwhile, clearly the u option for the ansible command specifies the username one logs into the SSH server as.

configuration_management SSH_protocol Secure_Shell_protocol command command_line server bash bash_script bash_scripting Bourne_Again_Shell Bourne_Again_Shell_script Bourne_Again_Shell_scripting ad-hoc_command ad-hoc_commands


bibliography

  • “Getting Started with Ansible.” In Ansible for DevOps: Server and Configuration Management for Humans, 2nd ed., 1–8. Leanpub, 2023.