[英]How can an operator funtion in ansible with “set_fact” and “when”
How can I use the set_fact module with "when" to use below conditions? 如何将set_fact模块与“何时”一起使用以在以下条件下使用? I need "shmall" value to be set depending upon the total memory in a RHEL server.
我需要根据RHEL服务器中的总内存设置“ shmall”值。 I need to know how operators work in Ansible.
我需要知道操作员如何在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
First of all, you don't need to get MemTotal from shell, you can get this info from ansible facts . 首先,您不需要从shell获取MemTotal,可以从ansible facts获取此信息。 In this case the fact is called
ansible_memtotal_mb
. 在这种情况下,事实称为
ansible_memtotal_mb
。 Operators in ansible work pretty much like python operators, here is example playbook for my notebook with 3834 total memory. ansible中的运算符非常类似于python运算符,这是我的笔记本的示例剧本,总内存为3834。
- 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
Edit: Added tasks, as asked in question. 编辑:根据问题添加任务。 You don't need to convert memtotal to KB, it's easier to convert you constraints to MBs
您无需将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
You can calculate your small value: 您可以计算出较小的值:
---
- 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 }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.