Last active
July 16, 2017 06:53
-
-
Save chrisblossom/8979722 to your computer and use it in GitHub Desktop.
Packer Ansible-from-git-checkout Provisioner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### *** Contains some Ubuntu-specific commands/packages. Modify accordingly. *** | |
### | |
# scripts/ansible.sh | |
### | |
#!/usr/bin/env bash | |
# Ansible dependencies | |
# This can be put in preseed.cfg as well | |
apt-get install git python-jinja2 python-paramiko python-yaml python-httplib2 | |
mkdir -p /tmp/ansible/repo/ | |
git clone https://github.com/ansible/ansible.git /tmp/ansible/git/ | |
cd /tmp/ansible/git/ | |
source ./hacking/env-setup | |
# Specify branch | |
git checkout devel | |
chmod +x /tmp/ssh-git.sh | |
export GIT_SSH="/tmp/ssh-git.sh" | |
export GIT_KEY="/tmp/github.rsa" | |
git clone ssh://[email protected]/GITHUB_REPO /tmp/ansible/repo/ | |
cd /tmp/ansible/repo/ | |
ansible-playbook --connection=local -i INVENTORY_FILE PLAYBOOK | |
# # Cleanup | |
rm -rf /tmp/ansible/ | |
rm /tmp/ssh-git.sh | |
rm /tmp/github.rsa | |
### | |
# files/ssh-git.sh | |
### | |
#!/usr/bin/env sh | |
if [ -z "$GIT_KEY" ]; then | |
# if GIT_KEY is not specified, run ssh using default keyfile | |
ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o ChallengeResponseAuthentication=no "$@" | |
else | |
ssh -i "$GIT_KEY" -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o ChallengeResponseAuthentication=no "$@" | |
fi | |
### | |
# files/github.rsa | |
### | |
-----BEGIN RSA PRIVATE KEY----- | |
YOUR_PRIVATE_KEY | |
-----END RSA PRIVATE KEY----- | |
### | |
# template.json | |
### | |
... | |
{ | |
"type": "file", | |
"source": "files/github.rsa", | |
"destination": "/tmp/github.rsa" | |
}, | |
{ | |
"type": "file", | |
"source": "files/ssh-git.sh", | |
"destination": "/tmp/ssh-git.sh" | |
}, | |
{ | |
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'", | |
"scripts": [ | |
"scripts/ansible.sh" | |
], | |
"type": "shell" | |
}, | |
... | |
### | |
# Notes | |
### | |
You can also skip the git checkout and install from local source (example with ansible files in ./ansible_files) by: | |
"First, the destination directory must already exist. If you need to create it, use a shell provisioner just prior to the file provisioner in order to create the directory." | |
Source: From http://www.packer.io/docs/provisioners/file.html | |
... | |
{ | |
"execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'", | |
"inline": [ | |
"mkdir -p /tmp/ansible/repo" | |
], | |
"type": "shell" | |
}, | |
{ | |
"type": "file", | |
"source": "ansible_files/", | |
"destination": "/tmp/ansible/repo" | |
}, | |
... | |
You could then remove the "*ssh-git.sh" , "*github.rsa" , "git clone ssh://[email protected]/GITHUB_REPO /tmp/ansible/repo/" sections from above |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! You save my night migrating our servers provisioning to Packer with BASH