【问题标题】:"When" condition on Ansible playbook doesn't work as expected using operatorsAnsible playbook 上的“何时”条件无法使用运算符按预期工作
【发布时间】:2017-09-01 23:03:44
【问题描述】:

下面的剧本使用条件语句和 Ansible 中的运算符。当我运行剧本时,它从不接受/验证条件,而是考虑“shmall”的最后一个 set_fact 值。

---
- hosts: sandbox
  user: robo
  become: yes
  gather_facts: yes
  tasks:
  - debug: msg="{{ansible_memtotal_mb}}"

  - name: SHMALL value for MEM less than 16G
    set_fact:
       shmall: 3670016
       when: ansible_memtotal_mb|int <= 16384

  - name: SHMALL value for MEM is between 16G and 32G
    set_fact:
       shmall: 7340032
       when: ansible_memtotal_mb|int > 16384 and ansible_memtotal_mb|int <= 32768

  - debug: var=shmall

================================================================================
SUDO password:

PLAY [sandbox] *****************************************************************

TASK [setup] *******************************************************************
ok: [uslv-sapp-lnx11]

TASK [debug] *******************************************************************
ok: [uslv-sapp-lnx11] => {
    "msg": 7872
}

TASK [SHMALL value for MEM less than 16G] **************************************
ok: [uslv-sapp-lnx11]

TASK [SHMALL value for MEM is between 16G and 32G] *****************************
ok: [uslv-sapp-lnx11]

TASK [debug] *******************************************************************
ok: [uslv-sapp-lnx11] => {
    "shmall": 7340032
}

PLAY RECAP *********************************************************************
uslv-sapp-lnx11            : ok=5    changed=0    unreachable=0    failed=0

【问题讨论】:

    标签: linux unix ansible devops


    【解决方案1】:

    修正缩进。 when 不是 set_fact 动作的参数,而是任务的参数:

    - name: SHMALL value for MEM less than 16G
      set_fact:
        shmall: 3670016
      when: ansible_memtotal_mb|int <= 16384
    
    - name: SHMALL value for MEM is between 16G and 32G
      set_fact:
        shmall: 7340032
      when: ansible_memtotal_mb|int > 16384 and ansible_memtotal_mb|int <= 32768
    

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 2020-01-13
      • 2012-04-29
      • 1970-01-01
      • 2020-12-24
      • 2011-06-14
      相关资源
      最近更新 更多