..

记 ansible 遇到的两个问题

问题1: docker 容器中无法通过 ansible 链接到远程服务器,但是在宿主机器上是可以的,错误信息如下:

@fatal: [target]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'xxxx' (ECDSA) to the list of known hosts.\r\nControl socket connect(/root/.ansible/cp/6d853b47a1): Connection refused\r\nFailed to connect to new control master", "unreachable": true}

问题很奇怪,解决方案也很奇怪,没有找到原因,上网搜索到答案是: 修改 ansible 的 ansible.cfg , 设置 controll path

control_path_dir=/dev/shm/ansible_control_path

问题2:也是在 docker 中进行 ansible 操作,宿主机器执行 ansible 网络能通过,容器内部执行 ssh 网络能通过,但是容器内部执行 ansible 网络就不通过,我是配置了 ssh proxy 的,通过条跳板机器到目标机器,显然 ansible 没有读取我的 ~/.ssh/config, 解决方式在 ansible 的 hosts 文件中设置 proxy,如下:

dev ansible_ssh_host=xxxx ansible_ssh_user=xxxx ansible_ssh_pass="xxxx" ansible_ssh_common_args=' -o ProxyCommand="ssh xxx@xxxxx -W %h:%p"'

或者在执行 ansible-playbook 的命令行中设置 ssh 参数

 ansbiel-playbook --ssh-common-args='-o ProxyCommand="ssh  -W %h:%p -q xxx@xxxx" ansible.yml