Hi everyone,
Apologies for the long post. I'm trying to run a playbook that does a
lookup on a template to populate a variable. This variable is then passed
into a role that the playbook is using. The results that I'm getting,
however, were not quite what I thought they would be. For context, I'm
using Ansible 1.9.2.
*Setup (greatly simplified)*
*Directory structure*
project
|----playbook
| |----stage
| | |----group_vars
| | | |----localhost.yml
| | |----inventory
| |----templates
| |----config.j2
| |----ansible.cfg
| |----site.yml
|----roles
|----ec2
|----tasks
|----main.yml
As you can see i have a project that contains a directory for roles and a
directory for my playbook. I organized the playbook to have different
profiles with staging being the one shown above with the idea being that I
will later add more profiles for production, qa, etc. Within the stage
directory I have my group_vars as well as the inventory. Here I also have a
templates directory that contains configuration files that don't
necessarily belong in a role or that need to be shared by several roles.
The important parts of each file are listed below.
*/playbook/stage/group_vars/localhost.yml*
kube_user_data: "{{ lookup('template', 'templates/config.j2') }}"
*/playbook/templates/config.j2*
Simply contains a cloud-config template that I use to setup CoreOS. Nothing
special here.
*/playbook/site.yml*
- name: AWS Test
hosts: localhost
connection: local
gather_facts: no
tasks:
- debug: var=kube_user_data
- name: AWS Setup
hosts: localhost
connection: local
gather_facts: no
roles:
- { role: ec2, user_data: "{{ kube_user_data }}" } #this has a lot more
variable assignments but this is just for simplicity
This file contains 2 plays. The first play was to verify that the
kube_user_data variable got the correct value.
*/roles/ec2/tasks/main.yml*
# Test
- debug: var=user_data
*Running the playbook*
I run the playbook from within the playbook directory like so:
lu4nm3$ cd project/playbook
lu4nm3$ ansible-playbook -i stage site.yml
*Output*
PLAY [AWS Test]
**************************************************************
TASK: [debug var=kube_user_data] ****************************************
ok: [localhost] => {
"var": {
"kubernetes_user_data": "#cloud-config\n\ncoreos:\n etcd2:\n
advertise-client-urls: http://$private_ipv4:4001\n data-dir:
/var/lib/etcd2\n discovery:
https://discovery.etcd.io/30254cae580177dc60aea05720eca87e\n
initial-advertise-peer-urls: http://$private_ipv4:7001\n
listen-client-urls: http://$private_ipv4:4001,http://localhost:4001\n
listen-peer-urls: http://$private_ipv4:7001,http://localhost:7001\n
flannel:\n etcd_endpoints: http://localhost:4001\n fleet:\n
etcd_servers: http://localhost:4001\n metadata: role=kubernetes\n
units:\n - name: docker.service\n command: start\n - name:
etcd2.service\n command: start\n - name: flanneld.service\n
command: start\n drop-ins:\n - name: 50-network-config.conf\n
content: |\n [Unit]\n
Requires=etcd2.service\n [Service]\n
ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config
'{\"Network\": \"10.244.0.0/16\", \"Backend\": {\"Type\": \"vxlan\"}}'\n
- name: fleet.service\n command: start\n"
}
}
PLAY [AWS Setup]
**************************************************************
TASK: [ec2 | debug var=user_data]
*********************************************
fatal: [localhost] => Failed to template {{ kubernetes_user_data }}: Failed
to template {{ lookup('template', 'templates/cloud-config-short.yml.j2') }}:
unable to read /home/ubuntu/temp/ansible/roles/aws/ec2/tasks/templates/cloud
-config-short.yml.j2
FATAL: all hosts have already failed -- aborting
As you can see from the run of the first play, Ansible was able to do a
template lookup and store the result into the kube_user_data variable.
Interestingly enough in the second play, when I pass this same variable
into my ec2 role to debug it, there is a failure. By the looks of the error
message, it seemed that Ansible tried to evaluate this variable from within
the role instead of from the playbook.
Is this expected behavior? I would have thought that Ansible would first
resolve any variable assignments within the playbook before passing them
into the roles that it's calling but that doesn't appear to be the case
here. If this is the expected behavior, is there any way to evaluate all
variable values before passing them into the role?
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/d8c41174-6dc5-4846-aad2-01fdef7b2601%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.