如何使用带有“何时”的 set_fact 模块来使用以下条件?我需要根据 RHEL 服务器中的总内存设置“shmall”值。 我需要知道运算符(operator)在 Ansible 中是如何工作的。
- name: Total Available Memory
shell: cat /proc/meminfo | grep MemTotal | awk '{print $2}'
register: MemTotal
# for RHEL7,SHMALL Setting should be (PHYSICAL MEMORY – MEMORY FOR SYSTEM) / PAGE SIZE with 4096 pagesize.
- name: SHMALL value to set for memory size less than 16G
set_fact:
shmall: 3670016
when: (MemTotal le "16777216") | int
- name: SHMALL value to set for memory size between 16G and 32G
set_fact:
shmall: 7340032
when: (MemTotal gt "16777216" and MemTotal le "33554432") | int
- name: SHMALL value to set for memory size between 32G and 64G
set_fact:
shmall: 14680064
when: (MemTotal gt "33554432" and MemTotal le "6710886") | int
- name: SHMALL value to set for memory size between 64G and 256G
set_fact:
shmall: 57671680
when: (MemTotal gt "67108864" and MemTotal le "268435456") | int
最佳答案
你可以算出你的小值:
---
- hosts: localhost
vars:
mem_gb: "{{ ansible_memtotal_mb/1024 }}"
mem_pow2: "{{ 2 | pow(((mem_gb|int|log)/(2|log)) | round(0,'ceil')) }}"
shmall_calculated: "{{ ((mem_pow2|int)*0.875*1024*1024*1024/4096) | int }}"
tasks:
- debug:
msg: "{{ shmall_calculated }}"
关于linux - 运算符(operator)如何使用 "set_fact"和 "when"在 ansible 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43240904/