python - 指定操作系统 - 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
            - python-setuptools
            - python-software-properties

目前支持的环境有UbuntuRed HatMac OS X。该剧本的当前编写方式仅适用于 Ubuntu (Debian)。我怎样才能让那部分代码根据操作系统执行?对于 Ubuntu,它是 apt,对于 Red Hat,它是 yum,对于 Mac OS X 酿造

最佳答案

通常的方法是通过检查 ansible_os_family 事实有条件地包含一个特定于操作系统系列的任务文件。

所以你的角色中可能有一个 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

redhat.yml 中我们有:

- name: install requirements
  yum: name={{item}} state=latest update_cache=true
  with_items:
      - gcc
      - python-dev
      - python-setuptools
      - python-software-properties

显然,这也允许您根据操作系统系列设置不同的依赖项列表。

如果你愿意,你也可以有条件地包括操作系统系列(或者任何你可以检查事实的东西)特定的变量,如下所示:

- name: Include OS-specific variables.
  include_vars: "{{ item }}"
  with_first_found:
    - ../vars/{{ ansible_distribution | lower }}.yml
    - ../vars/{{ ansible_os_family | lower }}.yml

然后像这样在 vars/debian.yml 中设置你的依赖列表:

python_dependencies:
  - gcc
  - python-dev
  - python-setuptools
  - python-software-properties

现在你的 tasks/debian.yml 看起来像:

- name: install requirements
  apt: name={{item}} state=latest update_cache=true
  with_items: python_dependencies

您可以通过查看源代码查看操作系统及其系列的列表 here它具有所有操作系统系列的命令:

# 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'
)

关于python - 指定操作系统 - Ansible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33762738/

相关文章:

python - 如何在Python中将一个项目的存储桶中的数据加载到另一个项目的表中?

mysql - 非常持久/opt/lampp/bin/mysql.server : 264: kill: No such process. Xampp/ubuntu 16.04

python - 从与 __init__ 导入同名的 python 模块导入对象

python - 将 csv 中的值插入到 mysql 时日期值不匹配

java进程构建器添加环境路径不起作用

c++ - 如何确定二进制文件是否在 Mac OS X 上被剥离?

objective-c - Mac OS 屏幕作为视频源

linux - 键盘快捷键创建 ubuntu

python - 在Ubuntu服务器上使用tmux运行tensorflow错误

python - Django 重定向 URL