尝试基于操作系统拆分我的Ansible playbook
我有一个剧本,需要根据操作系统运行。 用例:假设有一个正在运行的服务 在Linux上,我们可以使用 systemctl状态应用程序.service 在命令和windows上,我们将使用 sc查询“ServiceName”|查找“RUNNING” 现在,我们必须根据上述命令的输出安装它,这要求我们根据操作系统隔离剧本 经典示例:基于操作系统创建目录尝试基于操作系统拆分我的Ansible playbook,ansible,Ansible,我有一个剧本,需要根据操作系统运行。 用例:假设有一个正在运行的服务 在Linux上,我们可以使用 systemctl状态应用程序.service 在命令和windows上,我们将使用 sc查询“ServiceName”|查找“RUNNING” 现在,我们必须根据上述命令的输出安装它,这要求我们根据操作系统隔离剧本 经典示例:基于操作系统创建目录 - name: Install QCA Agent on Linux targets hosts: all gather_facts: tru
- name: Install QCA Agent on Linux targets
hosts: all
gather_facts: true
remote_user: root
tasks:
- name: Create Directory for Downloading Qualys Cloud Agent
sudo: yes
sudo_user: root
file:
path: /usr/q1/
state: directory
owner: root
group: root
mode: 0777
recurse: no
- name: Create Directory for Downloading Qualys Cloud Agent
win_file:
path: c:\q1
state: directory
owner: Administrator
group: Administrator
mode: 0777
recurse: no
playbook只有在满足其中一个条件(无论是Windows还是Unix操作系统)时才会成功。我可以随时添加一个条件,该条件将根据以下条件进行提示:
when: ansible_distribution == 'Redhat' or ansible_distribution == 'CentOS'
然而,我想要实现的是基于一个条件,它应该触发我的playbook.yml文件
name: Load a variable file based on the OS type, or a default if not found. Using free-form to specify the file.
include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}.yaml"
- "{{ ansible_os_family }}.yaml"
- default.yaml
https://docs.ansible.com/ansible/2.5/modules/include_vars_module.html?highlight=with_first_found
我想知道是否有更好的例子来解释我可以实现的相同点,或者是否有其他方法来实现相同点
谢谢,您在Ansible文档中展示的示例几乎是最佳实践,在许多涉及多个操作系统的剧本(以及角色)中都很常见。如果您有不同的代码(而不是这里的变量示例),您将使用
include_tasks
而不是include_vars
,但概念是相同的