Abhishek dot py
Abhishek dot py

Reputation: 939

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. 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 ? )

Thanks.

Upvotes: 0

Views: 588

Answers (1)

Felix Stupp
Felix Stupp

Reputation: 81

Ansible uses the YAML format, you need to use the right indent. In YAML, the indent is important as closing brackets or semicolons in most programming languages.

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 (e.g. 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

Upvotes: 1

Related Questions