problem passing var between tasks.

33 views
Skip to first unread message

Jeremy Hobbs

unread,
May 21, 2020, 11:38:38 AM5/21/20
to Ansible Project
I have a problem passing a variable between playbooks that are chained together. Any thoughts?

Playbook - audit_hardware_inventory.yml
---
- name: "Compare API to actual"
hosts: "all"
gather_facts: "no"
connection: "local"

tasks:
- name: "INCLUDE TASK: GET JSON WEB TOKEN FROM API"
include_tasks: "generate_api_data.yml"

- name: "INCLUDE TASK: HARDWARE CHECK"
include_tasks: "hardware_check_tasks.yml"

Playbook - generate_api_data.yml
---
- name: "Get JSON DATA"
uri:
method: "POST"
return_content: "yes"
validate_certs: "no"
body_format: "json"
delegate_to: "localhost"
register: "api_data"



The first task pulls the data from the api and registers it as "api_data" however when the second playbook ("hardware_check_tasks.yml") I'm not able to call that variable. How do I pass "api_data" between tasks?

Thanks

David Foley

unread,
May 21, 2020, 11:49:04 AM5/21/20
to Ansible Project
Why two Playbook ? 

But either way, Copy the Reg to a Local Json File

Then in your Second Playbook Just read in the JSON File 

 name: Read Json File
    shell: cat Info.json
    register: json_file


(json_file.stdout | from_json).VALUE

Jeremy Hobbs

unread,
May 21, 2020, 11:55:59 AM5/21/20
to Ansible Project
I simplified the playbook for the post.  But, this specific call is used by many different playbooks.  So instead of having to update each playbook if it changes, I want to abstract it out.  Writing to a file seems a pretty crappy workaround.  Is there not a way to pass variables between the playbooks directly?

David Foley

unread,
May 21, 2020, 12:05:19 PM5/21/20
to Ansible Project
Another way I know is to send the reg to the stdout and cat that stdout into the other playbook.
similar to outputting to the File, but if the API Value is big you may be better off with the Text File.


     register: test
   - debug: msg="{{test.stdout}}"

 


  tasks:
   - shell: cat "{{test.stdout}}"

Suneel Gali

unread,
May 21, 2020, 12:11:42 PM5/21/20
to ansible...@googlegroups.com

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/51b1da39-f24c-454d-8f93-eb332c8c4b23%40googlegroups.com.

Dick Visser

unread,
May 21, 2020, 12:12:22 PM5/21/20
to ansible...@googlegroups.com
Not tried yet, but this describes a dummy host:
https://stackoverflow.com/a/47811099/12731515
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/51b1da39-f24c-454d-8f93-eb332c8c4b23%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Jeremy Hobbs

unread,
May 21, 2020, 2:14:47 PM5/21/20
to Ansible Project
Suneel,  I attempted your solution, but it didn't work with the way I was setting up the workflow.  Dick,  I tried the Dummy host and it worked perfectly.  Thank you for your responses.  


Jeremy


On Thursday, May 21, 2020 at 11:12:22 AM UTC-5, Dick Visser wrote:
Not tried yet, but this describes a dummy host:
https://stackoverflow.com/a/47811099/12731515

On Thu, 21 May 2020 at 17:56, Jeremy Hobbs <gly...@gmail.com> wrote:
>
> I simplified the playbook for the post.  But, this specific call is used by many different playbooks.  So instead of having to update each playbook if it changes, I want to abstract it out.  Writing to a file seems a pretty crappy workaround.  Is there not a way to pass variables between the playbooks directly?
>
>
>
> On Thursday, May 21, 2020 at 10:49:04 AM UTC-5, David Foley wrote:
>>
>> Why two Playbook ?
>>
>> But either way, Copy the Reg to a Local Json File
>>
>> Then in your Second Playbook Just read in the JSON File
>>
>>  name: Read Json File
>>     shell: cat Info.json
>>     register: json_file
>>
>>
>> (json_file.stdout | from_json).VALUE
>
> --
> 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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages