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.

212 lines
7.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. ---
  2. - name: Calico | Write Calico cni config
  3. template:
  4. src: "cni-calico.conf.j2"
  5. dest: "/etc/cni/net.d/10-calico.conf"
  6. owner: kube
  7. - name: Calico | Set docker daemon options
  8. template:
  9. src: docker
  10. dest: "/etc/default/docker"
  11. owner: root
  12. group: root
  13. mode: 0644
  14. notify:
  15. - restart docker
  16. when: ansible_os_family != "CoreOS"
  17. - meta: flush_handlers
  18. - name: Calico | Create calico certs directory
  19. file:
  20. dest: "{{ calico_cert_dir }}"
  21. state: directory
  22. mode: 0750
  23. owner: root
  24. group: root
  25. - name: Calico | Link etcd certificates for calico-node
  26. file:
  27. src: "{{ etcd_cert_dir }}/{{ item.s }}"
  28. dest: "{{ calico_cert_dir }}/{{ item.d }}"
  29. state: hard
  30. with_items:
  31. - {s: "ca.pem", d: "ca_cert.crt"}
  32. - {s: "node.pem", d: "cert.crt"}
  33. - {s: "node-key.pem", d: "key.pem"}
  34. - name: Calico | Install calicoctl container script
  35. template:
  36. src: calicoctl-container.j2
  37. dest: "{{ bin_dir }}/calicoctl"
  38. mode: 0755
  39. owner: root
  40. group: root
  41. changed_when: false
  42. notify: restart calico-node
  43. - name: Calico | Copy cni plugins from hyperkube
  44. command: "/usr/bin/docker run --rm -v /opt/cni/bin:/cnibindir {{ hyperkube_image_repo }}:{{ hyperkube_image_tag }} /usr/bin/rsync -a /opt/cni/bin/ /cnibindir/"
  45. register: cni_task_result
  46. until: cni_task_result.rc == 0
  47. retries: 4
  48. delay: "{{ retry_stagger | random + 3 }}"
  49. changed_when: false
  50. - name: Calico | Copy cni plugins from calico/cni container
  51. command: "/usr/bin/docker run --rm -v /opt/cni/bin:/cnibindir {{ calico_cni_image_repo }}:{{ calico_cni_image_tag }} sh -c 'cp -a /opt/cni/bin/* /cnibindir/'"
  52. register: cni_task_result
  53. until: cni_task_result.rc == 0
  54. retries: 4
  55. delay: "{{ retry_stagger | random + 3 }}"
  56. changed_when: false
  57. when: "{{ overwrite_hyperkube_cni|bool }}"
  58. - name: Calico | wait for etcd
  59. uri: url=https://localhost:2379/health validate_certs=no
  60. register: result
  61. until: result.status == 200 or result.status == 401
  62. retries: 10
  63. delay: 5
  64. delegate_to: "{{groups['etcd'][0]}}"
  65. run_once: true
  66. - name: Calico | Check if calico network pool has already been configured
  67. command: |-
  68. curl \
  69. --cacert {{ etcd_cert_dir }}/ca.pem \
  70. --cert {{ etcd_cert_dir}}/admin.pem \
  71. --key {{ etcd_cert_dir }}/admin-key.pem \
  72. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  73. register: calico_conf
  74. delegate_to: "{{groups['etcd'][0]}}"
  75. run_once: true
  76. - name: Calico | Check calicoctl version
  77. run_once: true
  78. set_fact:
  79. legacy_calicoctl: "{{ calicoctl_image_tag | version_compare('v1.0.0', '<') }}"
  80. - name: Calico | Configure calico network pool
  81. shell: >
  82. echo '{
  83. "kind": "ipPool",
  84. "spec": {"disabled": false, "ipip": {"enabled": {{ cloud_provider is defined or ipip }}},
  85. "nat-outgoing": {{ nat_outgoing|default(false) and not peer_with_router|default(false) }}},
  86. "apiVersion": "v1",
  87. "metadata": {"cidr": "{{ kube_pods_subnet }}"}
  88. }'
  89. | {{ bin_dir }}/calicoctl create -f -
  90. environment:
  91. NO_DEFAULT_POOLS: true
  92. run_once: true
  93. when: (not legacy_calicoctl and
  94. "Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  95. - name: Calico (old) | Define ipip pool argument
  96. run_once: true
  97. set_fact:
  98. ipip_arg: "--ipip"
  99. when: (legacy_calicoctl and
  100. cloud_provider is defined or ipip)
  101. - name: Calico (old) | Define nat-outgoing pool argument
  102. run_once: true
  103. set_fact:
  104. nat_arg: "--nat-outgoing"
  105. when: (legacy_calicoctl and
  106. nat_outgoing|default(false) and not peer_with_router|default(false))
  107. - name: Calico (old) | Define calico pool task name
  108. run_once: true
  109. set_fact:
  110. pool_task_name: "with options {{ ipip_arg|default('') }} {{ nat_arg|default('') }}"
  111. when: (legacy_calicoctl and ipip_arg|default(false) or nat_arg|default(false))
  112. - name: Calico (old) | Configure calico network pool {{ pool_task_name|default('') }}
  113. command: "{{ bin_dir}}/calicoctl pool add {{ kube_pods_subnet }} {{ ipip_arg|default('') }} {{ nat_arg|default('') }}"
  114. environment:
  115. NO_DEFAULT_POOLS: true
  116. run_once: true
  117. when: (legacy_calicoctl and
  118. "Key not found" in calico_conf.stdout or "nodes" not in calico_conf.stdout)
  119. - name: Calico | Get calico configuration from etcd
  120. command: |-
  121. curl \
  122. --cacert {{ etcd_cert_dir }}/ca.pem \
  123. --cert {{ etcd_cert_dir}}/admin.pem \
  124. --key {{ etcd_cert_dir }}/admin-key.pem \
  125. https://localhost:2379/v2/keys/calico/v1/ipam/v4/pool
  126. register: calico_pools_raw
  127. delegate_to: "{{groups['etcd'][0]}}"
  128. run_once: true
  129. - set_fact:
  130. calico_pools: "{{ calico_pools_raw.stdout | from_json }}"
  131. run_once: true
  132. - name: Calico | Check if calico pool is properly configured
  133. fail:
  134. msg: 'Only one network pool must be configured and it must be the subnet {{ kube_pods_subnet }}.
  135. Please erase calico configuration and run the playbook again ("etcdctl rm --recursive /calico/v1/ipam/v4/pool")'
  136. when: ( calico_pools['node']['nodes'] | length > 1 ) or
  137. ( not calico_pools['node']['nodes'][0]['key'] | search(".*{{ kube_pods_subnet | ipaddr('network') }}.*") )
  138. run_once: true
  139. - name: Calico | Write /etc/network-environment
  140. template: src=network-environment.j2 dest=/etc/network-environment
  141. when: ansible_service_mgr in ["sysvinit","upstart"]
  142. - name: Calico | Write calico-node systemd init file
  143. template: src=calico-node.service.j2 dest=/etc/systemd/system/calico-node.service
  144. when: ansible_service_mgr == "systemd"
  145. notify: restart calico-node
  146. - name: Calico | Write calico-node initd script
  147. template: src=deb-calico.initd.j2 dest=/etc/init.d/calico-node owner=root mode=0755
  148. when: ansible_service_mgr in ["sysvinit","upstart"] and ansible_os_family == "Debian"
  149. notify: restart calico-node
  150. - name: Calico | Write calico-node initd script
  151. template: src=rh-calico.initd.j2 dest=/etc/init.d/calico-node owner=root mode=0755
  152. when: ansible_service_mgr in ["sysvinit","upstart"] and ansible_os_family == "RedHat"
  153. notify: restart calico-node
  154. - meta: flush_handlers
  155. - name: Calico | Enable calico-node
  156. service:
  157. name: calico-node
  158. state: started
  159. enabled: yes
  160. - name: Calico | Disable node mesh
  161. shell: "{{ bin_dir }}/calicoctl config set nodeToNodeMesh off"
  162. when: (not legacy_calicoctl and
  163. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])
  164. - name: Calico | Configure peering with router(s)
  165. shell: >
  166. echo '{
  167. "kind": "bgppeer",
  168. "spec": {"asNumber": {{ item.as }}},
  169. "apiVersion": "v1",
  170. "metadata": {"node": "rack1-host1", "scope": "node", "peerIP": "{{ item.router_id }}"}
  171. }'
  172. | {{ bin_dir }}/calicoctl create -f -
  173. with_items: peers
  174. when: (not legacy_calicoctl and
  175. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])
  176. - name: Calico (old) | Disable node mesh
  177. shell: "{{ bin_dir }}/calicoctl bgp node-mesh off"
  178. when: (legacy_calicoctl and
  179. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])
  180. - name: Calico (old) | Configure peering with router(s)
  181. shell: "{{ bin_dir }}/calicoctl node bgp peer add {{ item.router_id }} as {{ item.as }}"
  182. with_items: peers
  183. when: (legacy_calicoctl and
  184. peer_with_router|default(false) and inventory_hostname in groups['kube-node'])