使用 Ansible 管理 BSD 主机
管理 BSD 机器与管理其他类 Unix 机器有所不同。如果您有运行 BSD 的被管理节点,请查看这些主题。
连接到 BSD 节点
Ansible 默认使用 OpenSSH 连接到被管理节点。如果您使用 SSH 密钥进行身份验证,这在 BSD 上可以正常工作。但是,如果您使用 SSH 密码进行身份验证,Ansible 依赖于 sshpass。大多数版本的 sshpass 无法很好地处理 BSD 的登录提示,因此在对 BSD 机器使用 SSH 密码时,请使用 paramiko 代替 OpenSSH 进行连接。您可以在 ansible.cfg 中进行全局设置,或者将其设置为清单/组/主机变量。例如
[freebsd]
myfreebsdhost ansible_connection=paramiko
引导 BSD (Bootstrapping BSD)
Ansible 默认是无代理的,但它要求被管理节点上安装有 Python。只有 raw 模块可以在没有 Python 的情况下运行。虽然该模块可用于引导 Ansible 并在 BSD 变体上安装 Python(见下文),但其功能非常有限,必须使用 Python 才能充分利用 Ansible 的特性。
以下示例安装了 Python,其中包含 Ansible 全功能所需的 json 库。在控制机上,您可以针对大多数 FreeBSD 版本执行以下操作
ansible -m raw -a "pkg install -y python" myfreebsdhost
或者针对 OpenBSD
ansible -m raw -a "pkg_add -I python%3.11" myopenbsdhost
完成后,您现在可以使用除 raw 模块以外的其他 Ansible 模块了。
注意
本示例演示了在 FreeBSD 上使用 pkg 以及在 OpenBSD 上使用 pkg_add,但您应该能够将其替换为您所用 BSD 的相应软件包工具;软件包名称也可能有所不同。请参考您所使用的 BSD 变体的软件包列表或文档,以获取要安装的准确 Python 软件包名称。
设置 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
FreeBSD 软件包与 ports
在 FreeBSD 中,无法保证默认安装了 /usr/local/bin/python 可执行文件或指向可执行文件的链接。对于远程主机,在 Ansible 方面的最佳实践是至少安装 Ansible 支持的 Python 版本(例如 lang/python311),以及 meta ports 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.11_3,2 "meta-port" for the default version of Python interpreter
python3-3_4 Meta-port for the Python interpreter 3.x
python311-3.11.10 Interpreted object-oriented programming language
以及以下可执行文件和链接
shell> ls -l /usr/local/bin/ | grep python
lrwxr-xr-x 1 root wheel 7 Nov 1 18:55 python -> python3
lrwxr-xr-x 1 root wheel 14 Nov 1 18:55 python-config -> python3-config
lrwxr-xr-x 1 root wheel 10 Oct 31 11:40 python3 -> python3.11
lrwxr-xr-x 1 root wheel 17 Oct 31 11:40 python3-config -> python3.11-config
-r-xr-xr-x 1 root wheel 4744 Oct 31 11:14 python3.11
-r-xr-xr-x 1 root wheel 3113 Oct 31 11:14 python3.11-config
INTERPRETER_PYTHON_FALLBACK
从 2.8 版本起,Ansible 提供了一个非常有用的变量 ansible_interpreter_python_fallback,用于指定搜索 Python 的路径列表。请参阅 INTERPRETER_PYTHON_FALLBACK。此列表将被依次搜索,并使用找到的第一个项目。例如,下面的配置将使前一节中 meta-ports 的安装变得多余,也就是说,如果您不安装 Python meta ports,列表中的前两项将被跳过,并发现 /usr/local/bin/python3.11。
ansible_interpreter_python_fallback=['/usr/local/bin/python', '/usr/local/bin/python3', '/usr/local/bin/python3.11']
您可以使用此变量(并加入较低版本的 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.11']
ansible_perl_interpreter=/usr/local/bin/perl
下面的 playbook
# 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_CALLBACK_RESULT_FORMAT=yaml 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.18/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.11
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.11']
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 从 BSD 收集事实的方式与 Linux 机器类似,但由于网络、磁盘和其他设备的数据、名称和结构可能有所不同,因此预计输出会有轻微差异,但对于 BSD 管理员来说仍然很熟悉。
BSD 的努力与贡献
BSD 支持对 Ansible 而言至关重要。尽管我们的大多数贡献者使用并针对 Linux,但我们拥有一个活跃的 BSD 社区,并努力尽可能地对 BSD 友好。如果您在 BSD 上发现了任何问题或不兼容之处,请随时报告;同时也欢迎提交包含修复方案的 pull request!
另请参阅
- 即席命令(ad hoc commands)介绍
基本命令示例
- 使用 Playbook
学习 Ansible 的配置管理语言
- 您应该开发模块吗?
如何编写模块
- 交流方式
有疑问?需要帮助?想分享你的想法?请访问 Ansible 通信指南