Ansible dynamic variable

18 views
Skip to first unread message

Kethzer Docteur

unread,
Jul 10, 2017, 11:46:55 AM7/10/17
to Ansible Project
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']

Kai Stian Olstad

unread,
Jul 10, 2017, 2:09:05 PM7/10/17
to ansible...@googlegroups.com
On 10. juli 2017 00:50, Kethzer Docteur wrote:
>
>
> 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']

To make it work use
nagios_server_load: "{{ vars['nagios_' ~ ansible_distribution|lower ~
'_load'] }}"


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages