我希望 Ansible 仅在特定操作系统(比如 Ubuntu)上运行时才成为 root 权限。
我试过了:
become: ansible_os_family == 'Darwin'
become: (ansible_os_family == 'Darwin')
become: {{ ansible_os_family }} == 'Darwin'
become: {{ ansible_os_family == 'Darwin' }}
这些都不起作用。
这有可能吗?我该如何实现它?
答案1
我认为这是不可能的。作为解决方法,您可以:
重复任务并将条件放在子句
os_family
中when
- name: Become need command: become: yes when: ansible_os_family == 'Darwin' - name: No become need command: when: ansible_os_family != 'Darwin'
按库存级别的操作系统对主机进行分组,并
ansible_become
为操作系统组定义一个变量:[Darwin] server1 server2 server3 [Darwin:vars] ansible_become=true
答案2
根据#11381这应该可行。但您的四次尝试均不正确。
这两个只是字符串,而不是评估条件:
become: ansible_os_family == 'Darwin'
become: (ansible_os_family == 'Darwin')
虽然这两个应该会产生 YAML 错误:
become: {{ ansible_os_family }} == 'Darwin'
become: {{ ansible_os_family == 'Darwin' }}
你尝试过这个吗?:
become: "{{ ansible_os_family == 'Darwin' }}"