You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.7 KiB

  1. ---
  2. # NOTE: This should be a role (or custom module), but currently include_role is too buggy to use
  3. - name: "sync_file | Set facts for directory and file when sync_file_path is defined"
  4. set_fact:
  5. sync_file_dir: "{{ sync_file_path | dirname }}"
  6. sync_file: "{{ sync_file_path | basename }}"
  7. when: sync_file_path is defined and sync_file_path != ''
  8. - name: "sync_file | Set fact for sync_file_path when undefined"
  9. set_fact:
  10. sync_file_path: "{{ (sync_file_dir, sync_file)|join('/') }}"
  11. when: sync_file_path is not defined or sync_file_path == ''
  12. - name: "sync_file | Set fact for key path name"
  13. set_fact:
  14. sync_file_key_path: "{{ sync_file_path.rsplit('.', 1)|first + '-key.' + sync_file_path.rsplit('.', 1)|last }}"
  15. when: >-
  16. sync_file_is_cert|d() and (sync_file_key_path is not defined or sync_file_key_path == '')
  17. - name: "sync_file | Check if file exists"
  18. stat:
  19. path: "{{ sync_file_path }}"
  20. register: sync_file_stat
  21. - name: "sync_file | Check if key file exists"
  22. stat:
  23. path: "{{ sync_file_key_path }}"
  24. register: sync_file_key_stat
  25. when: sync_file_is_cert|d()
  26. - name: "sync_file | Combine all possible file sync sources"
  27. set_fact:
  28. sync_file_srcs: "{{ sync_file_srcs|default([]) + [host_item] }}"
  29. with_items: "{{ sync_file_hosts | unique }}"
  30. loop_control:
  31. loop_var: host_item
  32. when: hostvars[host_item]["sync_file_stat"]["stat"]["exists"]|bool
  33. - name: "sync_file | Combine all possible key file sync sources"
  34. set_fact:
  35. sync_file_key_srcs: "{{ sync_file_key_srcs|default([]) + [host_item] }}"
  36. with_items: "{{ sync_file_hosts | unique }}"
  37. loop_control:
  38. loop_var: host_item
  39. when: sync_file_is_cert|d() and hostvars[host_item]["sync_file_key_stat"]["stat"]["exists"]|bool
  40. - name: "sync_file | Remove sync sources with files that do not match sync_file_srcs|first"
  41. set_fact:
  42. _: "{% if inventory_hostname in sync_file_srcs %}{{ sync_file_srcs.remove(inventory_hostname) }}{% endif %}"
  43. when: >-
  44. sync_file_srcs|d([])|length > 1 and
  45. inventory_hostname != sync_file_srcs|first and
  46. sync_file_stat.stat.get("checksum") != hostvars[sync_file_srcs|first]["sync_file_stat"]["stat"]["checksum"]
  47. - name: "sync_file | Remove sync sources with keys that do not match sync_file_srcs|first"
  48. set_fact:
  49. _: "{% if inventory_hostname in sync_file_srcs %}{{ sync_file_srcs.remove(inventory_hostname) }}{% endif %}"
  50. when: >-
  51. sync_file_is_cert|d() and
  52. sync_file_key_srcs|d([])|length > 1 and
  53. inventory_hostname != sync_file_key_srcs|first and
  54. sync_file_key_stat.stat.checksum != hostvars[sync_file_srcs|first]["sync_file_key_stat"]["stat"]["checksum"]
  55. - name: "sync_file | Consolidate file and key sources"
  56. set_fact:
  57. sync_file_srcs: "{{ sync_file_srcs|d([]) | intersect(sync_file_key_srcs) }}"
  58. when: sync_file_is_cert|d()
  59. - name: "sync_file | Set facts for situations where sync is not needed"
  60. set_fact:
  61. sync_file_no_srcs: "{{ true if sync_file_srcs|d([])|length == 0 else false }}"
  62. sync_file_unneeded: "{{ true if sync_file_srcs|d([])|length == sync_file_hosts|length else false }}"
  63. - name: "sync_file | Set sync_file_result fact"
  64. set_fact:
  65. sync_file_result:
  66. no_srcs: "{{ sync_file_no_srcs }}"
  67. path: "{{ sync_file_path }}"
  68. sync_unneeded: "{{ sync_file_unneeded }}"
  69. - name: "sync_file | Update sync_file_results fact"
  70. set_fact:
  71. sync_file_results: "{{ sync_file_results|default([]) + [sync_file_result] }}"
  72. - include: sync.yml
  73. when: not (sync_file_no_srcs or sync_file_unneeded)
  74. - name: "Unset local vars to avoid variable bleed into next iteration"
  75. set_fact:
  76. sync_file: ''
  77. sync_file_dir: ''
  78. sync_file_key_path: ''
  79. sync_file_key_srcs: []
  80. sync_file_path: ''
  81. sync_file_srcs: []