Skip to content

Instantly share code, notes, and snippets.

@chrisblossom
Last active July 16, 2017 06:53
Show Gist options
  • Save chrisblossom/8979722 to your computer and use it in GitHub Desktop.
Save chrisblossom/8979722 to your computer and use it in GitHub Desktop.
Packer Ansible-from-git-checkout Provisioner
### *** 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://git@github.com/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://git@github.com/GITHUB_REPO /tmp/ansible/repo/" sections from above
@yograterol
Copy link

Thanks! You save my night migrating our servers provisioning to Packer with BASH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment