【问题标题】:(Ansible delegate_to) Failed to connect to the host via ssh(Ansible delegate_to) 无法通过 ssh 连接到主机
【发布时间】:2018-07-22 19:45:14
【问题描述】:

我们目前通过 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 示例进行滚动升级:https://github.com/ansible/ansible-examples/blob/master/lamp_haproxy/rolling_update.yml

我的剧本是这样的:

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

任务因以下语句而失败:

failed: [node-2] (item=groups.loadbalancer) => {"item": "groups.loadbalancer", "msg": "通过 ssh 连接主机失败: XXX@192.168.50.11:权限被拒绝(公钥)。\r\n", "unreachable": true}

令人惊讶的是XXX@192.168.50.11这行。 XXX 与清单文件中提供的 ansible_user 不匹配。

这可能是错误的原因吗?

【问题讨论】:

  • @techraf:我理解这个信息。处理它的一种缓慢方法是每次我重新配置这些盒子时清理我的 known_hosts。通过在 ansible.cfg 中添加 host_key_checking = False 指令,所有其他任务都不会出现此错误。它仅在使用 delegate_to 时失败。此外,似乎 Ansible 正在尝试连接用户 XXX 而不是 ubuntu(根据清单文件)
  • 我刚刚清理了 known_hosts 文件,现在收到错误消息:> failed: [node-2] (item=groups.loadbalancer) => {"item": "groups.loadbalancer", " msg": "通过 ssh 连接主机失败:XXX@192.168.50.11:权限被拒绝(公钥)。\r\n", "unreachable": true}

标签: ansible ansible-2.x


【解决方案1】:

刚刚发现我应该在任务中添加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

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 2017-02-08
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多