How Ansible Script module return execution result

2019-08-16 02:27发布

I execute a shell script in remote hosts using ansible script module. My shell script test.sh is

pwd
echo "hello"
ls -l

My ansible playbook is

- name: Transfer and execute a script.
  hosts: server
  tasks:
     - name: Copy and Execute the script 
       script: /root/test.sh

Besides, I want to view the execution result of every command in the script. But i only watched result.

changed: [172.18.96.134]
changed: [172.18.96.244]
changed: [172.18.96.245]

How can i watch result like this:

/root
hello
a.txt  test.txt

标签: ansible
1条回答
倾城 Initia
2楼-- · 2019-08-16 02:52

You can't. It's not possible to trace a module on-line in Ansible. You can write your own log from the script.

Ansible standard way to handle such requirements is Asynchronous Actions and Polling to efficiently handle the output of the potentially large number of hosts.

It's possible to register and print the module's result.

- name: Copy and Execute the script 
    script: /root/test.sh
    register: result
- debug:
    var: result.stdout
查看更多
登录 后发表回答