错误是:评估条件

时间:2016-10-21 11:26:27

标签: python-2.7 ansible ansible-playbook

我尝试使用Ansible在每台服务器上插入一个唯一的编号。

如下所示,我希望这些是:

192.168.60.6的myid.txt

中的

“1” 192.168.60.7的myid.txt

中的

“2”

- hosts: all
  gather_facts:True
  vars:
    servers:
      host1:
        ip: 192.168.60.6
        num: 1
      host2:
        ip: 192.168.60.7
        num: 2
      host3:
        ip: 192.168.60.8
        num: 3

  tasks:
    -name: write a unique number in myid.txt 
     lineinfile:
       dest: "/home/user/myid.txt"
       line: "{{ item.value.num }}"
     when: "{{ item.value.ip }} == {{ ansible_all_ipv4_addresses[1] }}"
     `enter code here`with_dict: "{{ servers }}"

不幸的是,我收到了这个错误:

TASK [write unique number in myid] 

*********************************************
fatal: [192.168.60.6]: FAILED! => {"failed": true, "msg": "The conditional check '{{item.value.ip}} == {{ ansible_all_ipv4_addresses[1] }}' failed.   
The error was: error while evaluating conditional ({{item.value.ip}} == {{ ansible_all_ipv4_addresses[1] }}): float object has no element 60

The error appears to have been in '/home/shihhao/Desktop/BlueTech/ansible/kafka_playbook.yml': line 101, column 7,   
but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:
    - name: write unique number in myid
      ^ here\n"}

似乎一旦我添加了这一行,我就会收到错误。

 when: "{{item.value.ip}} == {{ ansible_all_ipv4_addresses[1] }}"

BTW:{{ansible_all_ipv4_addresses [1]}}是192.168.60.6

1 个答案:

答案 0 :(得分:0)

您应该在when:语句中使用裸变量:

when: item.value.ip == ansible_all_ipv4_addresses[1]

来自documentation

  

在Ansible中使用when子句很容易,它包含一个没有双花括号的原始Jinja2表达式

原始表达式用于when:语句,assert模块和debug: var=varname debug: msg="{{varname}}")。