With the current implementation of the Ansible provisioner, you can specify role_paths
as an array of strings, but you have to be explicit with every role that you want to upload to the remote machine. This becomes a hassle when you have multiple roles or start using role dependencies, as you have to explicitly include all dependencies, dependencies of dependencies, and so on down the line. This makes things very fragile.
I'd love to have the ability to say:
"provisioners": [
{
"type": "ansible-local",
"playbook_file": "ansible/playbooks/packer_jenkins_master.yml",
"role_paths": [
"ansible/roles/*"
]
}
],
...instead of:
"provisioners": [
{
"type": "ansible-local",
"playbook_file": "ansible/playbooks/packer_jenkins_master.yml",
"role_paths": [
"ansible/roles/common",
"ansible/roles/git",
"ansible/roles/java",
"ansible/roles/jenkins",
"ansible/roles/nginx"
]
}
],
This would be a really useful, and timesaving, enhancement. It would also reduce issues with the provisioner not being able to find roles. :+1:
My solution to this was to use a file provisioner to upload the entire ansible directory before the ansible provision is run. The following are common provisioners I run for ansible:
{
"type": "shell",
"script": "{{user `shared_packer_path`}}/scripts/install-ansible.sh",
"execute_command": "echo '{{user `vm_password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'"
},
{
"type": "shell",
"inline": [
"mkdir -p {{user `ansible_staging_directory`}}/files",
"mkdir -p {{user `ansible_staging_directory`}}/group_vars",
"mkdir -p {{user `ansible_staging_directory`}}/handlers",
"mkdir -p {{user `ansible_staging_directory`}}/host_vars",
"mkdir -p {{user `ansible_staging_directory`}}/roles",
"mkdir -p {{user `ansible_staging_directory`}}/templates",
"mkdir -p {{user `ansible_staging_directory`}}/vars"
]
},
{
"type": "file",
"source": "ansible/",
"destination": "{{user `ansible_staging_directory`}}/"
},
{
"type": "ansible-local",
"staging_directory": "{{user `ansible_staging_directory`}}",
"playbook_file": "ansible/site.yml",
"extra_arguments": [
"--extra-vars=\"param_hostname={{user `vm_name`}}\""
]
},
{
"type": "shell",
"script": "{{user `shared_packer_path`}}/scripts/remove-ansible.sh",
"execute_command": "echo '{{user `vm_password`}}' | {{.Vars}} sudo -E -S bash '{{.Path}}'"
},
@jgornick brilliant!!! I hadn't used the File Provisioner before, but this works perfectly:
"provisioners": [
{
"type": "shell",
"inline": [
"mkdir -p {{ user `ansible_staging_directory` }}"
]
},
{
"type": "file",
"source": "ansible/roles",
"destination": "{{ user `ansible_staging_directory` }}"
},
{
"type": "ansible-local",
"playbook_file": "ansible/playbooks/packer_jenkins_master.yml"
}
],
"variables": {
"ansible_staging_directory": "/tmp/packer-provisioner-ansible-local"
}
I still think it would be nice if role_paths
were more flexible, but this is a great intermediate solution. Thank you!
This globbing feature would be quite convenient. :+1:
Yes, with the current implementation the configuration is both in the packer template and the ansible files.
Created a pull request to enable uploading of the whole playbook directory including roles, variables etc.
https://github.com/mitchellh/packer/pull/1150
Would like to note the additional challenge of including ansible-galaxy roles. These are presently downloaded to a destination outside a project's ansible folder, usually at /usr/local/etc/ansible/roles
. Furthermore, there may be several roles in that location that aren't necessarily used in the active build. It's possible to list all project-related galaxy dependencies in a file (allows install via ansible-galaxy install -r <filename>
), but that's not necessarily a common practice yet. Still, a galaxy_file: <filename>
option might make sense.
+1 to a globbing solution. Having to make sure every role directory is included can be a hassle, especially when something has been accidentally excluded and it takes a while for Packer to rebuild to that point.
From the comments it is not clear if globs is implemented or the feature request is rejected
The issue could probably be renamed. It was resolved with #1150 (see above):
Created a pull request to enable uploading of the whole playbook directory including roles, variables etc.