Skip to content

Instantly share code, notes, and snippets.

@sumeetpareek
Last active December 21, 2021 15:46
Show Gist options
  • Save sumeetpareek/5703e86528e4c1e9f596 to your computer and use it in GitHub Desktop.
Save sumeetpareek/5703e86528e4c1e9f596 to your computer and use it in GitHub Desktop.
packer template to create aws ami using ansible provisioner
{
"variables": {
"aws_access_key": "", // This helps me connect to AWS
"aws_secret_key": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "SECRET_MASK",
"instance_type": "t2.micro",
"ssh_username": "root",
"ami_name": "vimn_drupal_base_{{timestamp}}",
"vpc_id": "SECRET_MASK",
"subnet_id": "SECRET_MASK"
}],
"provisioners": [{
"type": "shell",
"inline": ["sleep 10"] // At first you want the shell to sleep for sometime, so that SSH is available
}, {
"type": "shell",
"inline": [
"yum install -y epel-release", // I needed this to be able to install extra yum packages
"yum install -y ansible" // You need to install ansible on your AWS box. Because ansible playbook would run locally
]
}, {
"type": "ansible-local",
"playbook_file": "../ansible/single_vbox_drupal/packer-ec2-ami.yml", // This runs your plays / roles locally
"role_paths": [ // This copies the roles to newly created AWS ec2
"../ansible/single_vbox_drupal/roles/common",
"../ansible/single_vbox_drupal/roles/apache",
"../ansible/single_vbox_drupal/roles/drush",
"../ansible/single_vbox_drupal/roles/mysql",
"../ansible/single_vbox_drupal/roles/php",
"../ansible/single_vbox_drupal/roles/site"
],
"group_vars": "../ansible/single_vbox_drupal/group_vars/all", // useful to tell ansible, not must
"playbook_dir": "../ansible/single_vbox_drupal" // useful to tell ansible, not must
}, {
"type": "shell",
"inline": [
"rm -rf /root/.ssh/authorized_keys" // I need this so that the new AMI would work with the KEY I create in my AWS account. This is because of a CentOS bug.
]
}]
}
@heitorlessa
Copy link

Thanks for sharing ;)

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