Ansible"当变量== true"时表现不尽如人意

Mit*_*tch 19 ansible ansible-playbook

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

  - 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/
Run Code Online (Sandbox Code Playgroud)

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

ansible-playbook install.yml -i $invFile --extra-vars="hosts=webservers force_install=True"
Run Code Online (Sandbox Code Playgroud)

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

 skipping: [server1] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}
Run Code Online (Sandbox Code Playgroud)

我的印象是条件检查应该通过所有这些作为force_install == true评估,以true使整个when评估为真(因为它是一系列'OR').

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


编辑:

将包含的第二个更改为Install NRPE以下作品,但不解释为什么另一个正常Run the prep运行:

工作:

when: (not nrpe_installed.stat.exists or force_install or force_nrpe_install)
Run Code Online (Sandbox Code Playgroud)

还在工作:

when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true)
Run Code Online (Sandbox Code Playgroud)

不工作:

when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true)
Run Code Online (Sandbox Code Playgroud)

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

TASK [debug] *******************************************************************
ok: [server2] => {
    "nrpe_installed.stat.exists": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
    "plugins_installed.stat.exists": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
    "force_install": true
}

TASK [Run the prep] ************************************************************
included: /tasks/nrpe-install/prep.yml for server2, server3, server4, server5, server6, server7

TASK [Prep and configure for installation | Install yum packages] **************
ok: [server6] => (item=[u'gcc', u'glibc', u'glibc-common', u'gd', u'gd-devel', u'make', u'net-snmp', u'openssl-devel', u'unzip', u'tar', u'gzip', u'xinetd']) => {"changed": false, "item": ["gcc", "glibc", "glibc-common", "gd", "gd-devel", "make", "net-snmp", "openssl-devel", "unzip", "tar", "gzip", "xinetd"], "msg": "", "rc": 0, "results": ["gcc-4.1.2-55.el5.x86_64 providing gcc is already installed", "glibc-2.5-123.el5_11.3.i686 providing glibc is already installed", "glibc-common-2.5-123.el5_11.3.x86_64 providing glibc-common is already installed", "gd-2.0.33-9.4.el5_4.2.x86_64 providing gd is already installed", "gd-devel-2.0.33-9.4.el5_4.2.i386 providing gd-devel is already installed", "make-3.81-3.el5.x86_64 providing make is already installed", "net-snmp-5.3.2.2-20.el5.x86_64 providing net-snmp is already installed", "openssl-devel-0.9.8e-40.el5_11.x86_64 providing openssl-devel is already installed", "unzip-5.52-3.el5.x86_64 providing unzip is already installed", "tar-1.15.1-32.el5_8.x86_64 providing tar is already installed", "gzip-1.3.5-13.el5.centos.x86_64 providing gzip is already installed", "xinetd-2.3.14-20.el5_10.x86_64 providing xinetd is already installed"]}

TASK [Prep and configure for installation | Make nagios group] *****************
ok: [server2] => {"changed": false, "gid": 20002, "name": "nagios", "state": "present", "system": false}

TASK [Prep and configure for installation | Make nagios user] ******************
ok: [server6] => {"append": false, "changed": false, "comment": "User for Nagios NRPE", "group": 20002, "home": "/home/nagios", "move_home": false, "name": "nagios", "shell": "/bin/bash", "state": "present", "uid": 20002}

TASK [debug] *******************************************************************
ok: [server2] => {
    "nrpe_installed.stat.exists": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
    "force_install": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
    "force_nrpe_install": false
}

TASK [Install NRPE] ************************************************************
skipping: [server2] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}
Run Code Online (Sandbox Code Playgroud)

udo*_*dan 53

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

force_install|bool == true
Run Code Online (Sandbox Code Playgroud)

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

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

您可能还想在force_install未定义的情况下设置默认值,因为它会导致未定义的变量错误:

force_install|default(false)|bool == true
Run Code Online (Sandbox Code Playgroud)

  • 是不是'== true`多余? (7认同)
  • 对,你当然可以省略.`force_install |默认(假)| bool` (7认同)
  • "true"和"yes"评价为true的原因是因为[YAML定义它们的方式](http://yaml.org/type/bool.html).它发生在从YAML到Python的翻译中. (3认同)
  • 因为`None`,`False`,`0`和````(空字符串)是假值而非空字符串是真值,你通常可以省略`bool`转换并且只说例如`force_install |默认(假)'. (2认同)
  • @SimonWoodside 这比这更令人困惑。Ansible 在“set_fact”(也不是“y”)期间不会自动将“on”/“off”自动转换为“True”/“False”。另一方面,它的“bool”过滤器查找“yes”、“on”、“1”、“true”、1”中的任何一个。请参阅 [`set_fact`](https://github.com/ansible/ansible/blob/6a7e438012e6e1dc911c8fcf06cd751f74c783c9/lib/ansible/plugins/action/set_fact.py#L54-L55) 和 [`bool`](https:// github.com/ansible/ansible/blob/devel/lib/ansible/plugins/filter/core.py#L95-L99)。这是非常不一致的。 (2认同)

KCD*_*KCD 7

是的……当然需要测试。我建议

  • | bool 除非您期望未定义的变量,否则很好
  • var is defined and (var | bool) 如果你期望未定义的变量

default(False)只要输入不是从 YAML 解析的,就可以,并且您很高兴这些都是真的。即通过CLI 参数定义的变量是字符串。--extra_vars

 "false"
 "null"
 "defined string" 
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助:

#!/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
Run Code Online (Sandbox Code Playgroud)

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