First I used the shell module to send a command that will count the number of files in each path and register the output into a variable.
- name: Count files in backups
shell:
cmd: ls | wc -l
chdir: '{{ item }}'
with_items:
- '/home/backups/{{ domain_name }}/fullWP'
- '/home/backups/{{ domain_name }}/coreWP'
- '/home/backups/{{ domain_name }}/mysql'
- '/home/backups/{{ domain_name }}/archive'
- '/home/backups/{{ domain_name }}/config'
register: number_files
tags: clear.files
The following task is supposed to run a command to each file path that counted more than 10 files in the directory.
- name: Delete the oldest backups
shell:
cmd: ls -t | tail -n +11 | xargs rm
chdir: '{{ item }}'
with_items:
- '/home/backups/{{ domain_name }}/fullWP'
- '/home/backups/{{ domain_name }}/coreWP'
- '/home/backups/{{ domain_name }}/mysql'
- '/home/backups/{{ domain_name }}/archive'
- '/home/backups/{{ domain_name }}/config'
when: number_files.stdout > 10 == true
tags: clear.oldFiles
My error tells me that I have an undefined variable but am unsure how to fix this.
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'value'\n\