HANDBOOK
解决ansible mitogen 0.3.0+ 插件未渲染ansible_ssh_common_args模板变量问题
问题 使用ansible mitogen 0.3.4插件进行kubespray安装时,报错: EOF on stream; last 100 lines received:\nssh: Could not resolve hostname {%: Name or service not known 分析 经过debug分析,kubespray-default默认定义了如下变量模板:
1 2 3 4 |
ansible_ssh_common_args: | "{% if 'bastion' in groups['all'] %} -o ProxyCommand='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p -p {{ hostvars['bastion']['ansible_port'] | default(22) }} {{ hostvars['bastion']['ansible_user'] }}@{{ hostvars['bastion']['ansible_host'] }} {% if ansible_ssh_private_key_file is defined %} -i {{ ansible_ssh_private_key_file }}{% endif %} ' {% endif %}" |
但是通过ansible role去执行后,通过mitogen进行ssh并没有渲染出来变量:
1 2 3 4 5 |
mitogen.parent: command line for Connection(None): ssh -o "LogLevel ERROR" -l root -p 22 -o "Compression yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 10" -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "GlobalKnownHostsFile /dev/null" -C -o ControlMaster=auto -o ControlPersist=60s {% if bastion in groups[all] %} -o "ProxyCommand=ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p -p {{ hostvars[bastion][ansible_port] | default(22) }} {{ hostvars[bastion][ansible_user] }}@{{ hostvars[bastion][ansible_host] }} {% if ansible_ssh_private_key_file is defined %} -i {{ ansible_ssh_private_key_file }}{% endif %} " {% endif %} 172.20.59.209 /usr/bin/python -c |
可以看到,mitogen的ssh将ansible_ssh_common_args原封不动地输出来了 修改源码验证: 修改transport_config.py, 增加debug信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
476 def ssh_args(self): 478 print('------------>') 479 print('ssh_args: ',C.config.get_config_value("ssh_args", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {}))) 480 print('ssh_common_args: ',C.config.get_config_value("ssh_common_args", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {}))) 481 print('ssh_extra_args: ', C.config.get_config_value("ssh_extra_args", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {}))) 482 print('----------->') 483 return [ 484 mitogen.core.to_text(term) 485 for s in ( 486 C.config.get_config_value("ssh_args", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {})), 487 C.config.get_config_value("ssh_common_args", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {})), 488 C.config.get_config_value("ssh_extra_args", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {})) 489 #C.config.get_config_value("ssh_args", plugin_type="connection", plugin_name="ssh", variables=local_vars), 490 #C.config.get_config_value("ssh_common_args", plugin_type="connection", plugin_name="ssh", variables=local_vars), 491 #C.config.get_config_value("ssh_extra_args", plugin_type="connection", plugin_name="ssh", variables=local_vars) 492 ) 493 for term in ansible.utils.shlex.shlex_split(s or '') 494 ] |
下面是输出:
1 2 3 4 5 |
------------> ssh_args: -o ControlMaster=auto -o ControlPersist=1d -o UserKnownHostsFile=/dev/null -o ConnectTimeout=30 -o Compression=yes -o TCPKeepAlive=yes -o ServerAliveInterval=60 -o StrictHostKeyChecking=no -o AddKeysToAgent=yes -o ControlPath=~/.ssh/%r@%h-%p ssh_common_args: {% if 'bastion' in groups['all'] %} -o ProxyCommand='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p -p {{ hostvars['bastion']['ansible_port'] | default(22) }} {{ hostvars['bastion']['ansible_user'] }}@{{ hostvars['bastion']['ansible_host'] }} {% if ansible_ssh_private_key_file is defined %}-i {{ ansible_ssh_private_key_file }}{% endif %} ' {% endif %} ssh_extra_args: -----------> |
可以看到ssh_common_args变量没有渲染 解决 将_task_vars.get(“vars”, {}) 改为 _task_vars.get(“hostvars”, {}), 从hostvars取值 搜索官方issures: https://github.com/mitogen-hq/mitogen/pull/956 commit ac34252bcccb60b50e6a8ed3a3b2f42d256d62e0 https://github.com/mitogen-hq/mitogen/pull/956/commits/ac34252bcccb60b50e6a8ed3a3b2f42d256d62e0 总结 Read more…