<p>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).</p>

<p>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.</p>

<p>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:</p>

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

CC BY-SA 3.0
2018-01-29T13:40:46.927Z
2018-01-29T14:30:48.940Z
2018-01-29T14:30:48.940Z
jto
<p><code>crond.yml</code> in your example is not a playbook. It is a tasks file (list of tasks), while playbook is a list of plays.</p>

<p>A play must have <code>hosts</code> directive at minimum. Please see <a href="http://docs.ansible.com/ansible/latest/playbooks_intro.html#hosts-and-users" rel="nofollow noreferrer">http://docs.ansible.com/ansible/latest/playbooks_intro.html#hosts-and-users</a></p>

<p>This is a playbook:</p>

<pre><code>- 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 &lt;ipaddress&gt;:"/data/rsnapshot-backups/&lt;ipaddress&gt;" /data/rsnapshot-backup'
        state: present
</code></pre>

CC BY-SA 3.0
2018-01-29T12:14:45.083Z
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
CC BY-SA 3.0
2018-01-29T12:25:23.827Z
Yes, syntax error, because you have messed `{{...}}` in job definition. I removed it with my edit.
Konstantin Suvorov
CC BY-SA 3.0
2018-01-29T12:40:21.720Z
No, I need the {{...}} to retain the double quotes around the single quotes.
jto
CC BY-SA 3.0
2018-01-29T12:03:13.863Z
2018-01-29T12:24:52.073Z
2018-01-29T12:24:52.073Z
Konstantin Suvorov
<p>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.</p>

<p>I am receiving the below error when attempting to run my playbook locally with:</p>

<pre><code>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
</code></pre>

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

<p>Here is my crond.yml file:</p>

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

<ansible><linux>
Creating a cron job with Ansible (ERROR! 'cron' is not a valid attribute for a Play)
jto
# Creating a cron job with Ansible (ERROR! 'cron' is not a valid attribute for a Play)

(Asked by: username_0 on 2018-01-29)

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 :"/data/rsnapshot-backups/" /data/rsnapshot-backup}}'
    state: present

**Tags:** ansible, linux


---

## 2 answers

### Answer by username_0 on 2018-01-29. Score: 0 | Accepted answer

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 "
    minute: "30"
    hour: "02"
    job: "/usr/bin/rsync -avhz -e \"ssh\" --rsync-path=\"sudo rsync\" -H --delete --numeric-ids :\"/data/rsnapshot-backups/\" /data/rsnapshot-backup"
    user: root
    cron_file: rsync.cron
    state: present


---

### Answer by username_1 on 2018-01-29. Score: 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 :"/data/rsnapshot-backups/" /data/rsnapshot-backup'
        state: present

> **Comments:**
> **username_0**: 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.  
> **username_1**: Yes, syntax error, because you have messed `{{...}}` in job definition. I removed it with my edit.  
> **username_0**: No, I need the {{...}} to retain the double quotes around the single quotes.  

---

