include_role question regarding variables

42 views
Skip to first unread message

Sven

unread,
Sep 8, 2020, 3:49:07 PM9/8/20
to Ansible Project
Hello Everyone,

i have a role "x" that uses two default vars:
(roles/x/defaults/main.yml)
server1var: server1
server2var: server2

based on these vars, tasks are executed on server1 or server2

now I have a playbook that executes some tasks and then includes role "x":

  tasks:
    - name: fetch file from remote server
      fetch:
        src: ...
        dest: ...
   - name: copy file to remote server
      copy:
        src: ...
        dest: ...

    - include_role:
        name: x

In this playbook the two vars from /roles/x/defaults/main.yml are also present with different values.
      vars:
        server1var: server3
        server2var: server4

Is it possible that the tasks executed by "include_role" take the vars from the current playbook?

 All tasks that gets executed by the include_role should be executed with the vars
        server1var: server3
        server2var: server4

instead of the default vars:
         server1var: server1
         server2var: server2

Can anyone tell me how i can do that?
is it just like this maybe?

    - include_role:
        name: x
      vars:
        server1var: server3
        server2var: server4

What is the best way to this?

Thank you for your time and help in advance.

 

Vladimir Botka

unread,
Sep 9, 2020, 5:01:37 AM9/9/20
to Sven, ansible...@googlegroups.com
On Tue, 8 Sep 2020 12:49:07 -0700 (PDT)
Sven <gebir...@gmail.com> wrote:

> ... role "x" that uses two default vars:
> (roles/x/defaults/main.yml)
> server1var: server1
> server2var: server2
>
> ... playbook ...
> vars:
> server1var: server3
> server2var: server4
> tasks:
> - include_role:
> name: x
> Is it possible that the tasks executed by "include_role" take the vars from
> the current playbook?

Yes. It's possible. For example

shell> cat roles/role_x/defaults/main.yml
server1var: server1
server2var: server2

shell> cat roles/role_x/tasks/main.yml
- debug:
var: server1var

shell> cat playbook.yml
- hosts: localhost
tasks:
- include_role:
name: role_x
- include_role:
name: role_x
vars:
server1var: server3

shell> ansible-playbook playbook.yml

TASK [include_role : role_x] ****

TASK [role_x : debug] ****
ok: [localhost] => { "server1var": "server1" }

TASK [include_role : role_x] ****

TASK [role_x : debug] ****
ok: [localhost] => { "server1var": "server3" }



> Can anyone tell me how i can do that?
> is it just like this maybe?
>
> - include_role:
> name: x
> vars:
> server1var: server3
> server2var: server4

Yes. This is the best way. FWIW, there are issues. See:
Modified "vars:" not visible inside include_role. #69799
https://github.com/ansible/ansible/issues/69799

--
Vladimir Botka

Sven

unread,
Sep 9, 2020, 7:29:39 AM9/9/20
to Ansible Project
Thank you very much for the help.

I have found out that the problem is caused by a random generator. Its not the include_role task itself (thats why I opened the question in first place)

I have two lists and building a new list excluding the items from list1 (which is something like "server1", "server2"...)

vars:
    list1:
       - "{{file_content.server1var}}"
       - "{{file_content.server2var}}"
    list2:
       - server1
       - server2
       - server3
       - server4
       - server5
       - server6
tasks:
    - name: generate newlist based on difference between list1 and list2
      set_fact:
        newlist: "{{ list2 | difference(list1) }}"

    - name: set fact random2
      set_fact:
        randomserver: "{{ newlist | random }}"

##This for example shows me server2 when i run the play one time###
     - name: show randomnode
      debug:
        msg: "{{randomnode}}"
...
##Now im using that randomnode variable for the include_role##
   - include_role:
        name: x
      vars:
        server1var: server3
        server2var:  "{{randomserver}}"

This is the task in role "x":

 - name: ping the servers
   ping:
   when: inventory_hostname == server1var or inventory_hostname == server2var

###Now im expecting that "server2var" will be "server2", but its kinda random what happens there###
###For example: sometimes its get executed on 4 servers, 3 servers, 2 servers, 1 server. The only Server that is consistent is server1var (whhich is "server1")###

Maybe you have an idea what might cause that problem?

I saw in your github examples, that you already used random filter.

Sven

unread,
Sep 9, 2020, 8:41:45 AM9/9/20
to Ansible Project
Well I actually found the problem.
I had to set run_once:yes in the following part:

   - name: set fact random2
      set_fact:
         randomserver: "{{ newlist | random}}"
      run_once: yes

Now I get the correct Servers

Anyway, Thank you for your help Mr. Botka.
Reply all
Reply to author
Forward
0 new messages