1

I am relatively inexperienced when it comes to Ansible however I have previously created playbooks which will create a cron job successfully. For the life of me I cannot work out what is wrong with my .yml file.

I am receiving the below error when attempting to run my playbook locally with:

ansible-playbook -i "localhost," -c local crond.yml


ERROR! 'cron' is not a valid attribute for a Play

The error appears to have been in 
'/etc/ansible/cds/roles/backups/tasks/crond.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- cron:
  ^ here

I have seen similar questions asked on SE/Ansible forums but to no avail. Any help/guidance would be much appreciated.

Here is my crond.yml file:

- cron:
    user: "root"
    minute: "30"
    hour: "02"
    job: '{{/usr/bin/rsync -avhz -e "ssh" --rsync-path="sudo rsync" -H --delete --numeric-ids <ipaddress>:"/data/rsnapshot-backups/<ipaddress>" /data/rsnapshot-backup}}'
    state: present
1

crond.yml in your example is not a playbook. It is a tasks file (list of tasks), while playbook is a list of plays.

A play must have hosts directive at minimum. Please see http://docs.ansible.com/ansible/latest/playbooks_intro.html#hosts-and-users

This is a playbook:

- hosts: localhost
  tasks:
    - cron:
        user: "root"
        minute: "30"
        hour: "02"
        job: '/usr/bin/rsync -avhz -e "ssh" --rsync-path="sudo rsync" -H --delete --numeric-ids <ipaddress>:"/data/rsnapshot-backups/<ipaddress>" /data/rsnapshot-backup'
        state: present
  • I have tried using this layout too, I get a syntax error when attempting to run with the same command. My company are using an overarching site.yml, when I run said playbook with --tags backups (relative tag to the crond.yml) it runs, but still fails. – jto Jan 29 '18 at 12:14
  • Yes, syntax error, because you have messed {{...}} in job definition. I removed it with my edit. – Konstantin Suvorov Jan 29 '18 at 12:25
  • No, I need the {{...}} to retain the double quotes around the single quotes. – jto Jan 29 '18 at 12:40
0

I got around this by adding my crond.yml task to the global playbook and now call it via tags (ansible-playbook -vvv -i "localhost," -c local site.yml --tags backups).

I added the cron_file section to define the location of the cron job as /etc/cron.d/rsync.cron rather than the root users crontab. I then added state: present to ensure that the cronjob exists on the server and doesn't delete other entries in the file.

I also updated the job section of the task to remove the '{{ }}' and replace with double quotes surrounding the command as well as '\'s around any double quoted strings like below:

- cron:
    name: "Copy all rsnapshot backups from <hostname>"
    minute: "30"
    hour: "02"
    job: "/usr/bin/rsync -avhz -e \"ssh\" --rsync-path=\"sudo rsync\" -H --delete --numeric-ids <ipaddress>:\"/data/rsnapshot-backups/<ipaddress>\" /data/rsnapshot-backup"
    user: root
    cron_file: rsync.cron
    state: present

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.