Im trying to have ansible Update variable value base on OS distribution, but Im faling when I insert the rules in a template.
Any idea how to solve this?
---
- hosts: fansible,cansible
vars:
nagios_freebsd_load: -w 75,70,65 -c 90,85,80
nagios_centos_load: -w 15,10,5 -c 30,25,20
nagios_server_load: vars['nagios_{{ ansible_distribution|lower }}_load']
tasks:
- name: Print server load variable
debug: msg="{{ nagios_server_load }}"
- name: Print server load variable
debug: var="{{ nagios_server_load }}"
- name: Copying file
template:
src: ~/ansitrial/load.conf
dest: /tmp/load.conf
cat ~/ansitrial/load.conf
{{ nagios_server_load }}
TASK [Print server load variable] *******************************************************************************************************************************************
ok: [fansible] => {
"msg": "vars['nagios_freebsd_load']"
}
ok: [cansible] => {
"msg": "vars['nagios_centos_load']"
}
TASK [Print server load variable] *******************************************************************************************************************************************
ok: [fansible] => {
"vars['nagios_freebsd_load']": "-w 75,70,65 -c 90,85,80"
}
ok: [cansible] => {
"vars['nagios_centos_load']": "-w 15,10,5 -c 30,25,20"
}
TASK [Copying file] *********************************************************************************************************************************************************
changed: [cansible]
changed: [fansible]
PLAY RECAP ******************************************************************************************************************************************************************
cansible : ok=4 changed=1 unreachable=0 failed=0
fansible : ok=4 changed=1 unreachable=0 failed=0
remote@fansible:~ % cat /tmp/load.conf
vars['nagios_freebsd_load']
[remote@cansible ~]$ cat /tmp/load.conf
vars['nagios_centos_load']
To make it work use
nagios_server_load: "{{ vars['nagios_' ~ ansible_distribution|lower ~ '_load'] }}"