简体   繁体   中英

ansible variable across hosts in one playbook

is it possible to store value in variable and use across multiple hosts. I found that it should work with set_fact so I tried it like this. But it's not working.

- hosts: db
     remote_user: root
     tasks:

     - name: set fact
       set_fact:
         zone_id: "app"
         subnet_id: 28
        when:  host_ip.find("10.1.30") != -1

        ....
 - hosts: ns01
     remote_user: root
     tasks:

    - name:
      debug:
        var: "{{ hostvars['db']['zone_id'] }}"

    ....

But i get following error "The task includes an option with an undefined variable. The error was: "hostvars['db']" is undefined".

Thanks

set_fact is used to extract information from a host and save it for later. It's usefor for information that changes over time.

It will save the information as a regular variable, so you will be able to access it as {{ zone_id }} .

Host vars are used to store 'static' information about each host that is likely different between hosts, so if this zone does not change, put it in the host vars. If it is something you need to check at runtime, use set_fact

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