Python 指定OS-Ansible
我是Python 指定OS-Ansible,python,macos,ubuntu,package,ansible,Python,Macos,Ubuntu,Package,Ansible,我是Ansible的新手,因此我编写了一个小Ansible实用程序,为我正在编写的系统安装一些包依赖项: --- - hosts: all user: root tasks: - name: install requirements apt: name={{item}} state=latest update_cache=true with_items: - gcc - python-dev
Ansible
的新手,因此我编写了一个小Ansible实用程序,为我正在编写的系统安装一些包依赖项:
---
- hosts: all
user: root
tasks:
- name: install requirements
apt: name={{item}} state=latest update_cache=true
with_items:
- gcc
- python-dev
- python-setuptools
- python-software-properties
当前支持的环境有
Ubuntu
、redhat
和macosx
。目前编写本剧本的方式只能在Ubuntu(Debian)
中使用。如何根据操作系统执行代码的这一部分?对于Ubuntu
它是apt
,对于Red Hat
它是yum
和Mac OS X
brew
通常的方法是通过检查ansible\u OS\u系列
事实,有条件地包含一个特定于操作系统系列的任务文件
因此,您的角色中可能有一个main.yml
任务文件,看起来像:
# Arbitrary task here, not needed but the point is you can have any generic tasks directly in main.yml
- name: get the date
shell: `date`
register: date
- include: debian.yml
when: ansible_os_family == 'Debian'
- include: redhat.yml
when: ansible_os_family == 'RedHat'
然后在debian.yml
中,我们有:
- name: install requirements
apt: name={{item}} state=latest update_cache=true
with_items:
- gcc
- python-dev
- python-setuptools
- python-software-properties
- name: install requirements
yum: name={{item}} state=latest update_cache=true
with_items:
- gcc
- python-dev
- python-setuptools
- python-software-properties
在redhat.yml
中,我们有:
- name: install requirements
apt: name={{item}} state=latest update_cache=true
with_items:
- gcc
- python-dev
- python-setuptools
- python-software-properties
- name: install requirements
yum: name={{item}} state=latest update_cache=true
with_items:
- gcc
- python-dev
- python-setuptools
- python-software-properties
显然,这也允许您根据操作系统系列设置不同的依赖项列表
如果您愿意,您还可以有条件地包括OS系列(或者您可以检查事实的任何东西)特定的变量,如下所示:
- name: Include OS-specific variables.
include_vars: "{{ item }}"
with_first_found:
- ../vars/{{ ansible_distribution | lower }}.yml
- ../vars/{{ ansible_os_family | lower }}.yml
python_dependencies:
- gcc
- python-dev
- python-setuptools
- python-software-properties
然后在sayvars/debian.yml
中设置依赖项列表,如下所示:
- name: Include OS-specific variables.
include_vars: "{{ item }}"
with_first_found:
- ../vars/{{ ansible_distribution | lower }}.yml
- ../vars/{{ ansible_os_family | lower }}.yml
python_dependencies:
- gcc
- python-dev
- python-setuptools
- python-software-properties
现在,您的任务/debian.yml
如下所示:
- name: install requirements
apt: name={{item}} state=latest update_cache=true
with_items: python_dependencies
您可以查看操作系统及其系列的列表,方法是检查包含所有操作系统系列的源代码:
# A list with OS Family members
OS_FAMILY = dict(
RedHat = 'RedHat', Fedora = 'RedHat', CentOS = 'RedHat', Scientific = 'RedHat',
SLC = 'RedHat', Ascendos = 'RedHat', CloudLinux = 'RedHat', PSBM = 'RedHat',
OracleLinux = 'RedHat', OVS = 'RedHat', OEL = 'RedHat', Amazon = 'RedHat',
XenServer = 'RedHat', Ubuntu = 'Debian', Debian = 'Debian', Raspbian = 'Debian', Slackware = 'Slackware', SLES = 'Suse',
SLED = 'Suse', openSUSE = 'Suse', SuSE = 'Suse', SLES_SAP = 'Suse', Gentoo = 'Gentoo', Funtoo = 'Gentoo',
Archlinux = 'Archlinux', Manjaro = 'Archlinux', Mandriva = 'Mandrake', Mandrake = 'Mandrake',
Solaris = 'Solaris', Nexenta = 'Solaris', OmniOS = 'Solaris', OpenIndiana = 'Solaris',
SmartOS = 'Solaris', AIX = 'AIX', Alpine = 'Alpine', MacOSX = 'Darwin',
FreeBSD = 'FreeBSD', HPUX = 'HP-UX'
)
我没有mac,因此无法测试它,但如果您运行
ansible-I“mac主机”,则所有-m安装程序(其中“mac主机”是您的OS X设备的名称),这将提供有关“mac主机”的所有信息希望这台机器能给你一些事实,你可以用它来区分它和另一个操作系统。在OSX
上,ansible\u OS\u家族
是Darwin
一个更新的家族名称到发行版的映射是。@pentavalentcarbon可以建议用更新后的OS\u家族格言对答案进行编辑。