Python 2.7 错误为:计算条件时出错

Python 2.7 错误为:计算条件时出错,python-2.7,ansible,ansible-playbook,Python 2.7,Ansible,Ansible Playbook,我尝试使用Ansible为我拥有的每台服务器插入一个唯一的编号 我希望以下是: 192.168.60.6的myid.txt中的“1” - 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

我尝试使用Ansible为我拥有的每台服务器插入一个唯一的编号

我希望以下是:

192.168.60.6的myid.txt中的“1”

- 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 }}"
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] }}"

顺便说一句:{{ansible\u all\u ipv4\u addresses[1]}是192.168.60.6

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

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

在Ansible with the when子句中很容易做到这一点,它包含一个没有双花括号的原始Jinja2表达式


原始表达式在
when:
语句中使用,带有
assert
模块和
debug:var=varname
但是
debug:msg=“{{{varname}}”
)。

谢谢Konstantin。谢谢你,伙计,但是现在我很困惑在哪种情况下应该使用裸变量,在哪种情况下应该使用变量?”{{},你能给我一些建议吗?