我的公司防火墙策略只允许同一源和目标之间每分钟 60 秒 20 个连接。
由于这个原因,ansible play 会在一段时间后挂起。
我希望多个任务使用相同的 ssh session 而不是创建新 session 。为此,我在本地文件夹 ansible.cfg
以及命令行中设置了以下 pipelining = True
。
cat /opt/automation/startservices/ansible.cfg
[defaults]
host_key_checking = False
gathering = smart
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=600s
control_path = %(directory)s/%%h-%%r
pipelining = True
ANSIBLE_SSH_PIPELINING=0 ansible-playbook -i /opt/automation/startservices/finalallmw.hosts /opt/automation/startservices/va_action.yml -e '{ dest_host: myremotehost7 }' -e dest_user=oracle
剧本太大,无法在此处共享,但正是这个任务在循环,由于在 60 秒内超过 20 个 ssh 连接,它在此处挂起。
- name: Copying from "{{ inventory_hostname }}" to this ansible server.
synchronize:
src: "{{ item.path }}"
dest: "{{ playbook_dir }}/homedirbackup/{{ inventory_hostname }}/{{ dtime }}/"
mode: pull
copy_links: yes
with_items:
- "{{ to_copy.files }}"
设置流水线设置后,我的播放在 20 个连接后仍然挂起。
以下是剧本设置:
hosts: "{{ groups['dest_nodes'] | default(groups['all']) }}"
user: "{{ USER | default(dest_user) }}"
any_errors_fatal: True
gather_facts: false
tags: always
vars:
ansible_host_key_checking: false
ansible_ssh_extra_args: -o StrictHostKeyChecking=no -o ConnectionAttempts=5
到目前为止,在此线程上发布建议问题仍然存在。下面是我的本地目录ansible.cfg
$ cat /opt/automation/startservices/ansible.cfg
# config file for ansible -- http://ansible.com/
# ==============================================
# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first
[defaults]
host_key_checking = False
roles_path = roles/
gathering = smart
[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=1200s -o ControlPath=~/.ansible/cp/%r@%h:%p
[persistent_connection]
control_path_dir = ~/.ansible/cp
$
对于所有任务都使用相同的 ssh session 并且流水线在这里不起作用的可靠方面的问题,您能否提出任何解决方案?
最佳答案
首先:pipelining = True
没有做您正在寻找的事情。它减少了网络操作的数量,但没有减少 ssh 连接的数量。检查docs获取更多信息。
恕我直言,使用它仍然是一件好事,因为它会加快您的剧本。
您要使用的是“持久控制模式”,这是 OpenSSH 的一项功能,可以保持连接打开。
例如,您可以在 ansible.cfg
中执行此操作:
[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=1200
这将使连接保持打开状态 1200 秒。
关于ssh - 我怎样才能使 ansible 重用 SSH session 而不是为每个任务创建一个新 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68496003/