Ansible"当变量== true"不按预期行事

时间:2016-06-17 19:09:17

标签: ansible ansible-playbook

我在编写的剧本中有以下任务(在<>中的调试语句旁边列出了结果):

var estore = Substitute.For<IEntityStore>();
var dataAccessTemplate = Substitute.For<IDataAccessTemplate>();
dataAccessTemplate.InvokeAsync(context => Task.FromResult(Arg.Any<DefaultActionPlan>)
    .ReturnsForAnyArgs(jj);

var pdp = new PlanDataProvider(dataAccessTemplate, estore);

我正在使用以下命令运行它:

  - debug: var=nrpe_installed.stat.exists <true>
  - debug: var=force_install <true>
  - debug: var=plugins_installed.stat.exists <true>

  - name: Run the prep 
    include: prep.yml
    when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true)
    tags: ['prep']

  - debug: var=nrpe_installed.stat.exists <true>
  - debug: var=force_install <true>
  - debug: var=force_nrpe_install <false>

  - name: Install NRPE
    include: install-nrpe.yml
    when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true)
    tags: ['install_nrpe']
    vars:
      nrpe_url: 'http://url.goes.here'
      nrpe_md5: 3921ddc598312983f604541784b35a50
      nrpe_version: 2.15
      nrpe_artifact: nrpe-{{ nrpe_version }}.tar.gz
      nagios_ip: {{ nagios_ip }}
      config_dir: /home/ansible/config/

第一个包含运行,但第二个包含此输出:

ansible-playbook install.yml -i $invFile --extra-vars="hosts=webservers force_install=True"

我认为条件检查应该通过所有条件,因为 skipping: [server1] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} 评估为force_install == true,这应该使整个true评估为真(因为它& #39;一系列&#39; OR&#39;

如何在适当设置变量时获取运行时间?

编辑:

更改when时的第二个内容包括以下作品,但无法解释为什么另一个Install NRPE正常运行:

工作:

Run the prep

还在工作:

when: (not nrpe_installed.stat.exists or force_install or force_nrpe_install)

不工作:

when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true)

该剧的特定部分的截断(重复删除)输出是:

when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true)

2 个答案:

答案 0 :(得分:48)

您需要将变量转换为布尔值:

force_install|bool == true

我并不是说我理解它背后的逻辑。在python中,任何非空字符串都应该是真实的。但是当直接在条件中使用时,它的计算结果为false。

然后bool过滤器再次将字符串“true”和“yes”(不区分大小写)解释为true。任何其他字符串都是false。

如果未定义force_install,您可能还想设置默认值,因为它会导致未定义变量错误:

force_install|default(false)|bool == true

答案 1 :(得分:2)

是的...当然需要测试。我推荐

    除非您期望使用未定义的变量,否则
  • | bool会很好
  • var is defined and (var | bool)(如果您希望使用未定义的变量)
只要输入是从YAML解析的 not

default(False)就可以了,您很高兴这些都是 true 。即通过--extra_vars CLI参数定义的变量是字符串。

 "false"
 "null"
 "defined string" 

希望这会有所帮助:

#!/usr/bin/env ansible-playbook
---
- name: Test truthiness
  hosts: localhost
  gather_facts: False
  vars:
    truthy_vars:
    # TRUE
    - True
    - 1
    - "true"
    # FALSE
    - "false"
    - null
    - False
    - 0
    # ERROR (invalid sytax error stops the loop of the first of these)
    - "null"
    - "defined string"
    # ERROR
    # truthy_var_undefined
    # FALSE
    truthy_var_defined:
  tasks:
  - name: Test truthy
    debug:
      msg: "is truthy"
    ignore_errors: True # beware, the loo
    when: item
    loop: "{{ truthy_vars }}"
    loop_control:
      label: Test {{ item }}
  - name: truthy_var_undefined
    debug:
    when: truthy_var_undefined
    ignore_errors: true
  - name: truthy_var_defined
    debug:
    when: truthy_var_defined

- name: Test | default(False)
  hosts: localhost
  gather_facts: False
  vars:
    default_pipe_vars:
    # TRUE
    - True
    - 1
    - "true"
    # beware these:
    - "false"
    - "null"
    - "defined string" 
    # FALSE
    - null
    - False
    - 0
    # FALSE
    # default_pipe_undefined
    # FALSE
    default_pipe_defined:
  tasks:
  - name: Test | default(False)
    debug:
      msg: "is not | default(False)"
    when: item | default(False)
    loop: "{{ default_pipe_vars }}"
    loop_control:
      label: Test {{ item }}
  - name: default_pipe_undefined | default(False)
    debug:
    when: default_pipe_undefined | default(False)
  - name: default_pipe_defined | default(False)
    debug:
    when: default_pipe_defined | default(False)

- name: Test | bool
  hosts: localhost
  gather_facts: False
  vars:
    bool_vars:
    # TRUE
    - True
    - 1
    - "true"
    # FALSE
    - "defined string"
    - "null"
    - "false"
    - null
    - False
    - 0
    # ERROR
    # bool_undefined
    # FALSE
    bool_defined:
  tasks:
  - name: Test bool parsing
    debug:
      msg: "parsed as true booleans"
    when: item | bool
    loop: "{{ bool_vars }}"
    loop_control:
      label: Test {{ item }}
  - name: bool_undefined | bool
    debug:
    when: bool_undefined | bool
    ignore_errors: true
  - name: bool_defined var | bool
    debug:
    when: bool_defined | bool


- name: Test is defined and | bool
  hosts: localhost
  gather_facts: False
  vars:
    defined_bool_vars:
    # TRUE
    - True
    - 1
    - "true"
    # FALSE
    - "defined string"
    - "null"
    - "false"
    - null
    - False
    - 0
    # FALSE
    # defined_bool_undefined
    # FALSE
    defined_bool_defined:
  tasks:
  - name: Test bool parsing
    debug:
      msg: "parsed as true booleans"
    when:
    - item is defined
    - item | bool
    loop: "{{ defined_bool_vars }}"
    loop_control:
      label: Test {{ item }}
  - name: defined_bool_undefined | bool
    debug:
    when:
    - defined_bool_undefined is defined
    - defined_bool_undefined | bool
    ignore_errors: true
  - name: defined_bool_defined var | bool
    debug:
    when:
    - defined_bool_defined is defined
    - defined_bool_defined | bool

https://gist.github.com/kcd83/4ea23d201c271b58f1c4ef7844408657