Score:0

Ansible: linked second playbook is not able to run a script for variables passed from multiple hosts from the first playbook

in flag

I am using 2 playbooks linked, there details mentioned below.

playbook1: collect raw data from multiple machines

- hosts: target_1
  gather_facts: false


  tasks:
   - name: Register a new value
     shell: df -h|awk '{gsub("%","|");print $5 $6}'
     register: PLAY1VAR

   - debug: msg="{{PLAY1VAR.stdout}}"

   - name: Register a new value
     shell: hostname
     register: PLAY2VAR

   - debug: msg="{{PLAY2VAR.stdout}}"

   - name: Register dummy host with variable
     add_host:
       name: "DUMMY_HOST"
       PLAY1VAR_NEW: " {{ PLAY1VAR.stdout }}"

   - name: Register dummy host with variable
     add_host:
       name: "DUMMY_HOST_1"
       PLAY2VAR_NEW: " {{ PLAY2VAR.stdout }}"
- hosts: target_2
  gather_facts: false


  tasks:
   - name: Register a new value

playbook2:parse data using python script

- hosts: localhost
  gather_facts: false

  tasks:
   - name: Echo the output - PLAY1 variable vaule
     command: python3 /opt/diskspace_watcher/watcher.py '{{hostvars['DUMMY_HOST']['PLAY1VAR_NEW']}}' '{{hostvars['DUMMY_HOST_1']['PLAY2VAR_NEW']}}'
     register: PLAY2_RESULTS

   - debug: msg="{{PLAY2_RESULTS.stdout}}"

I am passing couple of variables from the first playbook to another, when I am running this for just one host it is working as expected but when trying to run for multiple host it is unable to pass the arguments for all the host mentioned in the inventory file and is just picking up one variable for a particular host.

I am looking for a way to get this executed. Any help/idea is appreciated!!

in flag
Please add a reproducible example.
U880D avatar
ca flag
Can you add the details you mentioned by editing your question?
Rishabh Shukla avatar
in flag
@U880D, I have added the playbooks config
Rishabh Shukla avatar
in flag
@GeraldSchneider, added
in flag
I don't see anything in the first playbook that would run the second.
Rishabh Shukla avatar
in flag
@GeraldSchneider I am running both through a third playbook i.e vi global.yaml - import_playbook: playbook1.yaml - import_playbook: playbook2.yaml
Zeitounator avatar
fr flag
If I correctly understand the way you are trying to use your `dummy_host` to register a variable between plays, only the last registered values for the last host in your first play's host loop will be retained. You should edit you question and give a wider overview of what your are exactly trying to solve here because I'm almost sure you're stuck in an [x/y problem](https://xyproblem.info).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.