ansible 剧本 [设置] 收集事实 - SSH UNREACHABLE 连接在横幅期间超时

Aru*_*gal 2 ssh amazon-ec2 connection-timeout ansible ansible-2.x

我在 Mac 机器上。

$ which ansible
/Library/Frameworks/Python.framework/Versions/3.5/bin/ansible
Run Code Online (Sandbox Code Playgroud)

或者我猜,ansible可以位于通用位置:(/usr/bin/ansible例如:在 CentOS/Ubuntu 上)。

$ ansible --version
ansible 2.2.0.0
Run Code Online (Sandbox Code Playgroud)

在我的另一个 vagrant / Ubuntu 机器上运行以下 playbook 效果很好。

Playbook 文件如下所示:

- hosts: all
  become: true
  gather_facts: true

  roles:
    - a_role_which_just_say_hello_world_debug_msg
Run Code Online (Sandbox Code Playgroud)

从我的本地计算机,我可以成功ssh访问目标服务器/以下服务器(无需任何密码,因为我已经使用添加了 .pem 密钥文件ssh-add),这在 Ansible playbook 运行中的 Ansible playbook [Setup] (收集事实步骤)中失败。

在 Mac 机器上,我有时(不是每次)都会收到此错误。错误:Failed to connect to the host via ssh: Connection timed out during banner exchange。PS:这个问题不会一直出现。

$ ansible-playbook -i inventory -l tag_cluster_mycluster myplabook.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [myclusterSomeServer01_i_07f318688f6339971]
fatal: [myclusterSomeServer02_i_03df6f1f988e665d9]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Connection timed out during banner exchange\r\n", "unreachable": true}
Run Code Online (Sandbox Code Playgroud)

好的,尝试了几次,同样的行为,在 15 台服务器(我在 mycluster 集群中)中,设置[SETUP]在收集事实设置期间失败,而下次它工作正常。

重试: $ ansible-playbook -i inventory -l tag_cluster_mycluster myplabook.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [myclusterSomeServer01_i_07f318688f6339971]
ok: [myclusterSomeServer02_i_03df6f1f988e665d9]
ok: [myclusterSomeServer03_i_057dfr56u88e665d9]
...
.....more...this time it worked for all servers.
Run Code Online (Sandbox Code Playgroud)

正如您在上面看到的,这次上述步骤运行良好。在某些任务/操作期间(我尝试使用 Ansibleyum模块安装某些东西)会发生同样的问题(SSH 连接超时)。如果我再试一次,对于上次失败的服务器来说它工作正常,但随后可能会失败对于上次成功的另一台服务器。因此,该行为是随机的。

我的 /etc/ansible/ansible.cfg 文件有:

[ssh_connection]
scp_if_ssh = True
Run Code Online (Sandbox Code Playgroud)

Aru*_*gal 5

当我将其增加到 25 时,将以下timeout设置添加到 /etc/ansible/ansible.cfg 配置文件中是有效的。当它是 10 或 15 时,由于连接超时横幅问题,我仍然在某些服务器中看到错误。

[defaults]
timeout = 25

[ssh_connection]
scp_if_ssh = True
Run Code Online (Sandbox Code Playgroud)

除了上述之外,我还必须使用serial: Nserial: N%(其中 N 是一个数字)一次在 N 个或 N 个服务器上运行我的剧本,然后它工作得很好。

IE

- hosts: all
  become: true
  gather_facts: true
  serial: 2
  #serial: "10%"
  #serial: "{{ serialNumber }}"
  #serial: "{{ serialNumber }}%"

  vars:
   - serialNumber: 5

  roles:
    - a_role_which_just_say_hello_world_debug_msg
Run Code Online (Sandbox Code Playgroud)