Ansible sudo mode depending on variable -


i writing ansible role deploy docker container host. inventory file contains coreos , ubuntu servers, , docker has been installed on ubuntu without allowing use non-root users (aka need sudo docker ...). coreos hosts don't need it.

i wanted write task , use sudo if needed:

- name: start container xx   sudo: "{{ ansible_os_family , ansible_os_family == 'debian' }}"   docker:     ... 

using debug module, can see expression returns "true".

directly using sudo: "true" works fine.

but using given snippet doesn't work, sudo mode not enabled.

how can conditionally use sudo achieve goal?

i'm not familiar docker module itselve see 1 solution purpose. need use conditional when additional parameters in tasks:

- hosts:   tasks:    - name: debug message      debug: msg="{{ ansible_os_family , ansible_os_family == 'debian'  }}"     - name: rooted      shell: service docker restart      sudo: true      when: "{{ ansible_os_family , ansible_os_family == 'debian'  }}|default(false)|bool"     - name: not root      shell: service docker restart      # fail when sudo needed.      # when remove 'not' in condition see      when: "not {{ ansible_os_family , ansible_os_family == 'debian'  }}|default(false)|bool" 

Comments