(Ansible delegate_to)无法通过ssh连接到主机

(Ansible delegate_to)无法通过ssh连接到主机,ansible,ansible-2.x,Ansible,Ansible 2.x,目前,我们通过Vagrant设置了3个箱子,其库存文件如下: [loadbalancer] node-1 ansible_host=192.168.50.10 ansible_user=ubuntu [webservers] node-2 ansible_host=192.168.50.11 ansible_user=ubuntu [dbservers] node-3 ansible_host=192.168.50.12 ansible_user=ubuntu 下面是github的滚动升级

目前,我们通过Vagrant设置了3个箱子,其库存文件如下:

[loadbalancer]
node-1 ansible_host=192.168.50.10 ansible_user=ubuntu

[webservers]
node-2 ansible_host=192.168.50.11 ansible_user=ubuntu

[dbservers]
node-3 ansible_host=192.168.50.12 ansible_user=ubuntu
下面是github的滚动升级示例:

我的剧本是这样的:

pre_tasks:
- name: disable the server in haproxy
  haproxy: 'state=disabled backend=myapplb host={{ inventory_hostname }} socket=/var/lib/haproxy/stats'
  delegate_to: "{{ ansible_host }}"
  with_items: groups.loadbalancer
任务失败,出现以下语句:

失败:[node-2](item=groups.loadbalancer)=>{“item”:“groups.loadbalancer”,“msg”:“未能通过ssh连接到主机:XXX@192.168.50.11:权限被拒绝(公钥)。\r\n,“无法访问”:true}

令人惊讶的是这一行XXX@192.168.50.11。XXX与库存文件中提供的ansible_用户不匹配


这可能是错误的原因吗?

刚刚发现我应该将远程用户参数添加到任务中。 正确的语法是:

pre_tasks:
- name: disable the server in haproxy
  haproxy: 'state=disabled backend=myapplb host={{ inventory_hostname }} socket=/var/lib/haproxy/stats'
  delegate_to: "{{ ansible_host }}"
  remote_user: "{{ ansible_user }}"
  with_items: groups.loadbalancer

刚刚发现我应该将remote_user参数添加到任务中。 正确的语法是:

pre_tasks:
- name: disable the server in haproxy
  haproxy: 'state=disabled backend=myapplb host={{ inventory_hostname }} socket=/var/lib/haproxy/stats'
  delegate_to: "{{ ansible_host }}"
  remote_user: "{{ ansible_user }}"
  with_items: groups.loadbalancer

@techraf:我理解这个信息。处理这个问题的一个慢方法是每次我重新配置箱子时都清理我已知的主机。通过在ansible.cfg中添加host_key_checking=False指令,此错误不会发生在所有其他任务上。它仅在使用委托_访问时失败。此外,Ansible似乎正在尝试连接用户XXX而不是ubuntu(根据清单文件),我刚刚清理了已知的_hosts文件,现在收到错误消息:>失败:[node-2](item=groups.loadbalancer)=>{“item”:“groups.loadbalancer”,“msg”:“无法通过ssh连接到主机:XXX@192.168.50.11:权限被拒绝(公钥)。\r\n,“无法访问”:true}@techraf:我理解这条消息。处理这条消息的一个慢方法是每次我重新设置框时清理已知的主机。通过在ansible.cfg中添加host\u key\u checking=False指令,此错误不会发生在所有其他任务上。它只会在使用委托时失败。此外,ansible似乎正在尝试连接到用户XXX而不是ubuntu(根据清单文件),我刚刚清理了已知的_hosts文件,现在得到错误消息:>failed:[node-2](item=groups.loadbalancer)=>{“item”:“groups.loadbalancer”,“msg”:“无法通过ssh连接到主机:XXX@192.168.50.11:权限被拒绝(公钥)。\r\n,“无法访问”:true}