Ansible playbook上的“当”条件使用运算符时无法正常工作

问题描述 投票:0回答:1

在playbook中使用带有运算符的条件语句。当我运行playbook时,它从不接受/验证条件,而是考虑“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个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.