main.yaml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. ---
  2. - name: Validate Elasticsearch cluster size
  3. fail: msg="The openshift_logging_es_cluster_size may only be scaled down manually. Please see official documentation on how to do this."
  4. when: openshift_logging_facts.elasticsearch.deploymentconfigs | length > openshift_logging_es_cluster_size|int
  5. - name: Validate Elasticsearch Ops cluster size
  6. fail: msg="The openshift_logging_es_ops_cluster_size may only be scaled down manually. Please see official documentation on how to do this."
  7. when: openshift_logging_facts.elasticsearch_ops.deploymentconfigs | length > openshift_logging_es_ops_cluster_size|int
  8. - fail:
  9. msg: Invalid deployment type, one of ['data-master', 'data-client', 'master', 'client'] allowed
  10. when: not openshift_logging_elasticsearch_deployment_type in __allowed_es_types
  11. - set_fact:
  12. elasticsearch_name: "{{ 'logging-elasticsearch' ~ ( (openshift_logging_elasticsearch_ops_deployment | default(false) | bool) | ternary('-ops', '')) }}"
  13. es_component: "{{ 'es' ~ ( (openshift_logging_elasticsearch_ops_deployment | default(false) | bool) | ternary('-ops', '') ) }}"
  14. - name: Set default image variables based on openshift_deployment_type
  15. include_vars: "{{ var_file_name }}"
  16. with_first_found:
  17. - "{{ openshift_deployment_type }}.yml"
  18. - "default_images.yml"
  19. loop_control:
  20. loop_var: var_file_name
  21. - name: Set elasticsearch image facts
  22. set_fact:
  23. openshift_logging_elasticsearch_proxy_image_prefix: "{{ openshift_logging_elasticsearch_proxy_image_prefix | default(__openshift_logging_elasticsearch_proxy_image_prefix) }}"
  24. openshift_logging_elasticsearch_proxy_image_version: "{{ openshift_logging_elasticsearch_proxy_image_version | default(__openshift_logging_elasticsearch_proxy_image_version) }}"
  25. openshift_logging_elasticsearch_image_prefix: "{{ openshift_logging_elasticsearch_image_prefix | default(__openshift_logging_elasticsearch_image_prefix) }}"
  26. openshift_logging_elasticsearch_image_version: "{{ openshift_logging_elasticsearch_image_version | default(__openshift_logging_elasticsearch_image_version) }}"
  27. - include_tasks: determine_version.yaml
  28. # allow passing in a tempdir
  29. - name: Create temp directory for doing work in
  30. command: mktemp -d /tmp/openshift-logging-ansible-XXXXXX
  31. register: mktemp
  32. changed_when: False
  33. - set_fact:
  34. tempdir: "{{ mktemp.stdout }}"
  35. # This may not be necessary in this role
  36. - name: Create templates subdirectory
  37. file:
  38. state: directory
  39. path: "{{ tempdir }}/templates"
  40. mode: 0755
  41. changed_when: False
  42. # we want to make sure we have all the necessary components here
  43. # service account
  44. - name: Create ES service account
  45. oc_serviceaccount:
  46. state: present
  47. name: "aggregated-logging-elasticsearch"
  48. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  49. image_pull_secrets: "{{ openshift_logging_image_pull_secret }}"
  50. when: openshift_logging_image_pull_secret != ''
  51. - name: Create ES service account
  52. oc_serviceaccount:
  53. state: present
  54. name: "aggregated-logging-elasticsearch"
  55. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  56. when:
  57. - openshift_logging_image_pull_secret == ''
  58. # rolebinding reader
  59. - copy:
  60. src: rolebinding-reader.yml
  61. dest: "{{ tempdir }}/rolebinding-reader.yml"
  62. - name: Create rolebinding-reader role
  63. oc_obj:
  64. state: present
  65. name: "rolebinding-reader"
  66. kind: clusterrole
  67. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  68. files:
  69. - "{{ tempdir }}/rolebinding-reader.yml"
  70. delete_after: true
  71. # SA roles
  72. - name: Set rolebinding-reader permissions for ES
  73. oc_adm_policy_user:
  74. state: present
  75. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  76. resource_kind: cluster-role
  77. resource_name: rolebinding-reader
  78. user: "system:serviceaccount:{{ openshift_logging_elasticsearch_namespace }}:aggregated-logging-elasticsearch"
  79. - oc_adm_policy_user:
  80. state: present
  81. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  82. resource_kind: cluster-role
  83. resource_name: system:auth-delegator
  84. user: "system:serviceaccount:{{ openshift_logging_elasticsearch_namespace}}:aggregated-logging-elasticsearch"
  85. # logging-metrics-reader role
  86. - template:
  87. src: logging-metrics-role.j2
  88. dest: "{{mktemp.stdout}}/templates/logging-metrics-role.yml"
  89. vars:
  90. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  91. role_namespace: "{{ openshift_logging_elasticsearch_prometheus_sa | serviceaccount_namespace(openshift_logging_elasticsearch_namespace) }}"
  92. role_user: "{{ openshift_logging_elasticsearch_prometheus_sa | serviceaccount_name }}"
  93. - name: Create logging-metrics-reader-role
  94. command: >
  95. {{ openshift_client_binary }}
  96. --config={{ openshift.common.config_base }}/master/admin.kubeconfig
  97. -n "{{ openshift_logging_elasticsearch_namespace }}"
  98. create -f "{{mktemp.stdout}}/templates/logging-metrics-role.yml"
  99. register: prometheus_out
  100. check_mode: no
  101. ignore_errors: yes
  102. - fail:
  103. msg: "There was an error creating the logging-metrics-role and binding: {{prometheus_out}}"
  104. when:
  105. - "prometheus_out.stderr | length > 0"
  106. - "'already exists' not in prometheus_out.stderr"
  107. # View role and binding
  108. - name: Generate logging-elasticsearch-view-role
  109. template:
  110. src: rolebinding.j2
  111. dest: "{{mktemp.stdout}}/logging-elasticsearch-view-role.yaml"
  112. vars:
  113. obj_name: logging-elasticsearch-view-role
  114. roleRef:
  115. name: view
  116. subjects:
  117. - kind: ServiceAccount
  118. name: aggregated-logging-elasticsearch
  119. changed_when: no
  120. - name: Set logging-elasticsearch-view-role role
  121. oc_obj:
  122. state: present
  123. name: "logging-elasticsearch-view-role"
  124. kind: rolebinding
  125. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  126. files:
  127. - "{{ tempdir }}/logging-elasticsearch-view-role.yaml"
  128. delete_after: true
  129. # configmap
  130. - assert:
  131. that:
  132. - openshift_logging_elasticsearch_kibana_index_mode in __kibana_index_modes
  133. msg: "The openshift_logging_elasticsearch_kibana_index_mode '{{ openshift_logging_elasticsearch_kibana_index_mode }}' only supports one of: {{ __kibana_index_modes | join(', ') }}"
  134. - assert:
  135. that:
  136. - "{{ openshift_logging_es_log_appenders | length > 0 }}"
  137. msg: "The openshift_logging_es_log_appenders '{{ openshift_logging_es_log_appenders }}' has an unrecognized option and only supports the following as a list: {{ __es_log_appenders | join(', ') }}"
  138. - template:
  139. src: elasticsearch-logging.yml.j2
  140. dest: "{{ tempdir }}/elasticsearch-logging.yml"
  141. vars:
  142. root_logger: "{{openshift_logging_es_log_appenders | join(', ')}}"
  143. when: es_logging_contents is undefined
  144. changed_when: no
  145. - template:
  146. src: elasticsearch.yml.j2
  147. dest: "{{ tempdir }}/elasticsearch.yml"
  148. vars:
  149. allow_cluster_reader: "{{ openshift_logging_elasticsearch_ops_allow_cluster_reader | lower | default('false') }}"
  150. es_number_of_shards: "{{ openshift_logging_es_number_of_shards | default(1) }}"
  151. es_number_of_replicas: "{{ openshift_logging_es_number_of_replicas| default(0) }}"
  152. es_kibana_index_mode: "{{ openshift_logging_elasticsearch_kibana_index_mode | default('unique') }}"
  153. when: es_config_contents is undefined
  154. changed_when: no
  155. # create diff between current configmap files and our current files
  156. - import_role:
  157. name: openshift_logging
  158. tasks_from: patch_configmap_files.yaml
  159. vars:
  160. configmap_name: "logging-elasticsearch"
  161. configmap_namespace: "logging"
  162. configmap_file_names:
  163. - current_file: "elasticsearch.yml"
  164. new_file: "{{ tempdir }}/elasticsearch.yml"
  165. protected_lines: ["number_of_shards", "number_of_replicas"]
  166. - current_file: "logging.yml"
  167. new_file: "{{ tempdir }}/elasticsearch-logging.yml"
  168. - name: Set ES configmap
  169. oc_configmap:
  170. state: present
  171. name: "{{ elasticsearch_name }}"
  172. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  173. from_file:
  174. elasticsearch.yml: "{{ tempdir }}/elasticsearch.yml"
  175. logging.yml: "{{ tempdir }}/elasticsearch-logging.yml"
  176. register: es_config_creation
  177. notify: "restart elasticsearch"
  178. - when: es_config_creation.changed | bool
  179. block:
  180. - set_fact:
  181. _restart_logging_components: "{{ _restart_logging_components | default([]) + [es_component] | unique }}"
  182. - shell: >
  183. oc get dc -l component="{{ es_component }}" -n "{{ openshift_logging_elasticsearch_namespace }}" -o name | cut -d'/' -f2
  184. register: _es_dcs
  185. - set_fact:
  186. _restart_logging_nodes: "{{ _restart_logging_nodes | default([]) + [_es_dcs.stdout] | unique }}"
  187. when: _es_dcs.stdout != ""
  188. # secret
  189. - name: Set ES secret
  190. oc_secret:
  191. state: present
  192. name: "logging-elasticsearch"
  193. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  194. files:
  195. - name: key
  196. path: "{{ generated_certs_dir }}/logging-es.jks"
  197. - name: truststore
  198. path: "{{ generated_certs_dir }}/truststore.jks"
  199. - name: searchguard.key
  200. path: "{{ generated_certs_dir }}/elasticsearch.jks"
  201. - name: searchguard.truststore
  202. path: "{{ generated_certs_dir }}/truststore.jks"
  203. - name: admin-key
  204. path: "{{ generated_certs_dir }}/system.admin.key"
  205. - name: admin-cert
  206. path: "{{ generated_certs_dir }}/system.admin.crt"
  207. - name: admin-ca
  208. path: "{{ generated_certs_dir }}/ca.crt"
  209. - name: admin.jks
  210. path: "{{ generated_certs_dir }}/system.admin.jks"
  211. # services
  212. - name: Set logging-{{ es_component }}-cluster service
  213. oc_service:
  214. state: present
  215. name: "logging-{{ es_component }}-cluster"
  216. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  217. selector:
  218. component: "{{ es_component }}"
  219. provider: openshift
  220. labels:
  221. logging-infra: 'support'
  222. ports:
  223. - port: 9300
  224. - name: Set logging-{{ es_component }} service
  225. oc_service:
  226. state: present
  227. name: "logging-{{ es_component }}"
  228. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  229. selector:
  230. component: "{{ es_component }}"
  231. provider: openshift
  232. labels:
  233. logging-infra: 'support'
  234. ports:
  235. - port: 9200
  236. targetPort: "restapi"
  237. - name: Set logging-{{ es_component}}-prometheus service
  238. oc_service:
  239. state: present
  240. name: "logging-{{es_component}}-prometheus"
  241. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  242. labels:
  243. logging-infra: 'support'
  244. ports:
  245. - name: proxy
  246. port: 443
  247. targetPort: 4443
  248. selector:
  249. component: "{{ es_component }}"
  250. provider: openshift
  251. - oc_edit:
  252. kind: service
  253. name: "logging-{{es_component}}-prometheus"
  254. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  255. separator: '#'
  256. content:
  257. metadata#annotations#service.alpha.openshift.io/serving-cert-secret-name: "prometheus-tls"
  258. metadata#annotations#prometheus.io/scrape: "true"
  259. metadata#annotations#prometheus.io/scheme: "https"
  260. metadata#annotations#prometheus.io/path: "_prometheus/metrics"
  261. - name: Check to see if PVC already exists
  262. oc_obj:
  263. state: list
  264. kind: pvc
  265. name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  266. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  267. register: logging_elasticsearch_pvc
  268. # logging_elasticsearch_pvc.results.results | length > 0 returns a false positive
  269. # so we check for the presence of 'stderr' to determine if the obj exists or not
  270. # the RC for existing and not existing is both 0
  271. - when:
  272. - logging_elasticsearch_pvc.results.stderr is defined
  273. - openshift_logging_elasticsearch_storage_type == "pvc"
  274. block:
  275. # storageclasses are used by default but if static then disable
  276. # storageclasses with the storageClassName set to "" in pvc.j2
  277. - name: Creating ES storage template - static
  278. template:
  279. src: pvc.j2
  280. dest: "{{ tempdir }}/templates/logging-es-pvc.yml"
  281. vars:
  282. obj_name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  283. size: "{{ (openshift_logging_elasticsearch_pvc_size | trim | length == 0) | ternary('10Gi', openshift_logging_elasticsearch_pvc_size) }}"
  284. access_modes: "{{ openshift_logging_elasticsearch_pvc_access_modes | list }}"
  285. pv_selector: "{{ openshift_logging_elasticsearch_pvc_pv_selector }}"
  286. storage_class_name: "{{ openshift_logging_elasticsearch_pvc_storage_class_name | default('', true) }}"
  287. when:
  288. - not openshift_logging_elasticsearch_pvc_dynamic | bool
  289. # Storageclasses are used by default if configured
  290. - name: Creating ES storage template - dynamic
  291. template:
  292. src: pvc.j2
  293. dest: "{{ tempdir }}/templates/logging-es-pvc.yml"
  294. vars:
  295. obj_name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  296. size: "{{ (openshift_logging_elasticsearch_pvc_size | trim | length == 0) | ternary('10Gi', openshift_logging_elasticsearch_pvc_size) }}"
  297. access_modes: "{{ openshift_logging_elasticsearch_pvc_access_modes | list }}"
  298. pv_selector: "{{ openshift_logging_elasticsearch_pvc_pv_selector }}"
  299. when:
  300. - openshift_logging_elasticsearch_pvc_dynamic | bool
  301. - name: Set ES storage
  302. oc_obj:
  303. state: present
  304. kind: pvc
  305. name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  306. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  307. files:
  308. - "{{ tempdir }}/templates/logging-es-pvc.yml"
  309. delete_after: true
  310. - set_fact:
  311. es_deploy_name: "logging-{{ es_component }}-{{ openshift_logging_elasticsearch_deployment_type }}-{{ 8 | lib_utils_oo_random_word('abcdefghijklmnopqrstuvwxyz0123456789') }}"
  312. when: openshift_logging_elasticsearch_deployment_name == ""
  313. - set_fact:
  314. es_deploy_name: "{{ openshift_logging_elasticsearch_deployment_name }}"
  315. when: openshift_logging_elasticsearch_deployment_name != ""
  316. # DC
  317. - name: Set ES dc templates
  318. template:
  319. src: es.j2
  320. dest: "{{ tempdir }}/templates/logging-es-dc.yml"
  321. vars:
  322. es_cluster_name: "{{ es_component }}"
  323. component: "{{ es_component }}"
  324. logging_component: elasticsearch
  325. deploy_name: "{{ es_deploy_name }}"
  326. image: "{{ openshift_logging_elasticsearch_image_prefix }}logging-elasticsearch:{{ openshift_logging_elasticsearch_image_version }}"
  327. proxy_image: "{{ openshift_logging_elasticsearch_proxy_image_prefix }}oauth-proxy:{{ openshift_logging_elasticsearch_proxy_image_version }}"
  328. es_cpu_limit: "{{ openshift_logging_elasticsearch_cpu_limit | default('') }}"
  329. es_cpu_request: "{{ openshift_logging_elasticsearch_cpu_request | min_cpu(openshift_logging_elasticsearch_cpu_limit | default(none)) }}"
  330. es_memory_limit: "{{ openshift_logging_elasticsearch_memory_limit }}"
  331. es_node_selector: "{{ openshift_logging_elasticsearch_nodeselector | default({}) }}"
  332. es_storage_groups: "{{ openshift_logging_elasticsearch_storage_group | default([]) }}"
  333. es_container_security_context: "{{ _es_containers.elasticsearch.securityContext if _es_containers is defined and 'elasticsearch' in _es_containers and 'securityContext' in _es_containers.elasticsearch else None }}"
  334. deploy_type: "{{ openshift_logging_elasticsearch_deployment_type }}"
  335. es_replicas: 1
  336. - name: Set ES dc
  337. oc_obj:
  338. state: present
  339. name: "{{ es_deploy_name }}"
  340. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  341. kind: dc
  342. files:
  343. - "{{ tempdir }}/templates/logging-es-dc.yml"
  344. delete_after: true
  345. register: es_dc_creation
  346. notify: "restart elasticsearch"
  347. - set_fact:
  348. _restart_logging_components: "{{ _restart_logging_components | default([]) + [es_component] | unique }}"
  349. _restart_logging_nodes: "{{ _restart_logging_nodes | default([]) + [es_deploy_name] | unique }}"
  350. when: es_dc_creation.changed | bool
  351. - name: Retrieving the cert to use when generating secrets for the {{ es_component }} component
  352. slurp:
  353. src: "{{ generated_certs_dir }}/{{ item.file }}"
  354. register: key_pairs
  355. with_items:
  356. - { name: "ca_file", file: "ca.crt" }
  357. - { name: "es_key", file: "system.logging.es.key" }
  358. - { name: "es_cert", file: "system.logging.es.crt" }
  359. when: openshift_logging_es_allow_external | bool
  360. - set_fact:
  361. es_key: "{{ lookup('file', openshift_logging_es_key) | b64encode }}"
  362. when:
  363. - openshift_logging_es_key | trim | length > 0
  364. - openshift_logging_es_allow_external | bool
  365. changed_when: false
  366. - set_fact:
  367. es_cert: "{{ lookup('file', openshift_logging_es_cert) | b64encode }}"
  368. when:
  369. - openshift_logging_es_cert | trim | length > 0
  370. - openshift_logging_es_allow_external | bool
  371. changed_when: false
  372. - set_fact:
  373. es_ca: "{{ lookup('file', openshift_logging_es_ca_ext) | b64encode }}"
  374. when:
  375. - openshift_logging_es_ca_ext | trim | length > 0
  376. - openshift_logging_es_allow_external | bool
  377. changed_when: false
  378. - set_fact:
  379. es_ca: "{{ key_pairs | entry_from_named_pair('ca_file') }}"
  380. when:
  381. - es_ca is not defined
  382. - openshift_logging_es_allow_external | bool
  383. changed_when: false
  384. - name: Generating Elasticsearch {{ es_component }} route template
  385. template:
  386. src: route_reencrypt.j2
  387. dest: "{{mktemp.stdout}}/templates/logging-{{ es_component }}-route.yaml"
  388. vars:
  389. obj_name: "logging-{{ es_component }}"
  390. route_host: "{{ openshift_logging_es_hostname }}"
  391. service_name: "logging-{{ es_component }}"
  392. tls_key: "{{ es_key | default('') | b64decode }}"
  393. tls_cert: "{{ es_cert | default('') | b64decode }}"
  394. tls_ca_cert: "{{ es_ca | b64decode }}"
  395. tls_dest_ca_cert: "{{ key_pairs | entry_from_named_pair('ca_file') | b64decode }}"
  396. edge_term_policy: "{{ openshift_logging_es_edge_term_policy | default('') }}"
  397. labels:
  398. component: support
  399. logging-infra: support
  400. provider: openshift
  401. changed_when: no
  402. when: openshift_logging_es_allow_external | bool
  403. # This currently has an issue if the host name changes
  404. - name: Setting Elasticsearch {{ es_component }} route
  405. oc_obj:
  406. state: present
  407. name: "logging-{{ es_component }}"
  408. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  409. kind: route
  410. files:
  411. - "{{ tempdir }}/templates/logging-{{ es_component }}-route.yaml"
  412. when: openshift_logging_es_allow_external | bool
  413. ## Placeholder for migration when necessary ##
  414. - name: Delete temp directory
  415. file:
  416. name: "{{ tempdir }}"
  417. state: absent
  418. changed_when: False