add some docs for how to do complex custom commands

This commit is contained in:
Megan Marsh 2019-05-01 11:00:07 -07:00
parent afe760e635
commit c317679f52
1 changed files with 32 additions and 1 deletions

View File

@ -70,7 +70,9 @@ Optional Parameters:
``` ```
- `command` (string) - The command to invoke ansible. Defaults to - `command` (string) - The command to invoke ansible. Defaults to
`ansible-playbook`. `ansible-playbook`. If you would like to provide a more complex command,
for example, something that sets up a virtual environment before calling
ansible, take a look at the ansible wrapper guide below for inspiration.
- `empty_groups` (array of strings) - The groups which should be present in - `empty_groups` (array of strings) - The groups which should be present in
inventory file but remain empty. inventory file but remain empty.
@ -354,3 +356,32 @@ $ ssh-add -D
We recommend against running Packer as root; if you do then you won't be able 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. to successfully run your ansible playbook as root; `become: yes` will fail.
### 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
"ansible-playbook". For example, you may need to launch a Python virtualenv
before calling ansible. To do this, you'd want to create a bash script like
``` sh
#!/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:
``` json
{
"type": "ansible",
"command": "/Path/To/call_ansible.sh",
"playbook_file": "./playbook.yml"
}
```
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.