handlers and include files with variables in filenames

548 views
Skip to first unread message

Mauricio Tavares

unread,
Jun 8, 2017, 6:20:54 AM6/8/17
to ansible...@googlegroups.com
So I am running ansible 2.3.0.0. What I want to do is to have my
handlers/main.yml file to include another file (where some handlers
are defined) whose name depends on a variable defined somewhere else.
i.e.

- include: stuff"{{ myvariable }}".yml

where myvariable is defined in the file group_vars/all. I also have

[defaults]
handler_includes_static = True

defined in my ansible.cfg file per
https://github.com/ansible/ansible/issues/13485. But when I run it I
get the following message:

ERROR! Error when evaluating variable in include name: stuff"{{
myvariable }}".yml.

When using static includes, ensure that any variables used in their
names are defined in
vars/vars_files or extra-vars passed in from the command line. Static
includes cannot use
variables from inventory sources like group or host vars.

Where does it want me to define myvariable then? Somewhere in, say,
roles/role_in_question/vars/main.yml?

Jeff Li

unread,
Jun 26, 2017, 10:17:06 PM6/26/17
to Ansible Project
I solved the problem by  using vars_files directive in playbook. The  directory structure looks like

playbook
.yml
vars
/main.yml
roles
/my-roles/...

The playbook.yml  looks like

---
  hosts
: all
  vars_files
:
   
- vars/main.yml
  tasks
:
   
....
  roles
:
   

Message has been deleted

Mauricio Tavares

unread,
Jul 5, 2017, 11:00:19 PM7/5/17
to ansible...@googlegroups.com
On Fri, Jun 23, 2017 at 3:13 AM, Jeff Li <jaf...@gmail.com> wrote:
>
> I solved the problem by using vars_files directive in playbook. The directory structure looks like
>
> playbook.yml
> vars/main.yml
> roles/my-roles/...
>
> The playbook.yml looks like
>
> ---
> hosts: all
> vars_files:
> - vars/main.yml
> tasks:
> ....
> roles:
>
I hate to say but I still do not know how are you selecting
which handlers to use.
>
> On Thursday, June 8, 2017 at 6:20:54 PM UTC+8, Mauricio Tavares wrote:
>>
>> So I am running ansible 2.3.0.0. What I want to do is to have my
>> handlers/main.yml file to include another file (where some handlers
>> are defined) whose name depends on a variable defined somewhere else.
>> i.e.
>>
>> - include: stuff"{{ myvariable }}".yml
>>
>> where myvariable is defined in the file group_vars/all. I also have
>>
>> [defaults]
>> handler_includes_static = True
>>
>> defined in my ansible.cfg file per
>> https://github.com/ansible/ansible/issues/13485. But when I run it I
>> get the following message:
>>
>> ERROR! Error when evaluating variable in include name: stuff"{{
>> myvariable }}".yml.
>>
>> When using static includes, ensure that any variables used in their
>> names are defined in
>> vars/vars_files or extra-vars passed in from the command line. Static
>> includes cannot use
>> variables from inventory sources like group or host vars.
>>
>> Where does it want me to define myvariable then? Somewhere in, say,
>> roles/role_in_question/vars/main.yml?
>
> --
> 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 ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b268dafb-3a55-4a24-865e-bf9ccd482d2d%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Jeff Li

unread,
Jul 6, 2017, 8:59:58 AM7/6/17
to Ansible Project
Hope it is clear.

The directory structure looks like this


playbook
.yml
vars
/main.yml
roles
/my-roles/handlers/main.yml
roles
/my-roles/handlers/ocata.yml
roles
/my-roles/handlers/kilo.yml



The roles/my-roles/handlers/main.yml looks like this

---
- include: "{{ codename }}".yml


The variable codename is defined in  vars/main.yml 


codename
: ocata

Mauricio Tavares

unread,
Jul 8, 2017, 7:11:24 AM7/8/17
to ansible...@googlegroups.com
On Thu, Jul 6, 2017 at 8:59 AM, Jeff Li <jaf...@gmail.com> wrote:
> Hope it is clear.
>
> The directory structure looks like this
>
>
> playbook.yml
> vars/main.yml
> roles/my-roles/handlers/main.yml
> roles/my-roles/handlers/ocata.yml
> roles/my-roles/handlers/kilo.yml
>
>
>
> The roles/my-roles/handlers/main.yml looks like this
>
> ---
> - include: "{{ codename }}".yml
>
>
> The variable codename is defined in vars/main.yml
>
>
> codename: ocata
>

That looks very similar to what I am doing (test.yml is my playbook):

raub@desktop:~/dev/ansible$ cat test.yml
---
# file: test.yml
#

- hosts:
- test

roles:
- test
raub@desktop:~/dev/ansible$ cat group_vars/test
---
handlerpath: duck.yml
raub@desktop:~/dev/ansible$ cat roles/test/tasks/main.yml
---
- name: First we need to know the path for the handler file
debug:
msg: "Hander Path: {{ handlerpath }}"

- name: So which handler file are we using today?
shell: "echo Handler path {{ handlerpath }} "
notify: which handler
raub@desktop:~/dev/ansible$ cat roles/test/handlers/main.yml
---
# test/handlers/main.yml

- include: "{{ handlerpath }}"

# - name: which handler
# debug:
# msg: "Hander Path: {{ handlerpath }}"

raub@desktop:~/dev/ansible$ cat roles/test/handlers/duck.yml
---
- name: which handler
debug:
msg: "Path = {{ handlerpath }}"
raub@desktop:~/dev/ansible$

The commented debug line is in both the main handler and task files
for me to test whether the variable I created gets there. So if I go
to the main handler file and comment the include statement and
uncomment the debug stuff, I get

raub@desktop:~/dev/ansible$ ansible-playbook -i hosts test.yml

PLAY [test] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [ansibletest]

TASK [test : First we need to know the path for the handler file] **************
ok: [ansibletest] => {
"changed": false,
"msg": "Hander Path: duck.yml"
}

TASK [test : So which handler file are we using today?] ************************
changed: [ansibletest]

RUNNING HANDLER [test : which handler] *****************************************
ok: [ansibletest] => {
"changed": false,
"msg": "Hander Path: duck.yml"
}

PLAY RECAP *********************************************************************
ansibletest : ok=4 changed=1 unreachable=0 failed=0

raub@desktop:~/dev/ansible$

But if I run as shown above, it does not seem to be able to include my duck:

[....]
TASK [Gathering Facts] *********************************************************
ok: [ansibletest]

TASK [test : First we need to know the path for the handler file] **************
ok: [ansibletest] => {
"changed": false,
"msg": "Hander Path: duck.yml"
}

TASK [test : So which handler file are we using today?] ************************
ERROR! The requested handler 'which handler' was not found in either
the main handlers list nor in the listening handlers list
raub@desktop:~/dev/ansible$

First thing I did was to check if there was no extra blank spaces
around 'which handler'in duck.yml. BTW, just to be on the safe side, I
did then try moving group_vars/test to roles/test/vars/main.yml to see
if it makes any difference (and be closer to your setup). Outcome was
the same. And I am running ansible 2.3.0.0.
> https://groups.google.com/d/msgid/ansible-project/580fbe81-65c7-432c-86b8-a7ffb532c193%40googlegroups.com.

Jeff Li

unread,
Jul 17, 2017, 8:23:27 PM7/17/17
to ansible...@googlegroups.com
Filename variables in static inclusion could only be defined in 3 places , any one is ok.

1. extra vars from command line
2.  in vars section of the playbook, not the vars or defaults directory of the role
3.  in vars_file section of the playbook

I don't see non used from your description.

I have published a repo in github to show the usage of all the ways to define the filename variables.  Basically, you should pay attention to the 2 commits


> email to ansible-project+unsubscribe@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/F5ee5LkUPnw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAHEKYV721oMAVhGE8udo9OEP3PpBqoAp5bD2TrKFJ%3DO0skedKg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages