简体   繁体   English

使用时的Ansible剧本

[英]Ansible playbook when usage

- name: restart dcache if mem low
  hosts: test
  tasks:
  - name: getMem
    shell: /bin/bash /etc/zabbix/shell/MonitorMem.sh
    register: memNum
  - name: restart dcache if mem low
    shell: killall -9 dcache
    when: memNum < 3

MonitorMem.sh returns a num(an integer) that represents free memory,I want to use when to decide whether execute restart action. MonitorMem.sh返回一个表示可用内存的num(一个整数),我想在何时决定是否执行重启操作时使用。 but every time I run the playbook.it will skip restart action. 但每次我运行该剧本时,它都会跳过重启操作。 please give me a hand, thanks in advance. 请先帮我一下,谢谢。

The shell module's documentation provides the structure of the return values and the key point is that it returns a data structure (dict) that contains, amongst other things, the "standard out" (output) of the shell command. shell模块的文档提供了返回值的结构,关键是它返回的数据结构(dict)除其他外还包含shell命令的“标准输出”(输出)。 The stdout is found in the stdout attribute of the returned dictionary. 在返回的字典的stdout属性中找到该stdout The stdout_lines attribute contains the same, but with each line as a separate array entry. stdout_lines属性包含相同的属性,但每一行均作为单独的数组条目。

I added also a int jinja filter to convert the string value to an integer. 我还添加了一个int jinja过滤器,可将字符串值转换为整数。

- name: restart dcache if mem low
  hosts: test

  tasks:
  - name: getMem
    shell: /bin/bash /etc/zabbix/shell/MonitorMem.sh
    register: memNum

  - name: restart dcache if mem low
    shell: killall -9 dcache
    when: memNum.stdout|int < 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM