Log Checking via Ansible CLI

From bib. source

Any common log file operation (like using tail, cat, grep, etc.) works through the ansible command, with a few caveats […]

These caveats are that these operations cannot be the sort that (Geerling 2023, 40):

  • Continually monitor a file, as Ansible “only displays output after the operation is complete” and “you won’t be able to send the Control-C command to stop following the file”
  • “[R]eturns a huge amount of data via STDOUT via Ansible”
  • “Redirect and filter output from a command run via Ansible” (that is, do the redirect and filter remotely along with the command) unless using the shell module

For example, using tail -f on a remote server via Ansible–as exemplified below–will lead to a process that cannot be remotely terminated and whose output cannot be displayed locally:

ansible $INVENTORY_FILE_GROUP -b -a "tail -f ${FILEPATH}"

So, instead, one could still send the tail command via Ansible provided removal of its f option (Ibid):

ansible $INVENTORY_FILE_GROUP -b -a "tail ${FILEPATH}"

To filter or redirect this, or pipe it, remotely, it becomes necessary to use the shell module, as opposed to the implicit, default command module Ansible uses (Geerling 2023, 41):

ansible $INVENTORY_FILE_GROUP -b -m shell -a "tail ${FILEPATH} | grep ansible-command | wc -1"

More on modules

For background on some of these options, see 20250322150219-Ansible_Modules_via_CLI.

log_file configuration_management Bourne_Again_Shell bash Bourne_Again_Shell_script Bourne_Again_Shell_scripting ad-hoc_command ad-hoc_commands bash_script bash_scripting modules redirection piping unix_pipe unix_piping redirecting command_line command_lines command_line_interface command_line_interfaces modules


bibliography

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