我正在考虑让我的所有Ansible角色交叉兼容,但我从Darwin(Mac OSX)开始。我差不多完成了,但是我遇到了一个残障我不完全确定如何在不使用command
,shell
,raw
的情况下到处走走,或每个发行版的独特任务......
- name: "Ensure key is present"
become: yes
become_user: root
apt_key:
keyserver: "{{ role_keyserver }}"
id: "{{ role_id }}"
state: present
如果不使用command
,shell
或raw
任务,我如何才能使上述Ansible任务与Darwin兼容?
答案 0 :(得分:0)
通常,您只需将实际的跨平台任务放在角色的tasks / main.yml中,然后在适当的位置执行包含操作系统特定任务列表的任务。
所以一个简单的例子可能就是这样:
- name: os specific vars
include_vars: "{{ ansible_os_family | lower }}"
- name: os specific tasks
include: "{{ ansible_os_family | lower }}.yml"
- name: enable service
service:
name: "{{ service_name }}"
state: started
enabled: true
然后你可能有你的操作系统特定文件,如:
service_name: foobar
service_yum_package: "{{ service_name }}"
- name: install service
yum:
name: "{{ service_yum_package }}"
service_name: foobaz
service_apt_package: "{{ service_name }}"
- name: install service
apt:
name: "{{ service_yum_package }}"
答案 1 :(得分:0)
Apt只是基于Debian的发行版的包管理器,因此您可能希望将a when statement添加到该块的末尾:
- name: "Ensure key is present"
become: yes
become_user: root
apt_key:
keyserver: "{{ role_keyserver }}"
id: "{{ role_id }}"
state: present
when: ansible_os_family == "Debian"
在Darwin主机上运行时,这将跳过任务。
如果你有一些类似的任务你需要运行,但是对于Darwin,那么你可以在a fact上类似地对其进行调节,因此它只能在Darwin主机上运行。