简体   繁体   中英

how to reuse variables across different playbook/tasks using set_stats in Ansible

I am trying to reuse the test_stat variable in testing_todelete_stat.yml . I have defined this variable as set_stats in the file testing_todelete_task.yml but I am getting the following error message when running debug task.

what am I doing wrong?

Output:

FAILED! => {"msg": "The task includes an option with an undefined variable. 
 The error was: 'test_stat' is undefined\n\nThe error appears to be in ****/testing_todelete_stat.yml': line 7, column 7
PLAY RECAP *****************************************************************************************************************************************************************localhost                  : ok=5    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

testing_todelete_task.yml

- name: check google
 uri:
   url: http://www.google.com
   return_content: yes
 register: this

- set_fact:  
   this_local: "{{ this.url }}"   

- set_stats:
   data:
     test_stat: "{{ this_local }}"  

testing_todelete_stat.yml

- name: PLaybook to check set_stats
  hosts: localhost
  gather_facts: false
  tasks: 
    - name: include task
      include_tasks: testing_todelete_task.yml
    - name: content of set_stats
      debug:
        msg: "{{ test_stat }}"    

The answer by @ Krishna Reddy works for me: hostvars['localhost']['this_local']

To add more information about set_stats , you can get the value of those variables directly using workflow in Ansible Tower/AWX. This information is taken from one playbook to another through what are known as Artifacts .

Modifying the initial example it would be as follows using workflow:

Playbook1:

    - name: PLaybook to generate set_stats
      hosts: localhost
      gather_facts: true
      tasks:
        - name: check google
          uri:
            url: http://www.google.com
            return_content: yes
          register: this

        - set_fact:  
            this_local: "{{ this.url }}"   

        - set_stats:
            data:
              test_stat: "{{ this_local }}"

        - name: debug set fact
          debug:
            msg: "{{ this.url}}"

Playbook2:

    - name: PLaybook to check set_stats
      hosts: localhost
      gather_facts: true
      tasks:   
        - name: content of set_stats
          debug:
            msg: "{{ test_stat }}"  

Result:

PLAY [PLaybook to check set_stats] *********************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [content of set_stats] ****************************************************
ok: [localhost] => {
    "msg": "http://www.google.com"
}
PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM