Published on

Save Output to Local File with Ansible

Authors

This playbook demonstrates how you can redirect shell output to a local file

Inventory

Our inventory.ini file:

[localhost]
localhost

The Script

Our script: /tmp/foo

#!/usr/bin/env bash
echo "foo"
echo "bar"

Apply executable permissions:

$ chmod +x /tmp/foo

Playbook

Our playbook: debug.yml

---
- hosts: localhost
  tasks:
    - shell: /tmp/foo
      register: foo_result
      ignore_errors: True
    - local_action: copy content={{ foo_result.stdout }} dest=file

Running

Running the Ansible Playbook:

$ ansible-playbook -i inventory.ini debug.yml

PLAY [localhost] ********************************************************************************************************************************************************************

TASK [shell] ************************************************************************************************************************************************************************
changed: [localhost]

TASK [copy] *************************************************************************************************************************************************************************
changed: [localhost -> localhost]

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0

View the local saved file:

$ cat file
foo
bar

Read More

For more content on Ansible check out my Ansible category

Thank You

Thanks for reading, if you like my content, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.

Buy Me A Coffee