2015-03-10 00:11:57 -04:00
---
2017-06-14 21:04:16 -04:00
description: |
2020-03-18 18:46:47 -04:00
The ansible Packer provisioner allows Ansible playbooks to be run to provision
the machine.
2017-03-25 18:13:52 -04:00
layout: docs
2020-03-18 18:46:47 -04:00
page_title: Ansible - Provisioners
2020-04-02 19:39:47 -04:00
sidebar_title: Ansible (Remote)
2015-03-10 00:11:57 -04:00
---
# Ansible Provisioner
Type: `ansible`
2016-02-11 03:45:43 -05:00
The `ansible` Packer provisioner runs Ansible playbooks. It dynamically creates
an Ansible inventory file configured to use SSH, runs an SSH server, executes
`ansible-playbook`, and marshals Ansible plays through the SSH server to the
2018-07-18 12:42:15 -04:00
machine being provisioned by Packer.
2018-07-02 04:17:24 -04:00
2020-06-01 09:53:54 -04:00
-> **Note:** Any `remote_user` defined in tasks will be ignored. Packer
2018-10-26 20:02:51 -04:00
will always connect with the user given in the json config for this
provisioner.
2015-03-10 00:11:57 -04:00
## Basic Example
This is a fully functional template that will provision an image on
DigitalOcean. Replace the mock `api_token` value with your own.
2020-01-07 05:07:05 -05:00
Example Packer template:
2020-02-26 05:38:40 -05:00
2020-03-09 11:40:56 -04:00
```json
2015-03-10 00:11:57 -04:00
{
"provisioners": [
{
"type": "ansible",
2016-02-11 03:45:43 -05:00
"playbook_file": "./playbook.yml"
2015-03-10 00:11:57 -04:00
}
],
"builders": [
{
"type": "digitalocean",
"api_token": "6a561151587389c7cf8faa2d83e94150a4202da0e2bad34dd2bf236018ffaeeb",
"image": "ubuntu-14-04-x64",
"region": "sfo1"
2016-02-14 16:11:04 -05:00
}
2015-03-10 00:11:57 -04:00
]
}
```
2020-01-07 05:07:05 -05:00
Example playbook:
2020-03-31 18:47:06 -04:00
```yaml
2020-01-07 05:07:05 -05:00
---
# playbook.yml
2020-03-31 18:47:06 -04:00
- name: 'Provision Image'
2020-01-07 05:07:05 -05:00
hosts: default
become: true
tasks:
2020-03-31 18:47:06 -04:00
- name: install Apache
package:
name: 'httpd'
state: present
2020-01-07 05:07:05 -05:00
```
2015-03-10 00:11:57 -04:00
## Configuration Reference
Required Parameters:
2020-06-17 04:09:03 -04:00
@include 'provisioner/ansible/Config-required.mdx'
2015-03-10 00:11:57 -04:00
2016-02-04 21:40:17 -05:00
Optional Parameters:
2015-03-10 00:11:57 -04:00
2020-06-17 04:09:03 -04:00
@include '/provisioner/ansible/Config-not-required.mdx'
2020-03-18 18:46:47 -04:00
2020-03-23 20:02:12 -04:00
@include 'provisioners/common-config.mdx'
2019-04-08 10:05:04 -04:00
2017-04-22 11:24:37 -04:00
## Default Extra Variables
In addition to being able to specify extra arguments using the
`extra_arguments` configuration, the provisioner automatically defines certain
commonly useful Ansible variables:
2020-03-18 18:46:47 -04:00
- `packer_build_name` is set to the name of the build that Packer is running.
This is most useful when Packer is making multiple builds and you want to
distinguish them slightly when using a common playbook.
2017-04-22 11:24:37 -04:00
2020-03-18 18:46:47 -04:00
- `packer_builder_type` is the type of the builder that was used to create
the machine that the script is running on. This is useful if you want to
run only certain parts of the playbook on systems built with certain
builders.
2017-04-22 11:24:37 -04:00
2020-03-18 18:46:47 -04:00
- `packer_http_addr` If using a builder that provides an http server for file
transfer (such as hyperv, parallels, qemu, virtualbox, and vmware), this
will be set to the address. You can use this address in your provisioner to
download large files over http. This may be useful if you're experiencing
slower speeds using the default file provisioner. A file provisioner using
the `winrm` communicator may experience these types of difficulties.
2018-07-17 10:15:35 -04:00
2017-09-10 05:03:06 -04:00
## Debugging
2018-10-26 20:02:51 -04:00
To debug underlying issues with Ansible, add `"-vvvv"` to `"extra_arguments"`
to enable verbose logging.
2017-09-10 05:03:06 -04:00
2020-03-12 10:05:08 -04:00
```json
2017-09-10 05:03:06 -04:00
"extra_arguments": [ "-vvvv" ]
```
2015-03-10 00:11:57 -04:00
## Limitations
2016-09-25 18:57:02 -04:00
### Redhat / CentOS
2018-10-26 20:02:51 -04:00
Redhat / CentOS builds have been known to fail with the following error due to
`sftp_command`, which should be set to `/usr/libexec/openssh/sftp-server -e`:
2016-09-02 07:45:16 -04:00
2020-03-18 18:46:47 -04:00
```text
2016-09-02 07:45:16 -04:00
==> virtualbox-ovf: starting sftp subsystem
virtualbox-ovf: fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh", "unreachable": true}
```
2016-09-25 18:57:02 -04:00
2017-01-30 06:09:55 -05:00
### chroot communicator
2018-10-26 20:02:51 -04:00
Building within a chroot (e.g. `amazon-chroot`) requires changing the Ansible
2019-01-03 13:21:41 -05:00
connection to chroot and running Ansible as root/sudo.
2017-01-30 06:09:55 -05:00
2020-03-12 10:05:08 -04:00
```json
2017-01-30 06:09:55 -05:00
{
2017-03-25 18:13:52 -04:00
"builders": [
{
"type": "amazon-chroot",
"mount_path": "/mnt/packer-amazon-chroot",
"region": "us-east-1",
"source_ami": "ami-123456"
}
],
"provisioners": [
{
"type": "ansible",
"extra_arguments": [
"--connection=chroot",
"--inventory-file=/mnt/packer-amazon-chroot,"
],
"playbook_file": "main.yml"
}
]
2017-01-30 06:09:55 -05:00
}
```
2020-03-31 19:55:32 -04:00
### WinRM Communicator
2020-04-03 11:54:51 -04:00
There are two possible methods for using ansible with the WinRM communicator.
2016-09-25 18:57:02 -04:00
2020-03-31 19:55:32 -04:00
#### Method 1 (recommended)
The recommended way to use the WinRM communicator is to set `"use_proxy": false`
and let the Ansible provisioner handle the rest for you. If you
are using WinRM with HTTPS, and you are using a self-signed certificate you
will also have to set `ansible_winrm_server_cert_validation=ignore` in your
extra_arguments.
Below is a fully functioning Ansible example using WinRM:
2020-05-29 17:12:05 -04:00
```json
2020-03-31 19:55:32 -04:00
{
"builders": [
2020-05-29 17:12:05 -04:00
{
"type": "amazon-ebs",
"region": "us-east-1",
"instance_type": "t2.micro",
2020-03-31 19:55:32 -04:00
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "*Windows_Server-2012*English-64Bit-Base*",
"root-device-type": "ebs"
},
"most_recent": true,
"owners": "amazon"
},
2020-05-29 17:12:05 -04:00
"ami_name": "default-packer",
"user_data_file": "windows_bootstrap.txt",
"communicator": "winrm",
"force_deregister": true,
"winrm_insecure": true,
"winrm_username": "Administrator",
"winrm_use_ssl": true
}
],
2020-03-31 19:55:32 -04:00
"provisioners": [
{
2020-05-29 17:12:05 -04:00
"type": "ansible",
2020-03-31 19:55:32 -04:00
"playbook_file": "./playbook.yml",
"user": "Administrator",
"use_proxy": false,
2020-05-29 17:12:05 -04:00
"extra_arguments": ["-e", "ansible_winrm_server_cert_validation=ignore"]
2020-03-31 19:55:32 -04:00
}
]
}
```
Note that you do have to set the "Administrator" user, because otherwise Ansible
will default to using the user that is calling Packer, rather than the user
configured inside of the Packer communicator. For the contents of
windows_bootstrap.txt, see the winrm docs for the amazon-ebs communicator.
When running from OSX, you may see an error like:
2020-05-29 17:12:05 -04:00
```text
2020-03-31 19:55:32 -04:00
amazon-ebs: objc[9752]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.
amazon-ebs: objc[9752]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.
amazon-ebs: ERROR! A worker was found in a dead state
```
If you see this, you may be able to work around the issue by telling Ansible to
explicitly not use any proxying; you can do this by setting the template option
2020-05-29 17:12:05 -04:00
```json
2020-03-31 19:55:32 -04:00
"ansible_env_vars": ["no_proxy=\"*\""],
```
in the above Ansible template.
#### Method 2 (Not recommended)
If you want to use the Packer ssh proxy, then you need a custom Ansible
connection plugin and a particular configuration. You need a directory named
`connection_plugins` next to the playbook which contains a file named
packer.py` which implements the connection plugin. On versions of Ansible
before 2.4.x, the following works as the connection plugin:
2016-09-25 18:57:02 -04:00
2020-03-18 18:46:47 -04:00
```python
2016-09-25 18:57:02 -04:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.connection.ssh import Connection as SSHConnection
2018-07-03 11:12:12 -04:00
class Connection(SSHConnection):
''' ssh based connections for powershell via packer'''
transport = 'packer'
has_pipelining = True
become_methods = []
allow_executable = False
module_implementation_preferences = ('.ps1', '')
def __init__(self, *args, **kwargs):
super(Connection, self).__init__(*args, **kwargs)
```
2018-10-26 20:02:51 -04:00
Newer versions of Ansible require all plugins to have a documentation string.
You can see if there is a plugin available for the version of Ansible you are
using
[here](https://github.com/hashicorp/packer/tree/master/examples/ansible/connection-plugin).
2018-07-10 17:15:22 -04:00
2018-10-26 20:02:51 -04:00
To create the plugin yourself, you will need to copy all of the `options` from
the `DOCUMENTATION` string from the [ssh.py Ansible connection
plugin](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/ssh.py)
of the Ansible version you are using and add it to a packer.py file similar to
as follows
2018-07-03 11:12:12 -04:00
2020-03-18 18:46:47 -04:00
```python
2018-07-03 11:12:12 -04:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.connection.ssh import Connection as SSHConnection
DOCUMENTATION = '''
connection: packer
short_description: ssh based connections for powershell via packer
description:
2019-04-03 00:17:08 -04:00
- This connection plugin allows ansible to communicate to the target packer machines via ssh based connections for powershell.
2018-07-03 11:12:12 -04:00
author: Packer
version_added: na
options:
2018-07-10 17:15:22 -04:00
**** Copy ALL the options from
2018-07-03 11:12:12 -04:00
https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/ssh.py
for the version of Ansible you are using ****
'''
2016-09-25 18:57:02 -04:00
class Connection(SSHConnection):
''' ssh based connections for powershell via packer'''
transport = 'packer'
has_pipelining = True
become_methods = []
allow_executable = False
module_implementation_preferences = ('.ps1', '')
def __init__(self, *args, **kwargs):
super(Connection, self).__init__(*args, **kwargs)
```
2018-10-26 20:02:51 -04:00
This template should build a Windows Server 2012 image on Google Cloud
Platform:
2016-09-25 18:57:02 -04:00
2020-03-12 10:05:08 -04:00
```json
2016-09-25 18:57:02 -04:00
{
2017-03-25 18:13:52 -04:00
"variables": {},
"provisioners": [
{
2020-03-18 18:46:47 -04:00
"type": "ansible",
2017-03-25 18:13:52 -04:00
"playbook_file": "./win-playbook.yml",
"extra_arguments": [
2020-03-18 18:46:47 -04:00
"--connection",
"packer",
"--extra-vars",
"ansible_shell_type=powershell ansible_shell_executable=None"
2017-03-25 18:13:52 -04:00
]
}
],
"builders": [
{
"type": "googlecompute",
2020-03-31 19:55:32 -04:00
"account_file": "{{ user `account_file`}}",
2017-03-25 18:13:52 -04:00
"project_id": "{{user `project_id`}}",
"source_image": "windows-server-2012-r2-dc-v20160916",
"communicator": "winrm",
"zone": "us-central1-a",
"disk_size": 50,
"winrm_username": "packer",
"winrm_use_ssl": true,
"winrm_insecure": true,
"metadata": {
"sysprep-specialize-script-cmd": "winrm set winrm/config/service/auth @{Basic=\"true\"}"
2016-09-25 18:57:02 -04:00
}
2017-03-25 18:13:52 -04:00
}
]
2016-09-25 18:57:02 -04:00
}
2017-06-14 21:04:16 -04:00
```
2017-09-10 05:03:06 -04:00
2019-04-19 20:03:20 -04:00
-> **Warning:** Please note that if you're setting up WinRM for provisioning, you'll probably want to turn it off or restrict its permissions as part of a shutdown script at the end of Packer's provisioning process. For more details on the why/how, check out this useful blog post and the associated code:
2019-06-14 09:08:59 -04:00
https://cloudywindows.io/post/winrm-for-provisioning-close-the-door-on-the-way-out-eh/
2019-04-19 20:03:20 -04:00
2018-09-13 12:15:33 -04:00
### Post i/o timeout errors
2018-10-26 20:02:51 -04:00
If you see
`unknown error: Post http://<ip>:<port>/wsman:dial tcp <ip>:<port>: i/o timeout`
errors while provisioning a Windows machine, try setting Ansible to copy files
over [ssh instead of
sftp](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#envvar-ANSIBLE_SCP_IF_SSH).
2018-09-13 12:15:33 -04:00
2017-09-10 05:03:06 -04:00
### Too many SSH keys
2018-10-26 20:02:51 -04:00
SSH servers only allow you to attempt to authenticate a certain number of
times. All of your loaded keys will be tried before the dynamically generated
key. If you have too many SSH keys loaded in your `ssh-agent`, the Ansible
provisioner may fail authentication with a message similar to this:
2017-09-10 05:03:06 -04:00
2020-03-31 18:47:06 -04:00
```text
2017-09-10 05:03:06 -04:00
googlecompute: fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '[127.0.0.1]:62684' (RSA) to the list of known hosts.\r\nReceived disconnect from 127.0.0.1 port 62684:2: too many authentication failures\r\nAuthentication failed.\r\n", "unreachable": true}
```
2017-11-27 20:26:03 -05:00
To unload all keys from your `ssh-agent`, run:
2017-09-10 05:03:06 -04:00
2020-05-29 17:12:05 -04:00
```shell-session
2017-09-10 05:03:06 -04:00
$ ssh-add -D
```
2018-05-09 12:59:54 -04:00
### Become: yes
2018-10-26 20:02:51 -04:00
We recommend against running Packer as root; if you do then you won't be able
to successfully run your ansible playbook as root; `become: yes` will fail.
2019-05-01 14:00:07 -04:00
### Using a wrapping script for your ansible call
Sometimes, you may have extra setup that needs to be called as part of your
ansible run. The easiest way to do this is by writing a small bash script and
using that bash script in your "command" in place of the default
2020-03-18 18:46:47 -04:00
"ansible-playbook". For example, you may need to launch a Python virtualenv
2019-05-01 14:00:07 -04:00
before calling ansible. To do this, you'd want to create a bash script like
2020-05-29 17:12:05 -04:00
```shell
2019-05-01 14:00:07 -04:00
#!/bin/bash
source /tmp/venv/bin/activate && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 /tmp/venv/bin/ansible-playbook "$@"
```
The ansible provisioner template remains very simple. For example:
2020-03-12 10:05:08 -04:00
```json
2020-03-18 18:46:47 -04:00
{
"type": "ansible",
"command": "/Path/To/call_ansible.sh",
"playbook_file": "./playbook.yml"
}
2019-05-01 14:00:07 -04:00
```
Note that we're calling ansible-playbook at the end of this command and passing
all command line arguments through into this call; this is necessary for
making sure that --extra-vars and other important ansible arguments get set.
Note the quoting around the bash array, too; if you don't use quotes, any
arguments with spaces will not be read properly.
2019-11-04 17:19:41 -05:00
### Docker
When trying to use Ansible with Docker, you need to tweak a few options.
- Change the ansible_connection from "ssh" to "docker"
- Set a Docker container name via the --name option.
On a CI server you probably want to overwrite ansible_host with a random name.
Example Packer template:
2020-03-31 18:47:06 -04:00
```hcl
2019-11-04 17:19:41 -05:00
{
"variables": {
"ansible_host": "default",
"ansible_connection": "docker"
},
"builders":[
{
"type": "docker",
"image": "centos:7",
"commit": true,
"run_command": [ "-d", "-i", "-t", "--name", "{{user `ansible_host`}}", "{{.Image}}", "/bin/bash" ]
}
],
"provisioners": [
{
"type": "ansible",
"groups": [ "webserver" ],
"playbook_file": "./webserver.yml",
"extra_arguments": [
"--extra-vars",
"ansible_host={{user `ansible_host`}} ansible_connection={{user `ansible_connection`}}"
]
}
]
}
```
Example playbook:
2020-03-31 18:47:06 -04:00
```yaml
2019-11-04 17:19:41 -05:00
- name: configure webserver
hosts: webserver
tasks:
- name: install Apache
yum:
name: httpd
2020-01-07 05:07:05 -05:00
```
2020-03-31 19:55:32 -04:00
### Troubleshooting
If you are using an Ansible version >= 2.8 and Packer hangs in the
"Gathering Facts" stage, this could be the result of a pipelineing issue with
the proxy adapter that Packer uses. Setting `use_proxy: false,` in your
Packer config should resolve the issue. In the future we will default to setting
2020-04-03 11:54:51 -04:00
this, so you won't have to but for now it is a manual change you must make.