main.yaml 17 KB

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