Only the vars associated with the first parent are getting defined even when the second parent is specified

I have the following:

  • name: router
  • name: request_router
    children: [router]
    vars:
    component_script: ‘requestrouter.sh’
    process_identifier: ‘log4j-dxad-web.xml’
  • name: memcached_router
    children: [router]
    vars:
    component_script: ‘memcached-router.sh’
    process_identifier: ‘log4j-memcached-router.xml’

When the following executes component_script equals requestrouter.sh when it should equal memcached-router.sh.

  • hosts: memcached_router
    user: root
    vars_files:

  • vars/globals.yml
    tasks:

  • include: task-to-execute.yml.

How do I get component_script to be set as memcached-router.sh when using hosts: memcached_router?

So I am having trouble what children means here, this is not a playbook keyword and the system will yell about that … So a little confused.

– Michael

Here is the issue reproduced. router is a child of both request_router and memcached_router. In order to kill request_router, “log4j-dxad-web.xml” is looked for in the process running on the ‘router’ host. In order to kill memcached_router, “log4j-memcached-router.xml” is looked for in the process running on the ‘router’ host.

(skytap_env)-bash-4.1$ cat hosts
[router]
router.releng.net ansible_ssh_host=172.24.233.2

[router:vars]

[request_router]

[request_router:vars]
process_identifier=log4j-dxad-web.xml

[request_router:children]
router

[memcached_router]

[memcached_router:vars]
process_identifier=log4j-memcached-router.xml

[memcached_router:children]
router

[matcher]
matcher.releng.net ansible_ssh_host=172.24.233.3

[matcher:children]
ad_matcher
memcached_matcher

[ad_matcher]

[ad_matcher:vars]
process_identifier=log4j-admatcher.xml

[memcached_matcher]

[memcached_matcher:vars]
process_identifier=log4j-memcached-matcher.xml

[realtime_system_components]

[realtime_system_components:vars]
rts_user=dxrts

[realtime_system_components:children]
request_router
ad_matcher
(skytap_env)-bash-4.1$

(skytap_env)-bash-4.1$ cat ansible/kill.yml

  • hosts: request_router
    user: dataxu
    vars_files:

  • vars/globals.yml

  • vars/realtime_system.yml
    tasks:

  • include: tasks/realtime_system/kill.yml

  • hosts: memcached_router
    user: dataxu
    vars_files:

  • vars/globals.yml

  • vars/realtime_system.yml
    tasks:

  • include: tasks/realtime_system/kill.yml
    (skytap_env)-bash-4.1$

(skytap_env)-bash-4.1$ cat ansible/tasks/realtime_system/kill.yml

  • name: Kill process if not stopped gracefully
    shell: pgrep -f {{ process_identifier }} && (echo “Killing”; pkill -9 -f {{ process_identifier }});
    ignore_errors: True

In the following, the second pgrep/kill command should be looking for log4j-memcached-router.xml

(skytap_env)-bash-4.1$ ansible-playbook -v -i hosts ansible/kill.yml

PLAY [request_router] *********************************************************

GATHERING FACTS ***************************************************************
ok: [router.releng.net]

TASK: [Kill process if not stopped gracefully] ********************************
failed: [router.releng.net] => {“changed”: true, “cmd”: "pgrep -f log4j-dxad-web.xml && (echo "Killing"; pkill -9 -f log4j-dxad-web.xml); ", “delta”: “0:00:00.035682”, “end”: “2013-12-14 23:32:22.911873”, “item”: “”, “rc”: -9, “start”: “2013-12-14 23:32:22.876191”}
stdout: 26041
Killing
…ignoring

PLAY [memcached_router] *******************************************************

TASK: [Kill process if not stopped gracefully] ********************************
failed: [router.releng.net] => {“changed”: true, “cmd”: "pgrep -f log4j-dxad-web.xml && (echo "Killing"; pkill -9 -f log4j-dxad-web.xml); ", “delta”: “0:00:00.030875”, “end”: “2013-12-14 23:32:28.376401”, “item”: “”, “rc”: -9, “start”: “2013-12-14 23:32:28.345526”}
stdout: 26074
Killing
…ignoring

PLAY RECAP ********************************************************************
router.releng.net : ok=3 changed=2 unreachable=0 failed=0

If in the hosts file, I delete everything having to do with realtime_system_components (see first “cat hosts” above) which shouldn’t have anything to do with the kill commands, the opposite log4j xml file is looked for both times.

(skytap_env)-bash-4.1$ cat hosts
[router]
router.releng.net ansible_ssh_host=172.24.233.2

[router:vars]

[request_router]

[request_router:vars]
process_identifier=log4j-dxad-web.xml

[request_router:children]
router

[memcached_router]

[memcached_router:vars]
process_identifier=log4j-memcached-router.xml

[memcached_router:children]
router

[matcher]
matcher.releng.net ansible_ssh_host=172.24.233.3

[matcher:children]
ad_matcher
memcached_matcher

[ad_matcher]

[ad_matcher:vars]
process_identifier=log4j-admatcher.xml

[memcached_matcher]

[memcached_matcher:vars]
process_identifier=log4j-memcached-matcher.xml

(skytap_env)-bash-4.1$ ansible-playbook -v -i hosts ansible/kill.yml

PLAY [request_router] *********************************************************

GATHERING FACTS ***************************************************************
ok: [router.releng.net]

TASK: [Kill process if not stopped gracefully] ********************************
failed: [router.releng.net] => {“changed”: true, “cmd”: "pgrep -f log4j-memcached-router.xml && (echo "Killing"; pkill -9 -f log4j-memcached-router.xml); ", “delta”: “0:00:00.036037”, “end”: “2013-12-14 23:35:58.908763”, “item”: “”, “rc”: -9, “start”: “2013-12-14 23:35:58.872726”}
stdout: 26152
Killing
…ignoring

PLAY [memcached_router] *******************************************************

TASK: [Kill process if not stopped gracefully] ********************************
failed: [router.releng.net] => {“changed”: true, “cmd”: "pgrep -f log4j-memcached-router.xml && (echo "Killing"; pkill -9 -f log4j-memcached-router.xml); ", “delta”: “0:00:00.026807”, “end”: “2013-12-14 23:36:04.260145”, “item”: “”, “rc”: -9, “start”: “2013-12-14 23:36:04.233338”}
stdout: 26185
Killing
…ignoring

PLAY RECAP ********************************************************************
router.releng.net : ok=3 changed=2 unreachable=0 failed=0

Mike

It’s hard to say what you’ve got in there wrong without seeing your entire playbook.

I’m pretty sure everything is in the post.

Re-post ansible/kill.yml:

-bash-4.1$ cat ansible/kill.yml

  • hosts: request_router
    user: dataxu
    vars_files:

  • vars/globals.yml

  • vars/realtime_system.yml
    tasks:

  • include: tasks/realtime_system/kill.yml

  • hosts: memcached_router
    user: dataxu
    vars_files:

  • vars/globals.yml

  • vars/realtime_system.yml
    tasks:

  • include: tasks/realtime_system/kill.yml
    -bash-4.1$

Yeah, I mean the task files to see what variables you are using, whether anything is hard coded, etc…

(This is probably best uploaded to gist.github.com)

https://gist.github.com/anonymous/7979176

-bash-4.1$ ansible-playbook -v -i hosts kill.yml

PLAY [request_router] *********************************************************

GATHERING FACTS ***************************************************************
ok: [router.releng.net]

TASK: [Kill process if not stopped gracefully] ********************************
failed: [router.releng.net] => {“changed”: true, “cmd”: "pgrep -f log4j-dxad-web.xml && (echo "Killing"; pkill -9 -f log4j-dxad-web.xml); ", “delta”: “0:00:00.034292”, “end”: “2013-12-15 17:31:13.982646”, “item”: “”, “rc”: -9, “start”: “2013-12-15 17:31:13.948354”}
stdout: 13506
Killing
…ignoring

PLAY [memcached_router] *******************************************************

TASK: [Kill process if not stopped gracefully] ********************************
failed: [router.releng.net] => {“changed”: true, “cmd”: "pgrep -f log4j-dxad-web.xml && (echo "Killing"; pkill -9 -f log4j-dxad-web.xml); ", “delta”: “0:00:00.035120”, “end”: “2013-12-15 17:31:18.841508”, “item”: “”, “rc”: -9, “start”: “2013-12-15 17:31:18.806388”}
stdout: 13523
Killing
…ignoring

PLAY RECAP ********************************************************************
router.releng.net : ok=3 changed=2 unreachable=0 failed=0

-bash-4.1$

Your problem is that the router has been added to both groups, and the variables overlap.

You probably want to move the variable into play level, with vars_files, or similar.

The idea that variables are sourced from all groups is quite intentional, hosts may group by behavior or geography – for instance, a machine may be a webserver and also be running out of a particular datacenter and it might be addressed as “webservers” and need to pull in the info from the other geography-oriented group, etc.

If the same variable is defined for multiple parents ansible should be smart enough to return the value for the parent which is being referred to. At the very least, if this behavior is not going to be corrected, ansible should fail when the same variable is defined for multiple parents. Arbitrarily returning the value associated for any parent is a bug. I’ll work around it.