我如何获取日志/活动手册模块执行的详细信息?

说我执行以下。

$ cat test.sh #!/bin/bash echo Hello World exit 0 $ cat Hello.yml --- - hosts: MyTestHost tasks: - name: Hello yourself script: test.sh $ ansible-playbook Hello.yml PLAY [MyTestHost] **************************************************************** GATHERING FACTS *************************************************************** ok: [MyTestHost] TASK: [Hello yourself] ******************************************************** ok: [MyTestHost] PLAY RECAP ******************************************************************** MyTestHost : ok=2 changed=0 unreachable=0 failed=0 $ 

我确信它是成功的。

在哪里/如何看到我的脚本在远程主机(MyTestHost)上回显/打印的“Hello World”? 或脚本的返回/退出代码?

我的研究显示,我可以编写一个插件来拦截模块执行callback或者这些行上的东西,然后写一个日志文件。 我宁愿不浪费我的时间。

例如像下面的标准输出(注意我正在运行一个完整的,不可行的剧本):

 $ ansible plabb54 -i /project/plab/svn/plab-maintenance/ansible/plab_hosts.txt -m script -a ./test.sh plabb54 | success >> { "rc": 0, "stderr": "", "stdout": "Hello World\n" } $ 

如果您在命令行上将-v标志传递给ansible-playbook,则会看到执行的每个任务的stdout和stderr:

 $ ansible-playbook -v playbook.yaml 

Ansible还具有对日志logging的内置支持。 将以下行添加到您可信的configuration文件中 :

 [defaults] log_path=/path/to/logfile 

Ansible会在几个地方查找configuration文件:

  • ansible.cfg在您运行ansible-playbook的当前目录中
  • ~/.ansible.cfg
  • /etc/ansible/ansible.cfg

剧本脚本任务将生成stdout ,就像非playbook命令一样,只需要使用register将其保存到variables中即可。 一旦我们得到了, debugging模块可以打印到剧本输出stream。

 tasks: - name: Hello yourself script: test.sh register: hello - name: Debug hello debug: var=hello - name: Debug hello.stdout as part of a string debug: "msg=The script's stdout was `{{ hello.stdout }}`." 

输出应该是这样的:

 TASK: [Hello yourself] ******************************************************** changed: [MyTestHost] TASK: [Debug hello] *********************************************************** ok: [MyTestHost] => { "hello": { "changed": true, "invocation": { "module_args": "test.sh", "module_name": "script" }, "rc": 0, "stderr": "", "stdout": "Hello World\r\n", "stdout_lines": [ "Hello World" ] } } TASK: [Debug hello.stdout as part of a string] ******************************** ok: [MyTestHost] => { "msg": "The script's stdout was `Hello World\r\n`." } 

使用callback插件,可以使用play来以可读格式输出命令的标准输出: gist:human_log.py

编辑例如输出:

  _____________________________________ < TASK: common | install apt packages > ------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || changed: [10.76.71.167] => (item=htop,vim-tiny,curl,git,unzip,update-motd,ssh-askpass,gcc,python-dev,libxml2,libxml2-dev,libxslt-dev,python-lxml,python-pip) stdout: Reading package lists... Building dependency tree... Reading state information... libxslt1-dev is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 24 not upgraded. stderr: start: 2015-03-27 17:12:22.132237 end: 2015-03-27 17:12:22.136859