ansible register with loop debug print not working

浪子不回头ぞ 提交于 2019-12-02 17:28:36

问题


i have a simple playbook that is supposed to display my services status. and i want to view the output from the machine to see if the status is active or not. so i used a debug print, like so:

- name: name_of_services
  shell: systemctl status {{item}}
  with_items:
   - service1
   - service2
  register: out

- debug: var=item.stdout_lines
  with_items: out.results

when i execute this i get a lot of info i don't want plus the item.stdout_lines info that i do want in the end of it. how is it possible to view the output of my command better?


回答1:


For modules, including debug, called in a loop (ie with_items), the value of item at each iteration will be shown. I don't know of a way to turn this off. If you want you reduce your output you can try switching to using the msg parameter to the debug module which takes a jinja templated string. You could do something like this obviously adjusting the regex to match systemctl output.

- name: show values
  debug: msg="{{ item.stdout_lines | replace_regex('^(.*).service.*Active: (.*).$', \\\1 \\\2) }}"
  with_items: out.results

If you don't want to use the replace_regex function you can consider writing your own filter plugin to format the data the way you like it.

In general ansible playbooks aren't a great place to display status information gathered through register vars, facts, etc. The playbook output is more geared toward task status.



来源:https://stackoverflow.com/questions/36174337/ansible-register-with-loop-debug-print-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!