Best way to accomplish this task using slurp

1,206 views
Skip to first unread message

CyberCyber

unread,
Oct 18, 2015, 11:43:12 PM10/18/15
to Ansible Project
From my Ansible control server, I want to connect to server A and slurp the contents of a file into a variable. Then on server B, I want to output the contents of the variable into a file.

I'm using Ansible version 1.9.4 on Ubuntu 14. This is the approach that I've tried, but it does not work:

hosts file:
[server_A]
172.19.99.15

[server_B]
172.19.99.16

playbook:
---
- name: part one of playbook
  hosts: server_A
  gather_facts: no

  tasks:
   - name: slurp a file
     slurp: src=/tmp/test.txt
     register: fileContents

   - debug: msg="{{ fileContents.content | b64decode }}"

- name: part two of playbook
  hosts: server_B
  gather_facts: no

  tasks:
   - debug: msg="{{ fileContents.content | b64decode }}"

   - name: write out contents of fileContents to a file
     file: "dest=/tmp/test.txt content={{ fileContents | b64decode }}"

execution:
$ ansible-playbook main.yml -i inventory -u testing -k

PLAY [part one of playbook] ***********************************************

TASK: [slurp a file] **********************************************************
ok: [172.19.99.15]

TASK: [debug msg="{{ fileContents.content | b64decode }}"] ********************
ok: [172.19.99.15] => {
    "msg": "This is a test file to be slurped by ansible.\n"
}

PLAY [part two of playbook] ***********************************************

TASK: [debug msg="{{ fileContents.content | b64decode }}"] ****************************************
fatal: [172.19.99.16] => One or more undefined variables: 'fileContents' is undefined

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/tmp/main.retry

172.19.99.15              : ok=1    changed=0    unreachable=0    failed=0  
172.19.99.16              : ok=0    changed=0    unreachable=1    failed=0  

Brian Coca

unread,
Oct 19, 2015, 3:41:02 PM10/19/15
to Ansible Project
several things wrong there, but im just going to give you some thing
that should 'just work'

- name: part one of playbook
hosts: server_A
gather_facts: no

tasks:
- name: slurp a file
slurp: src=/tmp/test.txt
register: fileContents

- debug: msg="{{ fileContents.content | b64decode }}"

- template: src=file_contents.j2 dest=/tmp/test.txt
delegate_to: server_B

where file_contents.j2:
{{ fileContents.content | b64decode }}


--
Brian Coca

CyberCyber

unread,
Oct 20, 2015, 9:39:33 PM10/20/15
to Ansible Project

Hi Brian,

Can you elaborate on the several things that are wrong with the approach taken above? It seems dynamic variables do not carry over from each play. Is this intentional ?

Your suggestion would work, but won't that require server_B to have access to server_A. What if that is not an option? It's possible to use fetch and store the file locally on the control server, but what if I am trying to avoid doing that? Is there another approach using slurp ?

Thanks.

Brian Coca

unread,
Oct 21, 2015, 7:41:23 PM10/21/15
to Ansible Project
On Tue, Oct 20, 2015 at 9:39 PM, CyberCyber <myn3w...@gmail.com> wrote:
> Can you elaborate on the several things that are wrong with the approach
> taken above? It seems dynamic variables do not carry over from each play. Is
> this intentional ?

play vars are not carry over, but host associated vars do, you just
need to access them through the host they are associated with.


> Your suggestion would work, but won't that require server_B to have access
> to server_A. What if that is not an option? It's possible to use fetch and
> store the file locally on the control server, but what if I am trying to
> avoid doing that? Is there another approach using slurp ?

slurp copies the file to the 'controller' (machine you run ansible
from), so the servers do not need any access to each other.

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages