Ansible 连接的 stdin 对象已弃用,解决方法是使用 ansible.builtin.vars
模块来代替。下面是一个示例代码:
- name: Playbook 示例
hosts: all
gather_facts: false
tasks:
- name: 执行命令并获取输出
command: echo "Hello, World!"
register: command_output
changed_when: false
- name: 将输出写入变量
set_fact:
command_output: "{{ command_output.stdout_lines | join('\n') }}"
- name: 显示输出
debug:
var: command_output
在上面的示例中,command_output
是一个变量,用于存储执行命令的输出。通过 set_fact
模块,将 stdout_lines 转换为单个字符串,并将其赋值给 command_output
。最后,使用 debug
模块来显示输出。
这样,就可以避免使用已弃用的 stdin 对象,而使用 ansible.builtin.vars
模块来获取命令的输出。
下一篇:Ansible连续搜索一行