0

I want Ansible to become root only if it is running on a specific OS, let's say, Ubuntu.

I've tried:

become: ansible_os_family == 'Darwin'
become: (ansible_os_family == 'Darwin')
become: {{ ansible_os_family }} == 'Darwin'
become: {{ ansible_os_family == 'Darwin' }}

neither of those worked.

Is it even possible? How can I make this work?

2

I don't think it's possible. As a workaround you can:

  • duplicate the task and put the condition on the os_family in a when clause

    - name: Become need 
      command:
      become: yes
      when: ansible_os_family == 'Darwin'
    
    - name: No become need
      command:
      when: ansible_os_family != 'Darwin'
    
  • group your host (also) by os at inventory level and define an ansible_become variable for os group:

    [Darwin]
    server1
    server2
    server3
    
    [Darwin:vars]
    ansible_become=true
    
1
  • Defining variables is probably the easiest way to do this. May 1 '16 at 22:58
0

According to #11381 this should work. All four of your attempts though are incorrect.

These two simply are strings, not evaluated conditions:

become: ansible_os_family == 'Darwin'
become: (ansible_os_family == 'Darwin')

While these two should produce a YAML error:

become: {{ ansible_os_family }} == 'Darwin'
become: {{ ansible_os_family == 'Darwin' }}

Have you tried this?:

become: "{{ ansible_os_family == 'Darwin' }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.