Guest User

Untitled

a guest
Feb 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. # ansible-playbook -i none, test_recursive_loop.yml
  2. - hosts: localhost
  3. gather_facts: false
  4.  
  5. vars:
  6. top_level_dn: "ou=root,ou=groot"
  7. rec_units:
  8. all-computers:
  9. name: "all of them"
  10. writers: mX support
  11. children:
  12. mX-computers:
  13. name: "Computers for shop X"
  14. writers:
  15. - mX support
  16. children:
  17. test-clients:
  18. name: "Test clients for shop X"
  19. best-clients:
  20. name: "The very best ones"
  21. children:
  22. test-child:
  23. name: "And their children"
  24. children:
  25. test-grandchild:
  26. name: "And grand children"
  27. mY-computers:
  28. name: "Another branch"
  29. writers:
  30. - here too
  31. children:
  32. guest-clients:
  33. name: "Those are guests"
  34. west-clients:
  35. name: "Those are westerners"
  36. children:
  37. cowboys:
  38. name: "Tough guys"
  39. cowgirls:
  40. name: "Tough gals"
  41. mZ-computers:
  42. name: "Lonely"
  43.  
  44. deep_addon:
  45. all-computers:
  46. children:
  47. mX-computers:
  48. children:
  49. rest-clients:
  50. name: "Rest of clients in MX"
  51.  
  52. org_tree: "{{ rec_units | combine(deep_addon, recursive=true) }}"
  53.  
  54. tasks:
  55.  
  56. # requires jinja 2.10 (and most probably ansible 2.4)
  57. # could include whole item as well, but this will duplicate whole subtree N times
  58. - name: flatten dict with backlinks
  59. set_fact:
  60. flatten_dict3: |
  61. {% set res = [] -%}
  62. {% set parent = namespace(dn="", prev_dn="") -%}
  63. {% for rdn, props in org_tree.items() recursive -%}
  64. {% set dummy = res.extend([{'dn': 'ou=' + rdn + ',' + parent.dn + top_level_dn, 'name': props.name}]) -%}
  65. {% if 'children' in props -%}
  66. {% set parent.prev_dn = parent.dn -%}
  67. {% set parent.dn = 'ou=' + rdn + ',' + parent.dn -%}
  68. {% set dummy2 = loop(props.children | dictsort) -%}
  69. {% set parent.dn = parent.prev_dn.split(',')[-1*loop.depth:]|join(',') -%}
  70. {% endif -%}
  71. {% endfor -%}
  72. {{ res }}
  73.  
  74. - name: print results
  75. debug:
  76. msg: "{{ flatten_dict3 }}"
Add Comment
Please, Sign In to add comment