Ansible sudo mode depending on variable

I am writing an Ansible role to deploy a Docker container to a host. My inventory file contains CoreOS and Ubuntu servers, and Docker has been installed on Ubuntu without allowing its use by non-root users (aka I need sudo docker ...). CoreOS hosts don't need it.

I wanted to write a task and use sudo only if needed:

- name: Start container XX
  sudo: "{{ ansible_os_family and ansible_os_family == 'Debian' }}"
  docker:
    ...

Using the debug module, I can see this expression returns "True".

Directly using sudo: "True" works fine.

But using the given snippet doesn't work, the sudo mode is not enabled.

How can I conditionally use sudo to achieve my goal?

Answers


I'm not familiar with docker module itselve but I see one solution for that purpose. You need use conditional when with additional parameters in tasks:

- hosts: all
  tasks:
   - name: Debug message
     debug: msg="{{ ansible_os_family and ansible_os_family == 'Debian'  }}"

   - name: Rooted
     shell: service docker restart
     sudo: True
     when: "{{ ansible_os_family and ansible_os_family == 'Debian'  }}|default(false)|bool"

   - name: Not root
     shell: service docker restart
     # This will fail when sudo is needed.
     # When you remove 'not' in condition you will see that
     when: "not {{ ansible_os_family and ansible_os_family == 'Debian'  }}|default(false)|bool"

Need Your Help

kate editor, shortcut to "fold/unfold all"

ubuntu editor kate

kate editor (default kate package on Ubuntu 14.04).

Why is bottom-alligned UITextView getting 0 height when using autolayout for UITableViewCell?

ios objective-c uitableview uitextview

I am using autolayout for my UITableViewCell and my UITextView is getting zero height. It has the correct height in willDisplayCell but it becomes zero when cell is actually displayed.