Integration with Ansible
The initial plan for using Ansible at is to gradually replace existing obrar tasks within scorch with Ansible playbooks. This modular approach allows us to migrate to using Ansible as the underlying deployment mechanism without the large risk associated with a 'big bang' approach.
Sample scorch plugin
The sample below shows how easy it is to utilize an Ansible playbook with scorch . In this example we require two input variables based on the ENVIRONMENT and RELEASE parameters passed to scorch in the add template. These are mandatory for this plugin, if they are not specified the job will fail. Once received they are passed to the Ansible playbook using the --extra-vars switch.
scorch template to invoke this plugin
ACTION : ANSIBLE_DEMO
ENVIRONMENT : <any DEV or TEST environment i.e DEV1, FT1, etc>
RELEASE : A valid baseline release i.e VR-5.6.6
Plugin Source
Ansible_DEMO plugin
Plugin_ANSIBLE-DEMO() {
# Check we have what we need [[ -z ${RELEASE} ]] && WPError "No release set" [[ -z ${ENVIRONMENT} ]] && WPError "No environment set"
# OK we have some input vars, let's try and run it
# Hard coded locations for ansible binary, environment and playbooks
AddTask "/usr/bin/ansible-playbook -i /deployment/ansible/space/environments/DEV/deployment/ansible/space/common/playbooks/VERSIONREPORT.yml --extra-vars \"{'RELEASE':'${RELEASE}','ENV':'${ENVIRONMENT}'}\""
# Now the same using the envvars from the ansible_profile
AddTask "$ANSIBLE_BIN/ansible-playbook -i $ANSIBLE_HOME/environments/$ANSIBLE_ENV $ANSIBLE_HOME/common/playbooks/VERSIONREPORT.yml --extra-vars \"{'RELEASE':'${RELEASE}','ENV':'${ENVIRONMENT}'}\""
# Example limiting ansible playbook to a single tag on a single NDT environment (using the scorch environment variable) and for just a subset of hosts for CSS
AddTask "$ANSIBLE_BIN/ansible-playbook -i $ANSIBLE_HOME/environments/$ANSIBLE_ENV/${ENVIRONMENT} $ANSIBLE_HOME/common/playbooks/python.yml --limit \"CSS\" --tags \"pyver\""
# Example limiting ansible playbook to a two tags on a single NDT environment (using the scorch environment variable) and for just a subset of hosts for CSS
AddTask "$ANSIBLE_BIN/ansible-playbook -i $ANSIBLE_HOME/environments/$ANSIBLE_ENV/${ENVIRONMENT} $ANSIBLE_HOME/common/playbooks/python.yml --limit \"CSS\" --tags \"debug,pyall\""
} |
Using tags to allow single task invocation from an Ansible Playbook
It's very simple to run just a single task from a playbook that contains many of them by using ansible tags to limit the scope of your play. Using the scorch AddTask we can simply specify the tag we need from a playbook as shown below;
Running a single tag from a playbook
AddTask "$ANSIBLE_BIN/ansible-playbook -i $ANSIBLE_HOME/environments/$ANSIBLE_ENV $ANSIBLE_HOME/common/playbooks/python.yml --tags \"pyver\"" |
In this example we'll invoke the python.yml playbook which contains several tasks but we'll only run the ones with the "pyver" tag. In essence this will limit the tasks run to just one of the four available in the playbook.