linux - 运算符(operator)如何使用 "set_fact"和 "when"在 ansible 中运行

标签 linux unix automation ansible redhat

如何使用带有“何时”的 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/

相关文章:

java - Cucumber 在 @before hook 期间失败 : NoClassDefFoundError

automation - Cypress - 在 iframe 中运行测试

java - 如何使用 Java 执行多个 UNIX 命令并捕获单个输出或错误(如果有)

linux - 为什么 niceness 值与进程优先级成反比?

macos - 如何使用终端执行.command 文件?

c - 直接 io - 写入文件时 O_DIRECT 似乎不起作用

android - Appium AndroidDriver 的 DesiredCapabilities

linux - bash 查找至少包含一个数组元素的行

regex - 计算包含模式的行数

node.js - docker 里面的僵尸