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.

93 lines
3.4 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: sync_file_key_path is not defined or sync_file_key_path == ''
  16. - name: "sync_file | Check if {{sync_file_path}} file exists"
  17. stat:
  18. path: "{{ sync_file_path }}"
  19. register: sync_file_stat
  20. - name: "sync_file | Check if {{ sync_file_key_path }} key file exists"
  21. stat:
  22. path: "{{ sync_file_key_path }}"
  23. register: sync_file_key_stat
  24. - name: "sync_file | Combine all possible file sync sources"
  25. set_fact:
  26. sync_file_srcs: "{{ sync_file_srcs|default([]) + [host_item] }}"
  27. with_items: "{{ sync_file_hosts|default() | unique }}"
  28. loop_control:
  29. loop_var: host_item
  30. when: sync_file_stat.stat.exists|default()
  31. - name: "sync_file | Combine all possible key file sync sources"
  32. set_fact:
  33. sync_file_key_srcs: "{{ sync_file_key_srcs|default([]) + [host_item] }}"
  34. with_items: "{{ sync_file_hosts|default() | unique }}"
  35. loop_control:
  36. loop_var: host_item
  37. when: sync_file_key_stat.stat.exists|default()
  38. - name: "sync_file | Remove sync sources with files that do not match sync_file_srcs|first"
  39. set_fact:
  40. _: "{% if inventory_hostname in sync_file_srcs %}{{ sync_file_srcs.remove(inventory_hostname) }}{% endif %}"
  41. when: >-
  42. sync_file_srcs|d([])|length > 1 and
  43. inventory_hostname != sync_file_srcs|first
  44. - name: "sync_file | Remove sync sources with keys that do not match sync_file_srcs|first"
  45. set_fact:
  46. _: "{% if inventory_hostname in sync_file_srcs %}{{ sync_file_srcs.remove(inventory_hostname) }}{% endif %}"
  47. when: >-
  48. sync_file_is_cert|d() and
  49. sync_file_key_srcs|d([])|length > 1 and
  50. inventory_hostname != sync_file_key_srcs|first
  51. - name: "sync_file | Consolidate file and key sources"
  52. set_fact:
  53. sync_file_srcs: "{{ sync_file_srcs|d([]) | intersect(sync_file_key_srcs) }}"
  54. when: sync_file_is_cert|d()
  55. - name: "sync_file | Set facts for situations where sync is not needed"
  56. set_fact:
  57. sync_file_no_srcs: "{{ true if sync_file_srcs|d([])|length == 0 else false }}"
  58. sync_file_unneeded: "{{ true if sync_file_srcs|d([])|length == sync_file_hosts|length else false }}"
  59. - name: "sync_file | Set sync_file_result fact"
  60. set_fact:
  61. sync_file_result:
  62. no_srcs: "{{ sync_file_no_srcs }}"
  63. path: "{{ sync_file_path }}"
  64. sync_unneeded: "{{ sync_file_unneeded }}"
  65. - name: "sync_file | Update sync_file_results fact"
  66. set_fact:
  67. sync_file_results: "{{ sync_file_results|default([]) + [sync_file_result] }}"
  68. - include_tasks: sync.yml
  69. when: not (sync_file_no_srcs or sync_file_unneeded)
  70. - name: "Unset local vars to avoid variable bleed into next iteration"
  71. set_fact:
  72. sync_file: ''
  73. sync_file_dir: ''
  74. sync_file_key_path: ''
  75. sync_file_key_srcs: []
  76. sync_file_path: ''
  77. sync_file_srcs: []