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

时间:2018-02-12 15:27:51

标签: 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示例进行滚动升级: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

任务失败,声明如下:

  

失败:[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_user不匹配。

这可能是导致错误的原因吗?

1 个答案:

答案 0 :(得分: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
相关问题