ansible register with loop debug print not working

2019-03-03 18:32发布

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条回答
够拽才男人
2楼-- · 2019-03-03 18:51

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.

查看更多
登录 后发表回答