Ansible无法识别主机操作系统,执行APT升级任务失败求助
Ansible执行APT升级任务失败问题排查
问题背景
我正在运行Ansible 6.1.0版本,相关配置及执行情况如下:
hosts.yml文件片段
pi1: vars: hosts: 192.168.2.31:
Playbook内容
--- - name: Maintain Pi Cluster hosts: piCluster connection: ansible.netcommon.network_cli gather_facts: true remote_user: don become: true tasks: - name: Ping the Servers connection: local ansible.builtin.ping: - name: Upgrade installed APT Packages ansible.builtin.apt: upgrade: yes update_cache: yes
执行结果
- Ping任务成功:
TASK [Ping the Servers] **************************************************************************************************************************************************** ok: [192.168.2.31]
- APT升级任务失败:
TASK [Upgrade installed APT Packages] ************************************************************************************************************************************** fatal: [192.168.2.31]: FAILED! => {"msg": "Unable to automatically determine host network os. Please manually configure ansible_network_os value for this host"}
额外信息
我可以正常SSH连接到目标主机:
$ ssh don@192.168.2.31 Linux pi1 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 ... blah, blah ... don@pi1:~ $
请问哪里操作出错了?
问题原因及解决方法
核心错误
你在Playbook里指定了connection: ansible.netcommon.network_cli,这个连接插件是专门用于**网络设备(如交换机、路由器、防火墙等)**的,而树莓派是普通Linux主机,并不适用这个连接方式,这就是报错提示无法识别网络OS的原因。
另外,Ping任务成功是因为你给这个任务单独指定了connection: local,它是在本地执行的,没有用到全局的network_cli连接,所以能正常运行。
修复步骤
- 修改Playbook的全局连接方式:去掉
connection: ansible.netcommon.network_cli,或者替换为Linux主机默认的ansible.builtin.ssh(其实不指定的话Ansible会自动用ssh连接,因为目标是Linux主机)。 - 调整Ping任务的连接配置:如果要真正测试远程主机的连通性,应该去掉Ping任务的
connection: local,改用默认的ssh连接执行ping模块,这样才能验证远程主机是否可达且能正常响应Ansible指令。
修改后的Playbook示例
--- - name: Maintain Pi Cluster hosts: piCluster gather_facts: true remote_user: don become: true tasks: - name: Ping the Servers ansible.builtin.ping: - name: Upgrade installed APT Packages ansible.builtin.apt: upgrade: yes update_cache: yes
额外提示
你的hosts.yml里pi1组的vars字段是空的,虽然不影响当前问题,但如果后续要给该组添加变量,记得补全内容格式,比如:
pi1: vars: ansible_python_interpreter: /usr/bin/python3 hosts: 192.168.2.31:
内容的提问来源于stack exchange,提问作者AG6HQ
相关产品推荐
相关产品推荐

