Using Ansible Pull In Ansible Projects
Using A Pull Model To Deploy Code With Ansible
If you haven’t heard of Ansible Pull, thats because there is not really a lot of documentation on the subject. I stumbled across it a little while ago and found it to a good alternative, instead of needing to do the usual push implementation via Ansible Playbooks.
For example, if you wanted to install code from a GitHub repository every morning at a certain time, to make sure your development environment was up to date with the latest code, you could create a cronjob on your host that runs the ansible-pull command to download the latest master branch or development branch and install it on our server.
As always, the best way to try and demonstrate how ansible-pull works is through an example. In the following we will set up a cronjob that will make sure our server has the latest version of an example application. This application doesn’t really do much but it hopefully gives you a good indication on what you can do with ansible-pull. We have set up the code and it is available on GitHub for your convenience at: https://github.com/vincesesto/ansible-pull-example
If you look at the code, it is pretty straight forward. If we look through the directories you will be able to see how an ansible-pull job is set up:
ansible-pull-example/
├── README.md
├── ansible_test_app
│ └── ansible_app.sh
├── hosts
└── local.yml1 directory, 4 files
- README.md has all the usual read me details and interesting information and ansible_test_app is the application script of the project.
- hosts, is exactly the same as a hosts file we use with Ansible playbooks and gives us the location of specifically labeled servers. We will show you more details below.
- Lastly, the local.yml file. This resides in the root of the project directory and is what the ansible-pull command looks for. It is basically a playbook that will run through different tasks to set up our code the way we need it. We will have our example, further down for a more in depth discussion.
The Cronjob
This is the easy part, all you will be doing is adding the following as a cronjob to your system.
0 3 * * * /usr/local/bin/ansible-pull -U https://github.com/vincesesto/ansible-pull-example -i hosts
- If you’re new to cronjobs, all you need to know is this job is being run every day at 3am.
- The next part of the command is simply the full path to the ansible-pull command, in this instance, we are using the -U option to make sure our code is updated, if it is already installed.
- Then finally, the command also includes a hosts file that our playbook will run against.
Hosts File
Just like we said earlier, this is the same as a regular hosts file we use with a playbook. You specify a label of the host and then place the IP address or addresses underneath. As we are wanting to run this on the system the command is running on, the values our hosts file arelisted below.
1 [local]
2 127.0.0.1
The local.yml File
The local.yml file is where things get interesting and by now, if you have been using Ansible, it should be fairly straight forward for you to work out what it is doing. It’s a basic playbook which installs the test application into our environment.
1 ---
2 - hosts: all
3 tasks:
4 - name: install example application
5 copy:
6 src: ansible_test_app
7 dest: /tmp/
8 owner: root
9 group: root
As you can see, its a simple playbook that performs one task.
- Line 2 defines the hosts, it will be deployed to, in this case all of the hosts specified in our hosts file.
- Line 3 to 9 are the only task of our playbook which installs our application using the copy module to the /tmp/ directory as its destination, and finally changes the ownership of the directory and file.
Although ansible-pull is limited with the options available to it, the playbook it can then download allows you to basically do anything you can with a regular Ansible playbook. The ansible-pull command does allow you to checkout a different branch instead of the master branch and also set up different host files.
For more detail on the different options you can use with the ansible-pull command, please see the official documentation at the following link: https://docs.ansible.com/ansible/2.4/ansible-pull.html
Found this post useful? Kindly tap the clap button below! :)
About The Author