How to set a variable in vars/main.yml conditionally?

342 views
Skip to first unread message

ZillaYT

unread,
Sep 1, 2016, 12:02:09 PM9/1/16
to Ansible Project
I'm writing a role to configure syslog, and of course the service is different between Linux instances. How do I do this in var/main.yml?

# pseudo-code in vars/main.yml
if Redhat7.x
  syslog_service
: rsyslog
else if RedHat6.x
  syslog_service
: syslog
end


So then I can just do this in tasks/main.yml

- name: Ensure syslog is running
  service
: name="{{ syslog_service }}" state=running enabled=yes


Thanks!

Kai Stian Olstad

unread,
Sep 1, 2016, 1:45:02 PM9/1/16
to ansible...@googlegroups.com
On 01. sep. 2016 18:02, ZillaYT wrote:
> I'm writing a role to configure syslog, and of course the service is
> different between Linux instances. How do I do this in var/main.yml?

The directory must be vars not var.


> # pseudo-code in vars/main.yml
> if Redhat7.x
> syslog_service: rsyslog
> else if RedHat6.x
> syslog_service: syslog
> end

It's not possible, vars/main.yml can only contain variable definitions,
not logic.


> So then I can just do this in tasks/main.yml
>
> - name: Ensure syslog is running
> service: name="{{ syslog_service }}" state=running enabled=yes

You must split it in two tasks.
ansible_os_family and ansible_lsb.major_release values is probably wrong
so you need to find the correct ones.

- name: Ensure syslog is running on Redhat 6.x
service: name=syslog state=running enabled=yes
when: ansible_os_family == "RedHat" and ansible_lsb.major_release == "6"

- name: Ensure syslog is running on Redhat 7.x
service: name=rsyslog state=running enabled=yes
when: ansible_os_family == "RedHat" and ansible_lsb.major_release == "7"

--
Kai Stian Olstad

ZillaYT

unread,
Sep 1, 2016, 1:54:25 PM9/1/16
to Ansible Project, ansible-pr...@olstad.com
Having a lot of -name/when task blocks is just ugly infrastructure. I have to think of a better way. There's GOT to be a better way.

Allen Sanabria

unread,
Sep 1, 2016, 2:04:11 PM9/1/16
to Ansible Project, ansible-pr...@olstad.com
include_vars is your friend.

For instance in tasks/main.yml you can say..
- include_vars: RedHat6.yml
 when: ansible_os_family == "RedHat" and ansible_lsb.major_release == "6"

- include_vars: RedHat7.yml
 when: ansible_os_family == "RedHat" and ansible_lsb.major_release == "7"

Allen Sanabria

unread,
Sep 1, 2016, 2:07:23 PM9/1/16
to ansible...@googlegroups.com, ansible-pr...@olstad.com
include_vars is your friend.

For instance in tasks you can say..

```yaml
- include_vars: RedHat6.yml
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release == "6"

- include_vars: RedHat7.yml
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release == "7"
```

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5b1c97fc-2e65-4c7d-a32c-911f1ddeedca%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Matt Martz

unread,
Sep 1, 2016, 2:08:12 PM9/1/16
to ansible...@googlegroups.com, ansible-pr...@olstad.com
Additionally you can make syslog_service a dictionary value with keys for the different versions:

vars/main.yml

syslog_service:
    "6": syslog
    "7": rsyslog

Then just do:

- name: Ensure syslog is running
  service: name="{{ syslog_service[ansible_distribution_major_version] }}" state=running enabled=yes

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

ZillaYT

unread,
Sep 1, 2016, 2:30:21 PM9/1/16
to Ansible Project, ansible-pr...@olstad.com
Much better solution. Thanks!

I ended up doing this

- include_vars: "roles/syslog/vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages