# Using Ansible for Application Deployment

**URL:** <https://forum.ansible.com/t/using-ansible-for-application-deployment/18270>\
**Category:** Ansible Project\
**Created:** [May 20, 2015, 12:32pm UTC](https://forum.ansible.com/t/using-ansible-for-application-deployment/18270 "2015-05-20T12:32:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Herron](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Mike\_Herron](https://forum.ansible.com/u/Mike_Herron)\
**Post date:** [May 20, 2015, 12:32pm UTC](https://forum.ansible.com/t/using-ansible-for-application-deployment/18270/1 "2015-05-20T12:32:59Z")

</div>

I’ve been using Ansible for a few years, mostly for server provisioning and configuration management. I think it’s a fantastic tool. For a new project, I’ve also begun to attempt to use Ansible as a web application deployment tool where I would have previously used ant. However, there are a couple of snags that have me questioning whether the Ansible model is the best fit for this type of task.

For example, the first thing I want to is download the deployment artefact from Jenkins. This is only available internally, so my plan was to download locally and then upload to the production servers. I quickly realised that this was totally different to anything I’d done before as I was trying to perform a task that wasn’t really associated with any remote hosts. After a bit of searching, I found this:

- hosts: 127.0.0.1

connection: local

Which works perfectly well, and could have been designed for exactly this type of task, but it feels a bit like trying to fit a round peg in to a square hole. I understand that since all modules assume that they are executing within the context of a host, there is really no other way it could be, but it got me thinking if Ansible is a good place to write these type of non-host specific, pre-deployment steps?

The next challenge then was to have the play book prompt the user for the job and build to deploy. var\_prompt seemed perfect for this, so I ended up with a playbook like so:

- hosts: 127.0.0.1  
connection: local  
vars\_prompt:  
jenkins\_job: “Job Name”  
jenkins\_build: “Build Number”  
roles:

- jenkinsfetch

- hosts: dashboards  
roles:

- { role: uploadapp, app\_name: ‘dashboard’ }

Which seemed to work well until I tried to reference jenkins\_job and jenkins\_build from within the uploadapp role and couldn’t:

TASK: [uploadapp | upload build tar] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [local.dev] =\> One or more undefined variables: ‘jenkins\_job’ is undefined

FATAL: all hosts have already failed – aborting

I spent a bit of time trying to figure out how to make these variables visible across the plays but couldn’t. I tried set\_fact, which didn’t work as it’s host specific. I tried to include both roles within the same play but couldn’t (obviously as I don’t want to run uploadapp on 127.0.0.1) and finally just resolved to put the variables as part of a global var file rather than prompt for them. I can workaround this works, but it feels to me like I’m trying to make Ansible do something it really doesn’t want to.

The variable problem also came up when I wanted to abbreviate some paths within my jenkinsfetch role:

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 20, 2015, 3:05pm UTC](https://forum.ansible.com/t/using-ansible-for-application-deployment/18270/2 "2015-05-20T15:05:34Z")

</div>

Yeah so vars\_prompt is local to a specific play (remember here you can have multiple plays in a playbook).

I’m wondering if you could, or would want to pass the prompted values to your jenkinsfetch role though?

I use set\_fact to set facts which aren’t just true for the current host in some cases.

Interested to hear what others do though.

Jon
