如何使用“set_fact”和“when”在ansible中运算符功能

时间:2017-04-05 20:20:25

标签: linux unix automation ansible redhat

如何使用set_fact模块和“when”使用以下条件?我需要根据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

2 个答案:

答案 0 :(得分:0)

首先,您不需要从shell获取MemTotal,您可以从ansible facts获取此信息。在这种情况下,事实被称为ansible_memtotal_mb。 ansible工作的操作员非常像python操作符,这是我的笔记本的示例playbook,总内存为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

答案 1 :(得分:0)

您可以计算您的小值:

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