Packer integration with Ansible
Pre-requirement
Don't forget to install ansible
- Install pip
# apt-get install python-pip
- Install ansible
# pip install ansible==2.8.3
- Check ansible version
# ansible --version
ansible 2.8.3
Create Ansible Directory
Packer
├── ISO_Ubuntu_Server_xenial_16.04.6.json
├── ansible
│ ├── all.yml
│ ├── ansible.cfg
│ └── roles
│ └── install-offical-nginx
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
├── http
│ └── preseed.cfg
└── output
13 directories, 12 files
- Check
ansible.cfg
file
[defaults]
inventory = ./inventory
roles_path = ./roles
host_key_checking = False
retry_files_enabled = False
gathering = smart
fact_caching = jsonfile
fact_caching_connection = ./cache
fact_caching_timeout = 60
#strategy = mitogen_linear
#strategy_plugins = ./mitogen/ansible_mitogen/plugins/strategy/
callback_whitelist = profile_tasks
[callback_profile_tasks]
task_output_limit = 60
[privilege_escalation]
become=True
become_user=root
- Check
all.yml
file
---
- hosts: all
become: True
roles:
- install-offical-nginx
- Check
ISO_Ubuntu_Server_xenial_16.04.6.json
file
{
"variables":{
"vm_description": "Ubuntu Server Image",
"vm_version": "0.0.1",
"cpus": "2",
"memory": "2048",
"disk_size": "8192",
"vm_name": "ubuntu",
"iso_url": "http://ftp.ubuntu-tw.org/mirror/ubuntu-releases/16.04.6/ubuntu-16.04.6-server-amd64.iso",
"iso_checksum": "16afb1375372c57471ea5e29803a89a5a6bd1f6aabea2e5e34ac1ab7eb9786ac",
"iso_checksum_type": "sha256",
"ssh_username": "ubuntu",
"ssh_password": "ubuntu",
"s3_bucket_name": "packer-images"
},
"provisioners": [
{
"type": "ansible",
"user": "ubuntu",
"playbook_file": "./ansible/all.yml"
}
],
"builders": [
{
"type": "virtualbox-iso",
"output_directory": "builds",
"format": "ova",
"guest_os_type": "Ubuntu_64",
"iso_url": "{{user `iso_url`}}",
"iso_checksum": "{{user `iso_checksum`}}",
"iso_checksum_type": "{{user `iso_checksum_type`}}",
"ssh_username": "{{user `ssh_username`}}",
"ssh_password": "{{user `ssh_password`}}",
"ssh_port": 22,
"ssh_wait_timeout": "2000s",
"disk_size": "{{user `disk_size`}}",
"keep_registered": "true",
"shutdown_command": "echo {{user `ssh_password`}} | sudo -S shutdown -P now",
"vboxmanage": [
["modifyvm", "{{.Name}}", "--cpus", "{{user `cpus`}}"],
["modifyvm", "{{.Name}}", "--memory", "{{user `memory`}}"]
],
"http_directory": "./http/",
"boot_wait": "10s",
"boot_command": [
"<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
" /install/vmlinuz<wait>",
" noapic<wait>",
" preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg auto<wait>",
" locale=en_US<wait>",
" kbd-chooser/method=us<wait>",
" keyboard-configuration/modelcode=pc105<wait>",
" keyboard-configuration/layout=US<wait>",
" keyboard-configuration/variant=US<wait>",
" netcfg/get_hostname=ubuntu<wait>",
" fb=false <wait>",
" debconf/frontend=noninteractive<wait>",
" console-setup/ask_detect=false<wait>",
" initrd=/install/initrd.gz -- <wait>",
"<enter><wait>"
]
}
],
"post-processors": [
{
"type": "amazon-import",
"keep_input_artifact": true,
"s3_bucket_name": "{{user `s3_bucket_name`}}",
"ami_name": "ubuntu-16.04.6",
"license_type": "BYOL",
"tags": {
"Description": "Packer Import "
}
}
]
}
Check ISO_Ubuntu_Server_xenial_16.04.6.json
is validated via Packer
- Check the file is validate via Packer CLI
# packer validate ISO_Ubuntu_Server_xenial_16.04.6.json
Template validated successfully.
You can build right now.