We are now going to create a crontab entry to periodically call ansible-pull. Here too, we will rely on Ansible to create our cronjob remotely. Run the following command, adapting the IP address:
$ ansible '54.175.86.38' --private-key ~/.ssh/EffectiveDevOpsAWS.pem -m cron -a 'name=ansible-pull minute="*/10" job="/usr/local/bin/ansible-pull -U https://github.com/<your_username>/ansible helloworld.yml -i localhost --sleep 60"'
In the preceding command, we are telling Ansible to use the cron module targeting our ec2 instance. We are providing a name that Ansible will use to track the cronjob over time, telling cron to run the job every 10 minutes, and finally the command to execute and its parameters. The parameters we are giving to ansible-pull are the GitHub URL of our branch, the inventory file we just added to our repository, and a sleep that will make the command start at a random time between 1 and 60 seconds after the call started.
This will help spread out the load on the network and prevent all node services from restarting at the same time, if we have more than one server. After waiting for a bit, we can verify that our change is effective:
$ curl 54.175.86.38:3000 Hello World
After manually integrating Ansible to the EC2 instance we created using CloudFormation, we can now formalize the procedure.