【问题标题】:How can an operator funtion in ansible with "set_fact" and "when"运算符如何使用“set_fact”和“when”在ansible中发挥作用
【发布时间】:2017-04-05 20:20:24
【问题描述】:

如何使用带有“何时”的 set_fact 模块来使用以下条件?我需要根据 RHEL 服务器中的总内存设置“shmall”值。 我需要知道操作员在 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

【问题讨论】:

  • - name: SHMALL value to set for memory size less than 16G set_fact: shmall: 3670016 when: ansible_memtotal_mb 16384 和 ansible_memtotal_mb 32768 和 ansible_memtotal_mb
  • - name: oracle-recommended sysctl sysctl: name: "{{ item.name }}" value: "{{ item.value }}" sysctl_file: /etc/sysctl.d/99- sysctl.conf ignoreerrors: yes # 忽略未知键的错误 with_items: - { name: "kernel.shmall", value: "{{ shmall }}" }

标签: linux unix automation ansible redhat


【解决方案1】:

你可以计算你的小价值:

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

【讨论】:

    【解决方案2】:

    首先,您不需要从 shell 获取 MemTotal,您可以从 ansible facts 获取此信息。在这种情况下,事实称为ansible_memtotal_mb。 ansible 中的运算符的工作方式与 python 运算符非常相似,这是我的笔记本的示例剧本,总内存为 3834。

    playbook.yml

    - hosts: localhost
      tasks:
        - debug: msg="{{ansible_memtotal_mb}}"
    
        - debug: msg="It's over 9000!!"
          when: ansible_memtotal_mb > 9000
    
        - debug: msg="It's too weak, lesser than 9000"
          when: ansible_memtotal_mb < 9000
    
        - debug: msg="It's between 3000 and 4000"
          when: ansible_memtotal_mb > 3000 and ansible_memtotal_mb < 4000
    

    输出

    ansible-playbook -i localhost, playbook.yml
    
    PLAY [localhost] ***************************************************************
    
    TASK [setup] *******************************************************************
    ok: [localhost]
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "msg": 3834
    }
    
    TASK [debug] *******************************************************************
    skipping: [localhost]
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "msg": "It's too weak, lesser than 9000"
    }
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "msg": "It's between 3000 and 4000"
    }
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=4    changed=0    unreachable=0    failed=0 
    

    编辑:添加的任务,如问题所问。您不需要将 memtotal 转换为 KB,将约束转换为 MB 会更容易

    - name: SHMALL value to set for memory size less than 16G
      set_fact:
          shmall: 3670016
          when: ansible_memtotal_mb < 16384
    
    - name: SHMALL value to set for memory size between 16G and 32G
      set_fact:
          shmall: 7340032
          when: ansible_memtotal_mb > 16384 and ansible_memtotal_mb < 32768
    
    - name: SHMALL value to set for memory size between 32G and 64G
      set_fact:
          shmall: 14680064
          when: ansible_memtotal_mb > 32768 and ansible_memtotal_mb < 65536
    
    - name: SHMALL value to set for memory size between 64G and 256G
      set_fact:
          shmall: 57671680
          when: ansible_memtotal_mb > 65536 and ansible_memtotal_mb < 262144
    

    【讨论】:

    • 我的剧本这一部分的目的是在 sysctl.conf 文件中设置与服务器上的物理内存相关的 SHMALL 值。为此,我需要以 kb 为单位的内存。
    • - 名称:在 KB shell 中生成内存值:cat /proc/meminfo | grep 内存总计 | awk '{print $2}' register: MemTotal - debug: msg="{{MemTotal}}" - name: SHMALL value to set for memory size less than 16G set_fact: #shmall: "{{ (((1000000*ansible_memtotal_mb) /4096)*0.4) | int }}" shmall: 3670016 when: MemTotal 16777216 and MemTotal
    • 我对剧本这部分的要求是; - 检查 Redhat 服务器的总内存并以 kb 为单位计算 - 当:MemTotal 16777216 且 MemTotal 33554432 and MemTotal
    猜你喜欢
    • 2022-11-14
    • 2022-11-23
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2016-03-29
    • 2020-07-14
    相关资源
    最近更新 更多