title: How can I ensure more than one specific cron job exist using host_vars?
body: <p>I want to write an ansible playbook (I am pretty new to ansible - so be kind with me) that checks if various cronjobs exist in the user crontab. The jobs should just execute various scripts. The jobs are the same for all hosts except for the script name. A line in the crontab looks like this: </p>
  
  <pre><code>30 5 * * * /path/to/script/monitoringScript_serviceX &gt; /path/to/script/monitoringScript_serviceX.out 2&gt;&amp;1
  </code></pre>
  
  <p>Depending on which host I am on there are different services defined, resulting in different script names. I defined the services as a YAML list in host_vars in one file per host (inventory/host_vars/host1/services)</p>
  
  <p>One services file looks something like this:</p>
  
  <pre><code>services:
      - service1
      - service2
  </code></pre>
  
  <p>Some of the hosts only have one defined service. </p>
  
  <p>My playbook looks like this:</p>
  
  <pre><code>  - hosts: hostgroupX
      gather_facts: False
      tasks:
        - name: Ensure monitoring cron jobs are present
          cron:
              name: "check: monitoring data jobs"
              hour: "5"
              minute: "30"
              user: "user"
              job: "/path/to/script/monitoringScript_{{ item }} &gt; /path/to/script/monitoringScript_{{ item }}.out 2&gt;&amp;1"
          loop: "{{ services }}"
  </code></pre>
  
  <p>If I let the play run on one host on which only one service is defined in the file everything is working well. </p>
  
  <p>But if there is more than one service defined only the last one in the list is written in the crontab. The result with the example services file from above would look like this:</p>
  
  <pre><code>#Ansible: check: monitoring data jobs 
  30 5 * * * /path/to/script/monitoringScript_service2 &gt; /path/to/script/monitoringScript_service2.out 2&gt;&amp;1
  </code></pre>
  
  <p>even though I want it to look like this:</p>
  
  <pre><code>#Ansible: check: monitoring data jobs 
  30 5 * * * /path/to/script/monitoringScript_service1 &gt; /path/to/script/monitoringScript_service1.out 2&gt;&amp;1
  30 5 * * * /path/to/script/monitoringScript_service2 &gt; /path/to/script/monitoringScript_service2.out 2&gt;&amp;1
  </code></pre>
  
  <p>I would appreciate any ideas how to achieve this, thanks in advance!</p>