【问题标题】:Specifying the OS - Ansible指定操作系统 - Ansible
【发布时间】:2016-02-19 03:59:32
【问题描述】:

我是 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,是brew

【问题讨论】:

    标签: python macos ubuntu package ansible


    【解决方案1】:

    通常的方法是通过检查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'
    )
    

    【讨论】:

    • 我没有 mac,所以无法测试它,但如果你运行 ansible -i "mac-host," all -m setup(其中“mac-host”是你的 OS X 机器的名称),这将提供所有事实关于你的“mac-host”机器,希望能给你一些事实,你可以用它来区分它和另一个操作系统。
    • OS X ansible_os_familyDarwin
    • 姓氏到分布的更新映射是here
    • @pentavalentcarbon 随时建议使用更新的 OS_FAMILY 字典对答案进行编辑。
    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多