Ansible stable-2.12集成测试无法连接localhost问题求助
Ansible stable-2.12 集成测试无法连接localhost问题解决方案
在GitHub工作流环境中,仅Ansible stable-2.12版本的集成测试出现localhost连接失败问题,错误信息如下:
fatal: [localhost]: UNREACHABLE! => { "changed": false, "msg": "Data could not be sent to remote host \"localhost\". Make sure this host can be reached over ssh: OpenSSH_7.6p1 Ubuntu-4ubuntu0.5, OpenSSL 1.0.2n 7 Dec 2017\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: Control socket \"/root/.ansible/cp/72dc686b8e\" does not exist\r\ndebug2: resolving \"localhost\" port 22\r\ndebug2: ssh_connect_direct: needpriv 0\r\ndebug1: Connecting to localhost [127.0.0.1] port 22.\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug1: connect to address 127.0.0.1 port 22: Connection refused\r\ndebug1: Connecting to localhost [::1] port 22.\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug1: connect to address ::1 port 22: Cannot assign requested address\r\nssh: connect to host localhost port 22: Cannot assign requested address\r\n", "unreachable": true }
PLAY RECAP ********************************************************************* localhost : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0 Command exited with status 4 after 0.9616847038269043 seconds. ERROR: Command "ansible-playbook -i /tmp/ssh-inventory-5veyhtvk.json /root/ansible/test/lib/ansible_test/_data/playbooks/posix_hosts_prepare.yml -e '{"hosts_entries": ["172.17.0.2 nios-simulator # ansible-test 92c84b72-be55-4d0c-8515-cf8d7fa1cd0c"]}' -vvvvvv" returned exit status 4.
解决方案
启动GitHub Runner的SSH服务
GitHub默认Ubuntu runner未启用sshd,需在工作流步骤中先启动服务:sudo systemctl enable --now sshd强制Ansible使用本地连接
针对localhost,无需通过SSH连接,可在inventory或playbook中指定connection: local:- 修改inventory文件(如
/tmp/ssh-inventory-5veyhtvk.json):{ "localhost": { "ansible_connection": "local" } } - 或在playbook中直接指定:
- hosts: localhost connection: local tasks: # 任务内容
- 修改inventory文件(如
禁用SSH控制套接字
在ansible.cfg中添加配置,避免控制套接字导致的连接问题:[ssh_connection] control_path = none修复IPv6连接问题
错误显示IPv6连接失败,可临时禁用IPv6或强制使用IPv4:# 禁用IPv6 sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1 # 或强制Ansible使用IPv4连接localhost ansible-playbook ... -e ansible_host=127.0.0.1适配Ansible stable-2.12环境依赖
该版本可能存在兼容性问题,更新openssh-client确保环境正常:sudo apt-get update && sudo apt-get install -y openssh-client
内容的提问来源于stack exchange,提问作者Hemanth Kumar Kandamuri
相关产品推荐
相关产品推荐

