how to use dynamic variable set in one play as an input to a task in another play

132 views
Skip to first unread message

Kathy Allen

unread,
Aug 5, 2015, 5:24:39 PM8/5/15
to Ansible Project
Hiya.

In my playbook, I have two plays: the first is to be run on the control host, the second is run on a set of remote app hosts. What I want to do is have a variable determined by a task in the first play be used as a shell script argument for a task in the second play. I don't know how to do this. Can anyone help? Is this possible?

Here's a simplification of how these are called in my ansible deployment orchestration playbook. The json determined by a tool on the control host in common/test_prep.yml needs to be used as the input parameter to a tool which is called inside of common/test_client_check.yml on each client 'myhosts'.


$ cat testplaybook.yml
---
- hosts: 127.0.0.1
  connection
: local
  gather_facts
: False
  tasks
:

- include: "{{ playbook_dir }}/pb_test.yml myhosts=app_set1"

$ cat pb_test
.yml
---
- include: common/test_prep.yml
- include: common/test_client_check.yml

$ cat common
/test_prep.yml
---
- hosts: 127.0.0.1
  connection
: local
  gather_facts
: false
  tasks
:

 
- name: Check authoritative DNS for app datasources
    local_action
: shell ../bin/dns_check
    ignore_errors
: true
   
register: check_results

 
- debug: var=check_results.stdout_lines[0]
 
- set_fact: json_as_input_for_next_play={{ check_results.stdout_lines[0] }}
 
- debug: var=json_as_input_for_next_play

$ cat common
/test_client_check.yml
---
- hosts: '{{ myhosts }}'
  gather_facts
: false
  tasks
:
   
# this ruby script returns a Boolean
 
- name: Check DNS for app datasources on the clients
    shell
: /usr/local/bin/client-side-lookup json_as_input_for_next_play
    sudo
: yes

# When running it by hand, the json needs to be surrounded by single quotes:
#client#  /usr/local/bin/client-side-lookup '{"db1.example.com":"10.4.4.113","db2.example.com":"10.4.4.114"}'



How do I make this happen? Thank you!

kallen


Brian Coca

unread,
Aug 5, 2015, 5:47:16 PM8/5/15
to Ansible Project
the data you want is in:

hostvars['localhost']['json_as_input_for_next_play']
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/26e5de97-6947-433f-9a5c-1c34784ff39f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Kathy Allen

unread,
Aug 5, 2015, 6:34:20 PM8/5/15
to Ansible Project
Yep. Thank you! A coworker pointed out a similar post: https://groups.google.com/forum/#!searchin/ansible-project/felix/ansible-project/C51U547IXwY/2IyoNqCZpTQJ. Sorry I didn't catch that one before I posted my question.

I feared that hostvars dict created in localhost task might not be available to the remote host. But that's not the case, it is available and it worked.

kallen
Reply all
Reply to author
Forward
0 new messages