使用 Ansible 管理 BSD 主机
管理 BSD 机器与管理其他类 Unix 机器不同。 如果您管理着运行 BSD 的节点,请查看以下主题。
连接到 BSD 节点
Ansible 默认使用 OpenSSH 连接到受管节点。 如果您使用 SSH 密钥进行身份验证,这在 BSD 上可以正常工作。 但是,如果您使用 SSH 密码进行身份验证,Ansible 依赖于 sshpass
。 大多数版本的 sshpass
不能很好地处理 BSD 登录提示,因此在使用 SSH 密码连接 BSD 机器时,请使用 paramiko
而不是 OpenSSH 进行连接。 您可以在 ansible.cfg
中全局执行此操作,也可以将其设置为清单/组/主机变量。 例如
[freebsd]
mybsdhost1 ansible_connection=paramiko
引导 BSD
Ansible 默认是无代理的,但是,它需要在受管节点上安装 Python。 只有 raw 模块可以在没有 Python 的情况下运行。 虽然可以使用此模块来引导 Ansible 并在 BSD 变体上安装 Python(请参见下文),但它非常有限,并且需要使用 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/python3
和 lang/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
以下 playbook
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
您可以看到,在 FreeBSD 远程主机上发现了列表 ansible_interpreter_python_fallback
中的第一个项目。 变量 ansible_playbook_python
保留了在运行 playbook 的 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 捆绑的插件以外的其他插件,则可以为 bash
、perl
或 ruby
设置类似的变量,具体取决于插件的编写方式。 例如
[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 facts
Ansible 以类似于 Linux 机器的方式从 BSD 收集 facts,但是由于网络、磁盘和其他设备的数据、名称和结构可能会有所不同,因此应该预计输出会略有不同,但 BSD 管理员仍然会熟悉。
BSD 的努力和贡献
BSD 支持对 Ansible 来说很重要。 即使我们的大多数贡献者都使用 Linux 并以 Linux 为目标,我们仍然拥有一个活跃的 BSD 社区,并努力尽可能对 BSD 友好。 请随意报告您发现的任何 BSD 问题或不兼容之处;也欢迎包含修复程序的 pull 请求!
另请参阅
- Ad hoc 命令简介
基本命令示例
- 使用 Playbook
学习 Ansible 的配置管理语言
- 您是否应该开发一个模块?
如何编写模块
- 沟通
有疑问? 需要帮助? 想分享您的想法? 请访问 Ansible 通信指南