# Using pre\_tasks to install a role right before using it on a managed node?

**URL:** <https://forum.ansible.com/t/using-pre-tasks-to-install-a-role-right-before-using-it-on-a-managed-node/15638>\
**Category:** Ansible Project\
**Tags:** ubuntu, galaxy-ng\
**Created:** [July 9, 2014, 9:37pm UTC](https://forum.ansible.com/t/using-pre-tasks-to-install-a-role-right-before-using-it-on-a-managed-node/15638 "2014-07-09T21:37:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Rowlands](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Jonathan\_Rowlands](https://forum.ansible.com/u/Jonathan_Rowlands)\
**Post date:** [July 9, 2014, 9:37pm UTC](https://forum.ansible.com/t/using-pre-tasks-to-install-a-role-right-before-using-it-on-a-managed-node/15638/1 "2014-07-09T21:37:02Z")

</div>

I don’t know if this is a normal use-case but I am using [packer.io](http://packer.io) and the local ansible provisioner to build docker images. I wanted to use a pre\_task to actually install the role and then immediately use the role on a managed node. The problem is that _I think_ there is a sanity check that runs before the pre\_tasks actually gets called to make sure the referenced roles actually exist. So the play will fail with a message the role does not exist in which wouldn’t be true if the pre\_tasks ran first. Is my assumption of this sanity check correct and is there a recommended approach? I can have packer upload the role to my docker image if need be but I thought there might be a cleaner way.

See the following simple example playbook:

- name: Install openldap

hosts: all

sudo: True

pre\_tasks:

- name: Install the openldap role

command: ansible-galaxy install bennojoy.openldap\_server creates=/etc/ansible/roles/bennojoy.openldap\_server

roles:

- role: bennojoy.openldap\_server

openldap\_server\_domain\_name: [example.com](http://example.com)

openldap\_server\_rootpw: passme

openldap\_server\_enable\_ssl: true

openldap\_server\_country: US

openldap\_server\_state: Oregon

openldap\_server\_location: Portland

openldap\_server\_organization: IT

root@archvile:~/packer/openldap\_server-packer# sudo packer build -var-file=variables.json packer.json

docker output will be in this color.

==\> docker: Creating a temporary directory for sharing data…

==\> docker: Pulling Docker image: jgrowl/ubuntu-ansible

docker: Pulling repository jgrowl/ubuntu-ansible

==\> docker: Starting docker container…

docker: Run command: docker run -d -i -t -v /tmp/packer-docker258851345:/packer-files jgrowl/ubuntu-ansible /bin/bash

docker: Container ID: aa033de85e2009aea6d808ce9dc2ca5ae0dab7008b1de9de890955cf38d39780

==\> docker: Provisioning with Ansible…

docker: Creating Ansible staging directory…

docker: Creating directory: /tmp/packer-provisioner-ansible-local

docker: Uploading main Playbook file…

docker: Executing Ansible: cd /tmp/packer-provisioner-ansible-local && ansible-playbook /tmp/packer-provisioner-ansible-local/openldap\_server.yml -c local -i “127.0.0.1,”

docker: ERROR: cannot find role in /tmp/packer-provisioner-ansible-local/roles/bennojoy.openldap\_server or /tmp/packer-provisioner-ansible-local/bennojoy.openldap\_server or /etc/ansible/roles/bennojoy.openldap\_server

==\> docker: Killing the container: aa033de85e2009aea6d808ce9dc2ca5ae0dab7008b1de9de890955cf38d39780

Build ‘docker’ errored: Error executing Ansible: Non-zero exit status: 1

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 9, 2014, 9:43pm UTC](https://forum.ansible.com/t/using-pre-tasks-to-install-a-role-right-before-using-it-on-a-managed-node/15638/2 "2014-07-09T21:43:53Z")

</div>

not a sanity check, the playbook gets ‘compiled’ before running.​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 9, 2014, 10:49pm UTC](https://forum.ansible.com/t/using-pre-tasks-to-install-a-role-right-before-using-it-on-a-managed-node/15638/3 "2014-07-09T22:49:24Z")

</div>

yeah I can’t guarantee this will work even if it happened to work now.

A better idea might be to have a requirements file and run “ansible-galaxy” (the CLI) against the requirements\_file to slurp it all in. See ansible-galaxy --help for details.

If you have private roles, I recommend configuring the roles path in ansible.cfg, which it would be nice if this was exposed to Packer - not sure offhand if it is.
