在Ansible Playbook中使用主机变量

时间:2019-06-12 12:54:01

标签: ansible ansible-inventory

我正在尝试结合“ with_items”来引用剧本中的主机变量。

我的库存

[container]
testcontainer-01.example.org template_name="syslog" ipv4="192.168.1.101"
testcontainer-02.example.org template_name="syslog" ipv4="192.168.1.102"

剧本:

  tasks:
    - debug:
        var: "{{ item.ipv4 }}"
      with_items:
        - "{{ groups['container'] }}"

每当我播放剧本时,都会出现以下错误:

The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'ipv4'

当我只请求{{ item }}的调试时,没有ipv4属性,它只是说未定义变量。

"testcontainer-01.example.org ": "VARIABLE IS NOT DEFINED!: Unable to look up a name or access an attribute in template string ({{testcontainer-01.example.org}}).\nMake sure your variable name does not contain invalid characters like '-': unsupported operand type(s) for -: 'StrictUndefined' and 'StrictUndefined'"

2 个答案:

答案 0 :(得分:0)

如果要使用主机运行 ipv4

- debug:
    var: ipv4

如果要在容器中列出所有 ipv4 ,请使用

- debug:
    msg: "{{ hostvars[item].ipv4 }}"
  loop: "{{ groups['container'] }}"

(未经测试)

答案 1 :(得分:-1)

正如弗拉基米尔·博特卡(Vladimer Botka)发表的,以下作品。

net.corda.testing.node.internal.CustomCordapp

由于某种原因,Ansible仍在咆哮着说我的变量未定义。不管怎样它会产生我需要的结果。 ipv4变量的值已在正确的位置使用。

  // we collect the distinct set of paths in the event that we don't add the same cordapp twice
  private val cordapps = listOf(
    TemplateContract::class,
    Initiator::class
// and other key cordapp classes
  ).map { it.packageName }.distinct().map { TestCordapp.findCordapp(it) }

// here we declare a custom cordapp based the entry-point classes in a given test package
  private val customTestCordapp = CustomCordapp(packages = setOf(TestCordaService::class.packageName),
    classes = setOf(TestCordaService::class.java))

  private val network = MockNetwork(MockNetworkParameters(cordappsForAllNodes = cordapps + customTestCordapp))
  // ...