On Fri, Feb 26, 2016 at 11:10 PM, Brian Coca <[email protected]> wrote:

> ​Y​
> ou cannot use jinja2 commands in vars file, the templates are not used in
> the vars file, they are just assigned as strings that later get recognized
> as templates and templated. But you can do this:
>
> ansible_become_method: "{{
> (ansible_distribution_release|version_compare('5.8', '>='))|ternary('doas',
> 'sudo') }}"
>

This looks elegant, thanks. In my environment when I run the playbook using
this configuration, it fails with the variable showing undefined:

$ ansible-playbook site.yml --limit sinoptik --tags snmpd

PLAY
***************************************************************************

TASK [setup]
*******************************************************************
fatal: [sinoptik]: FAILED! => {"failed": true, "msg": "Version comparison:
'ansible_distribution_release' is undefined"}

NO MORE HOSTS LEFT
*************************************************************

PLAY RECAP
*********************************************************************
sinoptik                   : ok=0    changed=0    unreachable=0    failed=1


With the suggested test in place in the group_vars file, it also prevents
running the setup module from running with same error:

$ ansible sinoptik -m setup
sinoptik | FAILED! => {
    "failed": true,
    "msg": "Version comparison: 'ansible_distribution_release' is undefined"
}

I thought this might have something to do with gather_facts or similar, but
I guess I'm stumped at this point.



##
ansible.cfg

[privilege_escalation]
become=True


## group_vars/openbsd
---
# OpenBSD group variables

gather_facts: True
ansible_python_interpreter: /usr/local/bin/python
sudoers_supergroup: wheel
snmpd_service_name: snmpd
# OpenBSD 5.8 and newer use doas(1) in place of sudo(8)
ansible_become_method: "{{
(ansible_distribution_release|version_compare('5.8', '>='))|ternary('doas',
'sudo') }}"


## hosts
[openbsd]
sinoptik ansible_ssh_host=sinoptik.sancho2k.net


## roles/snmpd/tasks/main.yml
---
# SNMP daemon setup tasks

- name: install snmpd config file
  template: src=snmpd.conf.j2 dest=/etc/snmpd.conf owner=root group=wheel
    mode=0640 backup=yes validate='snmpd -nf %s'
  notify: restart snmpd
  tags: snmpd

- name: enable snmpd service
  service: name=snmpd enabled=yes
  tags: snmpd


-- 
Darren Spruell
[email protected]

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAKVSOJUCBocEotMAyjJo4ZzwKtMWsjwvVxJynEfHv3dL0G04rA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to