“什么时候"Ansible playbook 上的条件使用运算符无法按预期工作 [英] "When" condition on Ansible playbook doesn't work as expected using operators

查看:24
本文介绍了“什么时候"Ansible playbook 上的条件使用运算符无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below playbook use conditional statement with operators in Ansible. When I run the playbook, it never takes/validates the condition instead it consider the last set_fact value for "shmall".

---
- 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

推荐答案

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

Fix your indentation. when is not an argument of a set_fact action, but of a task:

- 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

这篇关于“什么时候"Ansible playbook 上的条件使用运算符无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆