add-docker-registry.yml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ---
  2. - hosts: OSEv3
  3. become: true
  4. vars:
  5. registries: []
  6. insecure_registries: []
  7. tasks:
  8. - name: Check if docker is even installed
  9. command: docker
  10. - name: Install atomic-registries package
  11. yum:
  12. name: atomic-registries
  13. state: latest
  14. - name: Get registry configuration file
  15. register: file_result
  16. stat:
  17. path: /etc/containers/registries.conf
  18. - name: Check if it exists
  19. assert:
  20. that: 'file_result.stat.exists'
  21. msg: "Configuration file does not exist."
  22. - name: Load configuration file
  23. shell: cat /etc/containers/registries.conf
  24. register: file_content
  25. - name: Store file content into a variable
  26. set_fact:
  27. docker_conf: "{{ file_content.stdout | from_yaml }}"
  28. - name: Make sure that docker file content is a dictionary
  29. when: '(docker_conf is string) and (not docker_conf)'
  30. set_fact:
  31. docker_conf: {}
  32. - name: Make sure that registries is a list
  33. when: 'registries is string'
  34. set_fact:
  35. registries_list: [ "{{ registries }}" ]
  36. - name: Make sure that insecure_registries is a list
  37. when: 'insecure_registries is string'
  38. set_fact:
  39. insecure_registries_list: [ "{{ insecure_registries }}" ]
  40. - name: Set default values if there are no registries defined
  41. set_fact:
  42. docker_conf_registries: "{{ [] if docker_conf['registries'] is not defined else docker_conf['registries'] }}"
  43. docker_conf_insecure_registries: "{{ [] if docker_conf['insecure_registries'] is not defined else docker_conf['insecure_registries'] }}"
  44. - name: Add other registries
  45. when: 'registries_list is not defined'
  46. register: registries_merge_result
  47. set_fact:
  48. docker_conf: "{{ docker_conf | combine({'registries': (docker_conf_registries + registries) | unique}, recursive=True) }}"
  49. - name: Add other registries (if registries had to be converted)
  50. when: 'registries_merge_result|skipped'
  51. set_fact:
  52. docker_conf: "{{ docker_conf | combine({'registries': (docker_conf_registries + registries_list) | unique}, recursive=True) }}"
  53. - name: Add insecure registries
  54. when: 'insecure_registries_list is not defined'
  55. register: insecure_registries_merge_result
  56. set_fact:
  57. docker_conf: "{{ docker_conf | combine({'insecure_registries': (docker_conf_insecure_registries + insecure_registries) | unique }, recursive=True) }}"
  58. - name: Add insecure registries (if insecure_registries had to be converted)
  59. when: 'insecure_registries_merge_result|skipped'
  60. set_fact:
  61. docker_conf: "{{ docker_conf | combine({'insecure_registries': (docker_conf_insecure_registries + insecure_registries_list) | unique }, recursive=True) }}"
  62. - name: Load variable back to file
  63. copy:
  64. content: "{{ docker_conf | to_yaml }}"
  65. dest: /etc/containers/registries.conf
  66. - name: Restart registries service
  67. service:
  68. name: registries
  69. state: restarted
  70. - name: Restart docker
  71. service:
  72. name: docker
  73. state: restarted