Running single ansible tasks in development

As you discovered, vagrant reload doesn’t necessarily run the provisioning, which was needed to apply the your changes to caching. I believe vagrant provision would have been sufficient, without the need to reload.

As you discovered, tags allow you to run portions of your playbook. The nginx tag is assigned to the entire nginx role, so adding the -t nginx option would run all the tasks in that role. As an aside, Ansible also makes it possible to add tags to individual tasks, for even more precision.

Your attempt to run the tagged nginx role by adding -t nginx to the end of your ansible-playbook command would work without SSH errors for staging or production, but development is a little different. The SSH to dev is normally handled through vagrant by the inventory file it sets up at your_project/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory. You’ll see in that file a line like this:

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_private_key_file=/Users/bleh/...

With that background, and till and if roots/trellis#314 is merged, you have a few options for using tags.

1 - Edit your hosts/development inventory. You could replace 127.0.0.1 with the line above that includes the ssh port and key file location. Then your ansible-playbook command should work with dev.

2 - Alternatively, you could feed Ansible the port and key file information by making an entry in your ~/.ssh/ssh_config file for 127.0.0.1. Again, then the ansible-playbook command should work with dev.

3 - Alternatively, you could just run vagrant provision after specifying the nginx tag in your Vagrantfile. Add ansible.tags = [‘nginx’] to the config.vm.provision :ansible section. Remember to remove or comment out the ansible.tags definition afterward.

Note that vagrant sometimes changes the port for a vm if a second vm is booted, which would require you to notice and change the port you’ve specified in option 1 or 2. So, option 3 is maybe most reliable, but requires an edit to your Vagrantfile each time you want to specify a tag.

6 Likes