0
votes

i am new to Ansible, and have write .yml file to empty a file, just like ">file_name"

---

tasks:


  - name: Empty Log Files greater then 400M



     shell: 'find "{{ item }}" -name "messages*" -size +400M -exec sh -c '> {}' \;'

    with_items:
      - /var/log
      - /var/opt
      - /var/spool/mail
    ignore_errors: yes

and i am getting this following error

ERROR! Syntax Error while loading YAML.

The error appears to have been in '/tmp/clean.yml': line 7, column 11, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  • name: Delete Log Files greater then 400M shell: 'find "{{ item }}" -name "messages*" -size +400M -exec sh -c '> {}' \;' ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:

    with_items:

    • {{ foo }}

Should be written as:

with_items:
  - "{{ foo }}"

exception type: exception: mapping values are not allowed in this context in "", line 7, column 11

where im getting it wrong?

1
Hi...WC to SO! I will suggest to use ansible find: module instead of shell command. docs.ansible.com/ansible/latest/modules/find_module.html...the yml syntax plays an important role in ansible....you should validate your syntax using ansible` --syntax-check` optionerror404
Have you checked that line in question? It contains a single quote in the middleNico Haase

1 Answers

0
votes

Kindly refer below playbook as a reference for the same.

---
- name: play to check the file size
  hosts: "{{ servers }}"
  tasks:
    - name: Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
      find:
        paths: "{{ item }}"
        age: 4w
        size: 1m
        recurse: yes
      with_items:
      - path_1
      - path_2
      - path_3