简体   繁体   English

有条件的时候

[英]When Condition with Ansible

I am new to Ansible, and I am writing a script to install a package when disk space is more then a limit. 我是Ansible的新手,当磁盘空间超出限制时,我正在编写脚本来安装软件包。 I am getting error like this >> error while evaluating conditional 我在评估条件时遇到这样的错误>>错误

---
- hosts: dev
  become: true
  become_user: root
  tasks:
   - name: Install zsh if enough space
      yum:
       name: zsh
       state: latest
       with_items: "{{ ansible_mounts}}"
      when: item.mount == "/" and item.size_available > 10737400

I am giving the size in bytes. 我以字节为单位给出大小。 ( Is there a way to give the size in MB ? ) (有没有办法以MB为单位给出大小?)

Thanks. 谢谢。

Ansible uses the YAML format, you need to use the right indent. Ansible使用YAML格式,您需要使用正确的缩进。 In YAML, the indent is important as closing brackets or semicolons in most programming languages. 在YAML中,缩进在大多数编程语言中作为右括号或分号很重要。

with_items is not a definition for the yum module, it is a directive for Ansible, so it should be at the same level as when and the module call (eg yum ). with_items不是yum模块的定义,它是Ansible的指令,因此它应该与when和模块调用处于同一级别(例如yum )。 Both examples below should work: 下面的两个示例都应该起作用:

---
- hosts: dev
  become: true
  become_user: root
  tasks:
   - name: Install zsh if enough space
     yum:
       name: zsh
       state: latest
     with_items: "{{ ansible_mounts }}"
     when: item.mount == "/" and item.size_available > 10737400

or 要么

---
- hosts: dev
  become: true
  become_user: root
  tasks:
   - name: Install zsh if enough space
     with_items: "{{ ansible_mounts }}"
     when: item.mount == "/" and item.size_available > 10737400
     yum:
       name: zsh
       state: latest

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

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