如何在ansible条件检查中使用字符串连接?

时间:2016-09-29 20:12:06

标签: ansible

我正在尝试验证DNS记录是否与ansible库存匹配,但是我收到的错误并不能很好地解释预期的语法。

- debug: msg="Update DNS for {{ inventory_hostname }}"
  when: "ec2_ip_address != lookup('ip', inventory_hostname + '.example.com')"

这段代码返回此错误:

fatal: [hostname]: FAILED! => {"failed": true, "msg": "The conditional check 'ec2_ip_address != lookup('ip', inventory_hostname + '.example.com')' failed. The error was: [Errno 8] nodename nor servname provided, or not known

The error appears to have been in 'some-playbook.yml': line 18, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


    - debug: msg=\"Update DNS for {{ inventory_hostname }}\"
      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - \"{{ foo }}\"
"}

1 个答案:

答案 0 :(得分:0)

这个简化的例子适用于ansible 2.1:

- hosts: localhost
  tasks:
  - set_fact:
      ec2_ip_address: "www.example.com"
  - debug: msg="Update DNS for {{ inventory_hostname }}"
    when: ec2_ip_address != lookup('dig', inventory_hostname + '.example.com')

我正在使用默认的dig dns查找插件