Ruan Bekker's Blog

From a Curious mind to Posts on Github

Save Output to Local File With Ansible

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

Inventory

Our inventory.ini file:

1
2
[localhost]
localhost

The Script

Our script: /tmp/foo

1
2
3
#!/usr/bin/env bash
echo "foo"
echo "bar"

Apply executable permissions:

1
$ chmod +x /tmp/foo

Playbook

Our playbook: debug.yml

1
2
3
4
5
6
7
---
- hosts: localhost
  tasks:
    - shell: /tmp/foo
      register: foo_result
      ignore_errors: True
    - local_action: copy content= dest=file

Running

Running the Ansible Playbook:

1
2
3
4
5
6
7
8
9
10
11
12
$ 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:

1
2
3
$ cat file
foo
bar

Read More

For more content on Ansible check out my Ansible category