使用 Ansible 管理 BSD 主机

管理 BSD 机器与管理其他类 Unix 机器不同。如果您管理运行 BSD 的节点,请查看以下主题。

连接到 BSD 节点

Ansible 默认使用 OpenSSH 连接到托管节点。如果您使用 SSH 密钥进行身份验证,这在 BSD 上有效。但是,如果您使用 SSH 密码进行身份验证,Ansible 依赖于 sshpass。大多数版本的 sshpass 不能很好地处理 BSD 登录提示,因此在针对 BSD 机器使用 SSH 密码时,请使用 paramiko 进行连接,而不是 OpenSSH。您可以在 ansible.cfg 中全局设置它,也可以将其设置为清单/组/主机变量。例如

[freebsd]
mybsdhost1 ansible_connection=paramiko

引导 BSD

Ansible 默认情况下是无代理的,但是,它需要托管节点上的 Python。只有 raw 模块可以在没有 Python 的情况下运行。虽然此模块可用于引导 Ansible 并安装 Python 在 BSD 变体上(见下文),但它非常有限,需要使用 Python 才能充分利用 Ansible 的功能。

以下示例安装了 Python,其中包括 Ansible 的完整功能所需的 json 库。在您的控制机上,您可以对大多数版本的 FreeBSD 执行以下操作

ansible -m raw -a "pkg install -y python" mybsdhost1

或者对于 OpenBSD

ansible -m raw -a "pkg_add python%3.8"

完成此操作后,您现在可以使用除 raw 模块以外的其他 Ansible 模块。

注意

此示例演示了在 FreeBSD 上使用 pkg 和在 OpenBSD 上使用 pkg_add,但是,您应该能够用您使用的 BSD 的适当包工具替换它;包名称也可能不同。有关您要安装的确切 Python 包名称,请参考您使用的 BSD 变体的包列表或文档。

设置 Python 解释器

为了支持各种类 Unix 操作系统和发行版,Ansible 不能总是依赖于现有环境或 env 变量来定位正确的 Python 二进制文件。默认情况下,模块指向 /usr/bin/python,因为这是最常见的位置。在 BSD 变体上,此路径可能不同,因此建议告知 Ansible 二进制文件的位置。见 INTERPRETER_PYTHON。例如,设置 ansible_python_interpreter 清单变量

[freebsd:vars]
ansible_python_interpreter=/usr/local/bin/python
[openbsd:vars]
ansible_python_interpreter=/usr/local/bin/python3.8

FreeBSD 包和端口

在 FreeBSD 中,不能保证默认情况下安装 /usr/local/bin/python 可执行文件或指向可执行文件的链接。关于 Ansible,远程主机的最佳实践是至少安装 Ansible 支持的 Python 版本,例如 lang/python38,以及两个元端口 lang/python3lang/python。引用自 */usr/ports/lang/python3/pkg-descr*

This is a meta port to the Python 3.x interpreter and provides symbolic links
to bin/python3, bin/pydoc3, bin/idle3 and so on to allow compatibility with
minor version agnostic Python scripts.

引用自 */usr/ports/lang/python/pkg-descr*

This is a meta port to the Python interpreter and provides symbolic links
to bin/python, bin/pydoc, bin/idle and so on to allow compatibility with
version agnostic python scripts.

因此,将安装以下包

shell> pkg info | grep python
python-3.8_3,2                 "meta-port" for the default version of Python interpreter
python3-3_3                    Meta-port for the Python interpreter 3.x
python38-3.8.12_1              Interpreted object-oriented programming language

以及以下可执行文件和链接

shell> ll /usr/local/bin/ | grep python
lrwxr-xr-x  1 root  wheel       7 Jan 24 08:30 python@ -> python3
lrwxr-xr-x  1 root  wheel      14 Jan 24 08:30 python-config@ -> python3-config
lrwxr-xr-x  1 root  wheel       9 Jan 24 08:29 python3@ -> python3.8
lrwxr-xr-x  1 root  wheel      16 Jan 24 08:29 python3-config@ -> python3.8-config
-r-xr-xr-x  1 root  wheel    5248 Jan 13 01:12 python3.8*
-r-xr-xr-x  1 root  wheel    3153 Jan 13 01:12 python3.8-config*

INTERPRETER_PYTHON_FALLBACK

从 2.8 版开始,Ansible 提供了一个有用的变量 ansible_interpreter_python_fallback 来指定要搜索 Python 的路径列表。见 INTERPRETER_PYTHON_FALLBACK。将搜索此列表,并使用找到的第一个项目。例如,下面的配置将使上一节中元端口的安装变得多余,也就是说,如果您不安装 Python 元端口,列表中的前两项将被跳过,并且将发现 /usr/local/bin/python3.8

ansible_interpreter_python_fallback=['/usr/local/bin/python', '/usr/local/bin/python3', '/usr/local/bin/python3.8']

您可以使用此变量,并通过较低版本的 Python 延长它,并将它放在例如 group_vars/all 中。然后,如果需要,在 group_vars/{group1, group2, ...} 中为特定组和在 host_vars/{host1, host2, ...} 中为特定主机覆盖它。见 变量优先级:我应该把变量放在哪里?.

调试 Python 的发现

例如,给定清单

shell> cat hosts
[test]
test_11
test_12
test_13

[test:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=true
ansible_become_user=root
ansible_become_method=sudo
ansible_interpreter_python_fallback=['/usr/local/bin/python', '/usr/local/bin/python3', '/usr/local/bin/python3.8']
ansible_perl_interpreter=/usr/local/bin/perl

下面的剧本

shell> cat playbook.yml
- hosts: test_11
  gather_facts: false
  tasks:
    - command: which python
      register: result
    - debug:
        var: result.stdout
    - debug:
        msg: |-
          {% for i in _vars %}
          {{ i }}:
            {{ lookup('vars', i)|to_nice_yaml|indent(2) }}
          {% endfor %}
      vars:
        _vars: "{{ query('varnames', '.*python.*') }}"

显示详细信息

shell> ansible-playbook -i hosts playbook.yml

PLAY [test_11] *******************************************************************************

TASK [command] *******************************************************************************
[WARNING]: Platform freebsd on host test_11 is using the discovered Python interpreter at
/usr/local/bin/python, but future installation of another Python interpreter could change the
meaning of that path. See https://docs.ansible.org.cn/ansible-
core/2.12/reference_appendices/interpreter_discovery.html for more information.
changed: [test_11]

TASK [debug] *********************************************************************************
ok: [test_11] =>
  result.stdout: /usr/local/bin/python

TASK [debug] *********************************************************************************
ok: [test_11] =>
  msg: |-
    ansible_interpreter_python_fallback:
      - /usr/local/bin/python
      - /usr/local/bin/python3
      - /usr/local/bin/python3.8

    discovered_interpreter_python:
      /usr/local/bin/python

    ansible_playbook_python:
      /usr/bin/python3

您可以看到,从列表 ansible_interpreter_python_fallback 中的第一项在 FreeBSD 远程主机上被发现。变量 ansible_playbook_python 保持运行剧本的 Linux 控制节点上的 Python 路径。

关于警告,引用自 INTERPRETER_PYTHON

The fallback behavior will issue a warning that the interpreter
should be set explicitly (since interpreters installed later may
change which one is used). This warning behavior can be disabled by
setting auto_silent or auto_legacy_silent. ...

您可以忽略它,或者通过设置变量 ansible_python_interpreter=auto_silent 来摆脱它,因为这实际上是您使用 /usr/local/bin/python(“以后安装的解释器可能会改变使用哪个”)所期望的。例如

shell> cat hosts
[test]
test_11
test_12
test_13

[test:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=true
ansible_become_user=root
ansible_become_method=sudo
ansible_interpreter_python_fallback=['/usr/local/bin/python', '/usr/local/bin/python3', '/usr/local/bin/python3.8']
ansible_python_interpreter=auto_silent
ansible_perl_interpreter=/usr/local/bin/perl

其他变量

如果您使用除了与 Ansible 捆绑在一起的插件以外的其他插件,您可以为 bashperlruby 设置类似的变量,具体取决于插件的编写方式。例如

[freebsd:vars]
ansible_python_interpreter=/usr/local/bin/python
ansible_perl_interpreter=/usr/local/bin/perl

哪些模块可用?

大多数核心 Ansible 模块都是针对类 Unix 机器和其他通用服务的组合而编写的,因此大多数应该在 BSD 上运行良好,但那些针对 Linux 专有技术的模块(例如 LVG)除外。

使用 BSD 作为控制节点

将 BSD 用作控制机与安装适用于您的 BSD 变体的 Ansible 包或遵循 pip 或“从源代码”说明一样简单。

BSD 事实

Ansible 以与 Linux 机器类似的方式从 BSD 收集事实,但由于网络、磁盘和其他设备的数据、名称和结构可能有所不同,因此应预计输出会有所不同,但对于 BSD 管理员来说仍然很熟悉。

BSD 努力和贡献

BSD 支持对我们 Ansible 来说很重要。即使我们大多数贡献者使用和针对 Linux,我们也有一个活跃的 BSD 社区,并努力尽可能地对 BSD 友好。如果您发现 BSD 的任何问题或不兼容问题,请随时报告;包含修复的拉取请求也欢迎!

另请参阅

介绍临时命令

基本命令的示例

使用剧本

学习 Ansible 的配置管理语言

您应该开发一个模块吗?

如何编写模块

邮件列表

问题?帮助?想法?请到 Google Groups 上的列表中

实时聊天

如何加入 Ansible 聊天频道