Problem with Conditional Imports

56 views
Skip to first unread message

thomas....@berenberg.de

unread,
Nov 11, 2015, 10:48:09 AM11/11/15
to Ansible Project
Hello,
I have a problem with conditional variable imports. I am trying to set a variable based on operatingsystem release. For 'Solaris10' I want one value and for all others I want another another value.

In this example I have two servers:

$ ansible server1 -m setup | grep -E 'ansible_os_family|ansible_distribution_version'
       
"ansible_distribution_version": "10",
       
"ansible_os_family": "Solaris",
$ ansible server2
-m setup | grep -E 'ansible_os_family|ansible_distribution_version'
       
"ansible_distribution_version": "11.2",
       
"ansible_os_family": "Solaris",


I am trying to mimic the construct from this example: http://docs.ansible.com/ansible/playbooks_conditionals.html#conditional-imports

$ cat vars/defaults.yml
---
homeprefix
: /blah


$ cat vars/
Solaris10.yml
---
homeprefix
: /export/home


$ cat debug
.yml
---
- hosts: dbservere1:dbservere3
  vars_files
:
   
- [ "vars/{{ ansible_os_family }}{{ ansible_distribution_version}}.yml", "vars/defaults.yml" ]


  tasks
:


 
- debug: msg="vars/{{ ansible_os_family }}{{ ansible_distribution_version }}.yml"
 
- debug: msg={{ homeprefix }}


I was now expecting to have "server1" import the file "vars/Solaris10.yml" and "server2" import the file "vars/defaults.yml" but instead both servers are importing the "vars/defaults.yml".
  
$ ansible-playbook debug.yml


PLAY
[server1:server2] **************************************************


GATHERING FACTS
***************************************************************
ok
: [server1]
ok
: [server2]


TASK
: [debug msg="vars/{{ ansible_os_family }}{{ ansible_distribution_version }}.yml"] ***
ok
: [server1] => {
   
"msg": "vars/Solaris10.yml"
}
ok
: [server2] => {
   
"msg": "vars/Solaris11.2.yml"
}


TASK
: [debug msg={{ homeprefix }}] ********************************************
ok
: [server1] => {
   
"msg": "/blah"
}
ok
: [server2] => {
   
"msg": "/blah"
}


PLAY RECAP
********************************************************************
server1                
: ok=3    changed=0    unreachable=0    failed=0
server2                
: ok=3    changed=0    unreachable=0    failed=0




Can someone point me to my error?

Cheers,
Thomas

Brian Coca

unread,
Nov 11, 2015, 11:29:34 AM11/11/15
to Ansible Project
what version of ansible are you using?

--
Brian Coca

thomas....@berenberg.de

unread,
Nov 12, 2015, 2:23:28 AM11/12/15
to Ansible Project
Sorry, I am using Ansible 1.9.4 on CentOS 7.1.

/ Thomas

Thomas Willert

unread,
Nov 12, 2015, 9:23:18 AM11/12/15
to Ansible Project
I havent managed to understand my problem but I have been able to use this workaround using the "include_vars" module instead:

---
- hosts: server1:server2

  tasks:

  - include_vars: "{{ item }}"
    with_first_found:
      - "vars/{{ ansible_os_family }}{{ ansible_distribution_version }}.yml"
      - "vars/defaults.yml"

  - debug: msg={{ homeprefix }}


/ Thomas
Reply all
Reply to author
Forward
0 new messages