Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tiswanso/217e403ce9ac96bbc5cc22d30cceedb2 to your computer and use it in GitHub Desktop.
Save tiswanso/217e403ce9ac96bbc5cc22d30cceedb2 to your computer and use it in GitHub Desktop.
There's a couple options for code-reuse in playbooks:
1) include a playbook from another playbook (& set vars in outer playbook)
2) make the common tasks into a role that's used by specific playbooks (& set vars at playbook level)
My general preference is to use roles (option 2). If you put the common tasks into a role, ie. run_tasks, you can just make a var
for the openstack branch and have each different playbook set it. For example:
networking-cisco-tox-master.yml
---
- hosts: all
name: Autoconverted job legacy-networking-cisco-tox-master from old job gate-networking-cisco-tox-master-ubuntu-xenial
vars:
openstack_branch: master
roles:
- role: run_tox
---
networking-cisco-tox-pike.yml
---
- hosts: all
name: Autoconverted job legacy-networking-cisco-tox-pike from old job gate-networking-cisco-tox-pike-ubuntu-xenial
vars:
openstack_branch: pike
roles:
- role: run_tox
---
... and so on...
The new role would look like this:
playbooks/roles/run_tox/main.yaml
---
- name: Ensure legacy workspace directory
file:
path: '{{ ansible_user_dir }}/workspace'
state: directory
- shell:
cmd: |
set -e
set -x
CLONEMAP=`mktemp`
REQS_DIR=`mktemp -d`
function cleanup {
mkdir -p $WORKSPACE
rm -rf $CLONEMAP $REQS_DIR
}
trap cleanup EXIT
cat > $CLONEMAP << EOF
clonemap:
- name: $ZUUL_PROJECT
dest: .
EOF
# zuul cloner works poorly if there are 2 names that are the
# same in here.
if [[ "$ZUUL_PROJECT" != "openstack/requirements" ]]; then
cat >> $CLONEMAP << EOF
- name: openstack/requirements
dest: $REQS_DIR
EOF
fi
/usr/zuul-env/bin/zuul-cloner -m $CLONEMAP --cache-dir /opt/git \
git://git.openstack.org $ZUUL_PROJECT openstack/requirements
# REQS_DIR is not set for openstack/requirements and there is also
# no need to copy in this case.
if [[ "$ZUUL_PROJECT" != "openstack/requirements" ]]; then
cp $REQS_DIR/upper-constraints.txt ./
fi
executable: /bin/bash
chdir: '{{ ansible_user_dir }}/workspace'
environment: '{{ zuul | zuul_legacy_vars }}'
- shell:
cmd: /usr/local/jenkins/slave_scripts/install-distro-packages.sh
chdir: '{{ ansible_user_dir }}/workspace'
environment: '{{ zuul | zuul_legacy_vars }}'
- shell:
cmd: |
if [ -x tools/test-setup.sh ] ; then
tools/test-setup.sh
fi
chdir: '{{ ansible_user_dir }}/workspace'
environment: '{{ zuul | zuul_legacy_vars }}'
- shell:
cmd: |
set -x
sudo rm -f /etc/sudoers.d/zuul
# Prove that general sudo access is actually revoked
! sudo -n true
executable: /bin/bash
chdir: '{{ ansible_user_dir }}/workspace'
environment: '{{ zuul | zuul_legacy_vars }}'
- shell:
cmd: /usr/local/jenkins/slave_scripts/run-tox.sh {{ openstack_branch }}
chdir: '{{ ansible_user_dir }}/workspace'
environment: '{{ zuul | zuul_legacy_vars }}'
- shell:
cmd: |
OUT=`git ls-files --other --exclude-standard --directory`
if [ -z "$OUT" ]; then
echo "No extra files created during test."
exit 0
else
echo "The following un-ignored files were created during the test:"
echo "$OUT"
exit 0 # TODO: change to 1 to fail tests.
fi
executable: /bin/bash
chdir: '{{ ansible_user_dir }}/workspace'
environment: '{{ zuul | zuul_legacy_vars }}'
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment