backup.yml 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ---
  2. - name: Backup etcd
  3. hosts: etcd_hosts_to_backup
  4. vars:
  5. embedded_etcd: "{{ groups.oo_etcd_to_config | default([]) | length == 0 }}"
  6. timestamp: "{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
  7. etcdctl_command: "{{ 'etcdctl' if not openshift.common.is_containerized or embedded_etcd else 'docker exec etcd_container etcdctl' }}"
  8. roles:
  9. - openshift_facts
  10. tasks:
  11. # Ensure we persist the etcd role for this host in openshift_facts
  12. - openshift_facts:
  13. role: etcd
  14. local_facts: {}
  15. when: "'etcd' not in openshift"
  16. - stat: path=/var/lib/openshift
  17. register: var_lib_openshift
  18. - stat: path=/var/lib/origin
  19. register: var_lib_origin
  20. - name: Create origin symlink if necessary
  21. file: src=/var/lib/openshift/ dest=/var/lib/origin state=link
  22. when: var_lib_openshift.stat.exists == True and var_lib_origin.stat.exists == False
  23. # TODO: replace shell module with command and update later checks
  24. # We assume to be using the data dir for all backups.
  25. - name: Check available disk space for etcd backup
  26. shell: df --output=avail -k {{ openshift.common.data_dir }} | tail -n 1
  27. register: avail_disk
  28. # TODO: replace shell module with command and update later checks
  29. - name: Check current embedded etcd disk usage
  30. shell: du -k {{ openshift.etcd.etcd_data_dir }} | tail -n 1 | cut -f1
  31. register: etcd_disk_usage
  32. when: embedded_etcd | bool
  33. - name: Abort if insufficient disk space for etcd backup
  34. fail:
  35. msg: >
  36. {{ etcd_disk_usage.stdout }} Kb disk space required for etcd backup,
  37. {{ avail_disk.stdout }} Kb available.
  38. when: (embedded_etcd | bool) and (etcd_disk_usage.stdout|int > avail_disk.stdout|int)
  39. # For non containerized and non embedded we should have the correct version of
  40. # etcd installed already. So don't do anything.
  41. #
  42. # For embedded or containerized we need to use the latest because OCP 3.3 uses
  43. # a version of etcd that can only be backed up with etcd-3.x and if it's
  44. # containerized then etcd version may be newer than that on the host so
  45. # upgrade it.
  46. #
  47. # On atomic we have neither yum nor dnf so ansible throws a hard to debug error
  48. # if you use package there, like this: "Could not find a module for unknown."
  49. # see https://bugzilla.redhat.com/show_bug.cgi?id=1408668
  50. #
  51. # TODO - We should refactor all containerized backups to use the containerized
  52. # version of etcd to perform the backup rather than relying on the host's
  53. # binaries. Until we do that we'll continue to have problems backing up etcd
  54. # when atomic host has an older version than the version that's running in the
  55. # container whether that's embedded or not
  56. - name: Install latest etcd for containerized or embedded
  57. package:
  58. name: etcd
  59. state: latest
  60. when: ( embedded_etcd | bool or openshift.common.is_containerized ) and not openshift.common.is_atomic
  61. - name: Generate etcd backup
  62. command: >
  63. {{ etcdctl_command }} backup --data-dir={{ openshift.etcd.etcd_data_dir }}
  64. --backup-dir={{ openshift.common.data_dir }}/etcd-backup-{{ backup_tag | default('') }}{{ timestamp }}
  65. - set_fact:
  66. etcd_backup_complete: True
  67. - name: Display location of etcd backup
  68. debug:
  69. msg: "Etcd backup created in {{ openshift.common.data_dir }}/etcd-backup-{{ backup_tag | default('') }}{{ timestamp }}"
  70. - name: Gate on etcd backup
  71. hosts: localhost
  72. connection: local
  73. become: no
  74. tasks:
  75. - set_fact:
  76. etcd_backup_completed: "{{ hostvars
  77. | oo_select_keys(groups.etcd_hosts_to_backup)
  78. | oo_collect('inventory_hostname', {'etcd_backup_complete': true}) }}"
  79. - set_fact:
  80. etcd_backup_failed: "{{ groups.etcd_hosts_to_backup | difference(etcd_backup_completed) }}"
  81. - fail:
  82. msg: "Upgrade cannot continue. The following hosts did not complete etcd backup: {{ etcd_backup_failed | join(',') }}"
  83. when: etcd_backup_failed | length > 0