我有一个剧本由另一个剧本使用包含设置启动,如下所示。
父.yml
- hosts: localhost
gather_facts: False
tasks:
- add_host:
name: "{{ item.value['hostname'] }}"
ansible_host: "{{ item.value['oob_ip'] }}"
ansible_user: "{{ servers.web_servers.vars.ilousername }}"
ansible_ssh_pass: "{{ servers.web_servers.vars.ilopassword }}"
group: ilohosts
- hosts: ilohosts
tasks:
- include: collect_mac.yml
collect_mac.yml
#- set_fact:
# ansible_ssh_pass: "{{ servers.web_servers.vars.ilopassword }}"
- name: collect MAC address
raw: "racadm getsysinfo"
register: eth_info
其余的yml我已经去掉了。作为获取MAC地址的几个正则表达式。
问题是,当我将 collect_mac.yml 作为单独的剧本运行时,添加标头并在清单文件中包含相同的主机。
- name: collect mac
hosts: hosts
remote_user: root
gather_facts: False
它运行没有问题。但是当它通过 parent.yml 运行时,我收到以下错误
{"msg": "failed to open a SFTP connection (Garbage packet received)"}
为什么 ansible 尝试打开 SFTP 会话,而不是 SSH 并运行原始命令?
- 列出主机变量
- 仔细检查参数是否正确
- 并可能修复add_host的参数
.
- hosts: ilohosts
tasks:
- debug: var=hostvars
例如,下面的剧本按预期工作。
- hosts: localhost
tasks:
- add_host:
name: test_01
ansible_host: 10.1.0.51
groups: ilohosts
- hosts: ilohosts
tasks:
- raw: hostname
register: info
- debug:
var: info.stdout
找到了实际问题。这是导致问题gather_facts。一旦我修改了parent.yml中调用child.yml的部分,就像这个错误消失了一样。
- hosts: localhost
gather_facts: False
tasks:
- add_host:
name: "{{ item.value['hostname'] }}"
ansible_host: "{{ item.value['oob_ip'] }}"
ansible_user: "{{ servers.web_servers.vars.ilousername }}"
ansible_ssh_pass: "{{ servers.web_servers.vars.ilopassword }}"
group: ilohosts
- hosts: ilohosts
gather_facts: False
tasks:
- include: collect_mac.yml
我认为这是因为我连接的服务器不支持 SFTP,当启用gather_facts时,它会尝试将结果 json SFTP 发回源服务器。谢谢@Vladimir博特卡,你的评论为我指明了正确的方向