如何让 ansible 重用 SSH 会话,而不是为每项任务创建一个新会话?

Ash*_*har 4 ssh pipeline ansible session-reuse

我的公司防火墙策略仅允许同一源和目标之间每分钟 60 秒 20 个连接。

因此,ansible 播放一段时间后就会挂起。

我希望多个任务使用相同的 ssh 会话而不是创建新会话。为此,我pipelining = True在本地文件夹ansible.cfg和命令行中设置了以下内容。

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
Run Code Online (Sandbox Code Playgroud)

该剧本太大,无法在此处共享,但正是该任务循环,并且由于 60 秒内超过 20 个 ssh 连接而挂起。

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
Run Code Online (Sandbox Code Playgroud)

设置管道设置后,我的游戏在 20 个连接后仍然挂起。

以下是剧本设置:

 - 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 }}"
Run Code Online (Sandbox Code Playgroud)

到目前为止,在此线程上发布建议,问题仍然存在。下面是我的本地目录 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
$
Run Code Online (Sandbox Code Playgroud)

您能否建议任何解决方案来解决 ansible 方面的问题,其中所有任务都使用相同的 ssh 会话并且管道在这里不起作用?

toy*_*ian 8

第一:pipelining = True没有做你正在寻找的事情。它减少了网络操作的数量,但没有减少 ssh 连接的数量。检查文档以获取更多信息。
恕我直言,它仍然是一个好用的东西,因为它会加快你的剧本。

您想要使用的是“持久控制模式”,这是 OpenSSH 保持连接打开的功能。

例如,您可以在您的ansible.cfg

[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=1200
Run Code Online (Sandbox Code Playgroud)

这将使连接保持打开状态 1200 秒。