Google compute engine Ansible中带有连字符的GCE主机名
我将使用Ansible在Google Compute Engine中配置我的系统,该系统有一些手动创建的实例。其中一个实例被命名为测试模块 因此,我把这一行作为一项任务放在我的剧本中Google compute engine Ansible中带有连字符的GCE主机名,google-compute-engine,ansible,Google Compute Engine,Ansible,我将使用Ansible在Google Compute Engine中配置我的系统,该系统有一些手动创建的实例。其中一个实例被命名为测试模块 因此,我把这一行作为一项任务放在我的剧本中 某物:{testing-module.private_ip} Ansible的结果让我觉得 致命:[foo]=>无法在模板字符串中查找名称或访问属性。确保变量名不包含诸如“-”之类的无效字符。 消息告诉我Ansible playbook中的变量名不能包含连字符。问题是我如何引用测试模块的私有ip?当你说{{
某物:{testing-module.private_ip}
Ansible的结果让我觉得
致命:[foo]=>无法在模板字符串中查找名称或访问属性。确保变量名不包含诸如“-”之类的无效字符。
消息告诉我Ansible playbook中的变量名不能包含连字符。问题是我如何引用测试模块的私有ip?当你说
{{foo.private\u ip}
那么foo
是一个Ansible变量的名称,而不是主机的名称。当foo
对应于Ansible具有资源清册的主机名时,将定义“private\u ip”
等参数
如果您希望通过Ansible获取此主机的IP地址,则需要强制Ansible对该主机进行清点,并为此需要针对其运行任务。完成后,您需要使用全局变量hostvars
获取实际信息。所以你会想要这样的东西:
- hosts: testing-module
tasks:
- name: Gather Facts
debug: msg="Getting facts for {{ inventory_hostname_short }}"
- name: Test Facts
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
- hosts: primary-server
tasks:
- name: Test Facts of testing-module
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
编辑:这里有一个关于hostvars
变量的链接。另外,当您第一次开始使用这样的变量时,一个方便的技巧是使用debug
模块简单地显示所有内容:
- name: Display hostvars
debug: var=hostvars
这将输出一个相当大的JSON结构,显示您可以通过
hostvars
变量获得的所有信息。当您说{{foo.private_ip}}
时,foo
是一个可解析变量的名称,而不是主机的名称。当foo
对应于Ansible具有资源清册的主机名时,将定义“private\u ip”
等参数
如果您希望通过Ansible获取此主机的IP地址,则需要强制Ansible对该主机进行清点,并为此需要针对其运行任务。完成后,您需要使用全局变量hostvars
获取实际信息。所以你会想要这样的东西:
- hosts: testing-module
tasks:
- name: Gather Facts
debug: msg="Getting facts for {{ inventory_hostname_short }}"
- name: Test Facts
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
- hosts: primary-server
tasks:
- name: Test Facts of testing-module
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
编辑:这里有一个关于hostvars
变量的链接。另外,当您第一次开始使用这样的变量时,一个方便的技巧是使用debug
模块简单地显示所有内容:
- name: Display hostvars
debug: var=hostvars
这将输出一个相当大的JSON结构,显示您可以通过
hostvars
变量获得的所有信息。当您说{{foo.private_ip}}
时,foo
是一个可解析变量的名称,而不是主机的名称。当foo
对应于Ansible具有资源清册的主机名时,将定义“private\u ip”
等参数
如果您希望通过Ansible获取此主机的IP地址,则需要强制Ansible对该主机进行清点,并为此需要针对其运行任务。完成后,您需要使用全局变量hostvars
获取实际信息。所以你会想要这样的东西:
- hosts: testing-module
tasks:
- name: Gather Facts
debug: msg="Getting facts for {{ inventory_hostname_short }}"
- name: Test Facts
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
- hosts: primary-server
tasks:
- name: Test Facts of testing-module
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
编辑:这里有一个关于hostvars
变量的链接。另外,当您第一次开始使用这样的变量时,一个方便的技巧是使用debug
模块简单地显示所有内容:
- name: Display hostvars
debug: var=hostvars
这将输出一个相当大的JSON结构,显示您可以通过
hostvars
变量获得的所有信息。当您说{{foo.private_ip}}
时,foo
是一个可解析变量的名称,而不是主机的名称。当foo
对应于Ansible具有资源清册的主机名时,将定义“private\u ip”
等参数
如果您希望通过Ansible获取此主机的IP地址,则需要强制Ansible对该主机进行清点,并为此需要针对其运行任务。完成后,您需要使用全局变量hostvars
获取实际信息。所以你会想要这样的东西:
- hosts: testing-module
tasks:
- name: Gather Facts
debug: msg="Getting facts for {{ inventory_hostname_short }}"
- name: Test Facts
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
- hosts: primary-server
tasks:
- name: Test Facts of testing-module
debug msg="IP address is {{ hostvars['testing-module']['private_ip'] }}"
编辑:这里有一个关于hostvars
变量的链接。另外,当您第一次开始使用这样的变量时,一个方便的技巧是使用debug
模块简单地显示所有内容:
- name: Display hostvars
debug: var=hostvars
这将输出一个相当大的JSON结构,显示您可以通过
hostvars
变量获得的所有信息。您能否详细说明一下该测试模块的定义位置?我指的不是实例,而是Ansible变量/对象。我只找到了两种用连字符定义变量的方法。一个是set\u fact
模块,另一个是--通过命令行传递的额外变量。您不能在Ansible中的任何yaml文件中定义带连字符的变量,我相信从版本1.3开始,他们会检查它。如果这是主机名,您可以使用hostvars[“testing module”]
访问它。您能详细说明一下这个测试模块的定义吗?我指的不是实例,而是Ansible变量/对象。我只找到了两种用连字符定义变量的方法。一个是set\u fact
模块,另一个是--通过命令行传递的额外变量。您不能在Ansible中的任何yaml文件中定义带连字符的变量,我相信从版本1.3开始,他们会检查它。如果这是主机名,您可以使用hostvars[“testing module”]
访问它。您能详细说明一下这个测试模块的定义吗?我指的不是实例,而是Ansible变量/对象。我只找到了两种用连字符定义变量的方法。一个是set\u fact
模块