Quantcast
Channel: polygun2000的博客
Viewing all articles
Browse latest Browse all 55

ansible的when条件不允许jinja2模版变量

$
0
0
今天写一个playbook的时候,使用when条件,ansible报了如下错误

[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or
{% %}. Found: {{ item.value.ip }} == {{ inventory_hostname}}

反复尝试几种写法都不行,于是google了一下,找到参考文档这么一个有价值的回答。

其实就是使用 单引号包裹,里边再用双引号。

示例playbook如下:
---
- name: test yaml
  hosts: all
  vars:
    hostinfo:
      server1: {ip: 10.18.105.8,user_id: "it's current server!!"}
      server2:
        ip: 10.18.105.2
        user_id: 1b03wre
      server3:
        ip: 10.18.105.3
        user_id: 1ew111e
      server4:
        ip: 10.18.105.4
        user_id: 1b04311
      server5:
        ip: 10.18.105.5
        user_id: 1122232

  tasks:
    - name: show all variables
      debug:
        msg: Server {{item.key}} IP is {{ item.value.ip }} and ID is {{ item.value.user_id }}
      with_dict: "{{ hostinfo }}"
      when: '"{{ item.value.ip }}" == "{{ inventory_hostname}}"'       《----- 就是这里,单引号包裹,里边双引号
      register: aaa

    - name: print name
      debug: msg="{{ aaa }}"

参考文档:
http://oki2a24.com/2017/05/03/fix-when-statements-should-not-include-jinja2-templating-delimiters-in-ansible-2-3/

 

Viewing all articles
Browse latest Browse all 55

Trending Articles