简体   繁体   English

ansible 变量在一个剧本中跨主机

[英]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.我发现它应该与 set_fact 一起使用,所以我这样尝试。 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".但我收到以下错误“该任务包含一个带有未定义变量的选项。错误是:“hostvars ['db']”未定义”。

Thanks谢谢

set_fact is used to extract information from a host and save it for later. set_fact用于从主机中提取信息并保存以供以后使用。 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 }} .它将信息保存为常规变量,因此您将能够以{{ 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如果您需要在运行时检查它,请使用 set_fact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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