Páginas

Thursday, 4 July 2019

Limit the displayed output of a task

When you are using loops in complex data structures, the output of a task can be huge and unbearable.There is no need to display that info since there is no added value.

To get rid of that output, you can use the label directive with loop_control

This is an example of how I use this in a task, in my case I use this task to search for all the .profile files under / and set root as owner:

   - name: Search .profile files
     find:
      paths: '/'
      patterns: '*.profile'
      recurse: yes
      hidden: yes
     register: profileout

   - name: Set root as .profile files owner
     file:
      path: "{{ item.path }}"
      owner: root
     loop: "{{ profileout.files }}"
     loop_control:
       label: "{{ item.path }}"


without the lines in bold this is the output:

[ec2-user@arconada playbooks]$ ansible-playbook profile.yml

PLAY [zubi] ********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [zubi]

TASK [Search .profile files] ***************************************************************************************
ok: [zubi]

TASK [Set root as .profile files owner] ****************************************************************************
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1513629874.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/cache-mq.profile', u'xusr': False, u'atime': 1513629874.0, u'inode': 854459, u'isgid': False, u'size': 531, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1513629874.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/cache-smq.profile', u'xusr': False, u'atime': 1513629874.0, u'inode': 854460, u'isgid': False, u'size': 339, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1533091209.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/command_profile_template.profile', u'xusr': False, u'atime': 1533091209.0, u'inode': 854461, u'isgid': False, u'size': 3020, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1513629874.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/lvmdbusd.profile', u'xusr': False, u'atime': 1513629874.0, u'inode': 854462, u'isgid': False, u'size': 2309, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1533091209.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/metadata_profile_template.profile', u'xusr': False, u'atime': 1533091209.0, u'inode': 854463, u'isgid': False, u'size': 828, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1513629874.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/thin-generic.profile', u'xusr': False, u'atime': 1513629874.0, u'inode': 854496, u'isgid': False, u'size': 76, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})
ok: [zubi] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1513629874.0, u'gr_name': u'root', u'path': u'/etc/lvm/profile/thin-performance.profile', u'xusr': False, u'atime': 1513629874.0, u'inode': 854497, u'isgid': False, u'size': 80, u'isdir': False, u'ctime': 1551319647.8698492, u'roth': True, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 51713, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0444', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': False})

PLAY RECAP *********************************************************************************************************
zubi                       : ok=3    changed=0    unreachable=0    failed=0


With the lines in bold

[ec2-user@arconada playbooks]$ ansible-playbook profile.yml

PLAY [zubi] ********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [zubi]

TASK [Search .profile files] ***************************************************************************************
ok: [zubi]

TASK [Set root as .profile files owner] ****************************************************************************
ok: [zubi] => (item=/etc/lvm/profile/cache-mq.profile)
ok: [zubi] => (item=/etc/lvm/profile/cache-smq.profile)
ok: [zubi] => (item=/etc/lvm/profile/command_profile_template.profile)
ok: [zubi] => (item=/etc/lvm/profile/lvmdbusd.profile)
ok: [zubi] => (item=/etc/lvm/profile/metadata_profile_template.profile)
ok: [zubi] => (item=/etc/lvm/profile/thin-generic.profile)
ok: [zubi] => (item=/etc/lvm/profile/thin-performance.profile)

PLAY RECAP *********************************************************************************************************
zubi                       : ok=3    changed=0    unreachable=0    failed=0