Skip to content

Instantly share code, notes, and snippets.

@holybit
Last active May 6, 2016 16:44
Show Gist options
  • Save holybit/5fce0fdc3c73747a21342e2fea966041 to your computer and use it in GitHub Desktop.
Save holybit/5fce0fdc3c73747a21342e2fea966041 to your computer and use it in GitHub Desktop.
Packer condistional ansibl-local provisioner w/ variable
{
"variables": {
"aws_access_key": "",
"aws_secret_key": "",
"build_type": "general"
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-321",
"instance_type": "t2.medium",
"ssh_username": "ubuntu",
"ami_name": "analytics-toolkit-{{isotime \"2006-01-02-15-04-05\"}}-{{user `build_type`}}",
"vpc_id": "vpc-123",
"subnet_id": "subnet-123",
"associate_public_ip_address": "true"
}],
"provisioners": [
{
"type": "shell",
"inline": ["sudo apt-get install -y software-properties-common",
"sudo apt-add-repository -y ppa:ansible/ansible",
"sudo apt-get update",
"sudo apt-get install -y ansible"]
},
{
"type": "ansible-local",
"command": "[[ \"{{user `build_type`}}\" == \"general\" ]] && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook",
"playbook_dir": "ansible/",
"playbook_file": "ansible/analytics_user.yml"
}
]
}
# Goal is an ansible-local provisioner that will compare the value of a variable
# and run if it's matches a specific string (e.g., "general") but exit 0 otherwise (i.e., not run the provisioner)
# I can get it to run when equality is true
$ packer build analytics.json
...
amazon-ebs: Executing Ansible: cd /tmp/packer-provisioner-ansible-local && [[ "general" == "general" ]] && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/analytics_user.yml -c local -i /tmp/packer-provisioner-ansible-local/packer-provisioner-ansible-local186516603
...
==> amazon-ebs: Creating the AMI: analytics-toolkit-2016-05-06-16-14-54-general
amazon-ebs: AMI: ami-123
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.
==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-east-1: ami-123
# however when equality is false (i.e., I modified analytics.json changing "general" to "gneral")
# I can't figure out how to get the conditional check to exit 0
$ packer build analytics.json
...
amazon-ebs: Executing Ansible: cd /tmp/packer-provisioner-ansible-local && [[ "general" == "gneral" ]] && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/analytics_user.yml -c local -i /tmp/packer-provisioner-ansible-local/packer-provisioner-ansible-local625270349
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: No AMIs to cleanup
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Error executing Ansible: Non-zero exit status: 1
==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Error executing Ansible: Non-zero exit status: 1
==> Builds finished but no artifacts were created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment