Michael,
Thank you for looking at my post. I'll try to clarify -- feel free to skip towards the bottom and examine the playbook as this is verbose;
Jenkins jobs are used to test and deploy our application [using ansible] if the tests pass. Development of our application occurs in feature branches -- and ansible provisions an accessible environment for each branch as part of the deployment process. So for instance if our goal is to implement "excel import functionality", we create a git branch named "f/excel" and then instruct the application's jenkins job to build the "f/excel" branch via a "post build task" shell script -- which makes the application code accessible e.g. at http://[branch].
features.application.com/ - The post build task resembles something like;
----
#!/bin/sh
APPLICATION="example"
ROLE="app-server"
#strip origin/ from GIT_BRANCH
GIT_BRANCH=${GIT_BRANCH#*/}
### prepare environment (if not already prepared)
ENVIRONS=~/.environs/$APPLICATION
mkdir -p $ENVIRONS
if [ ! -f $ENVIRONS/$GIT_SAFE_BRANCH];
then
ansible-playbook -i automation/common/hosts automation/apps/$APPLICATION/$APPLICATION+$ROLE.yml -e "git_branch=$GIT_BRANCH" --private-key=infrastructure/keys/$ROLE+root.key
touch "$ENVIRONS/$GIT_SAFE_BRANCH"
fi
## deploy branch
ansible-playbook -i automation/common/hosts automation/apps/$APPLICATION/$APPLICATION-deploy.yml -e "git_branch=$GIT_BRANCH" --private-key=infrastructure/keys/$ROLE+$APPLICATION.key
----
Notice that we pass `git_branch` as an external variable to the ansible playbook, based on the branch passed to the jenkins build. I currently use this to hint the desired target environment (e.g. master == production, develop == staging, f/excel == feature branch)... and need this normalized as I create user accounts, application directories, etc. etc. based on the branch/environment. E.g.
tasks:
- name: checkout to application server
action: git repo=$git_repo dest=$application_dir version=$git_branch
so;
/apps/application-master/[code] features code running our production environment available at
https://application.com//apps/application-develop/[code] features code running our staging environment available at
https://staging.application.com//apps/application-f_excel/[code] features code runninng the import excel feature available at
http://excel.features.application.com/Here is stripped example of my deploy playbook;
---
- name: APPLICATION DEPLOYMENT
hosts: $host_group
user: application
gather_facts: no
vars:
namespace: application
###############################################################################
# @var host_group: (str) host(s) to deploy to [e.g. fermata-production]
# @var git_branch: (str) master|develop
#
# MUST BE PASSED VIA COMMAND LINE, e.g.
# ansible deploy.yml -e "git_branch=develop workspace=/builds/app host_group="
###############################################################################
# normalize $git_branch, replace non-alphanumerics with `_`
branch: $PIPE(echo "$git_branch" | sed 's/[^a-zA-Z0-9\_\-]/_/g')
vars_files:
- "../../common/vars/global_vars.yml"
- [ "vars/$namespace-$branch.yml", "vars/$namespace.yml" ]
tasks:
- name: checkout to application server
action: git repo=$git_repo dest=$application_dir version=$git_branch
- name: (configuration) synching settings.$branch.clj with settings.clj
action: template src=$item dest=$application_dir/settings.clj
first_available_file:
- templates/settings.$branch.clj
- templates/settings.clj
- include: $task_dir/application-restart.yml
----
The line I am most concerned with is;
# normalize $git_branch, replace non-alphanumerics with `_`
branch: $PIPE(echo "$git_branch" | sed 's/[^a-zA-Z0-9\_\-]/_/g')
(Should I be using | Is it possible to use) Python to sanitize $git_branch ?
Thanks again,
~ Brice