main.yaml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 deployment_type
  15. include_vars: "{{ var_file_name }}"
  16. with_first_found:
  17. - "{{ openshift_deployment_type | default(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.common.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. - set_fact:
  146. __es_num_of_shards: "{{ _es_configmap | default({}) | walk('index.number_of_shards', '1') }}"
  147. __es_num_of_replicas: "{{ _es_configmap | default({}) | walk('index.number_of_replicas', '0') }}"
  148. - template:
  149. src: elasticsearch.yml.j2
  150. dest: "{{ tempdir }}/elasticsearch.yml"
  151. vars:
  152. allow_cluster_reader: "{{ openshift_logging_elasticsearch_ops_allow_cluster_reader | lower | default('false') }}"
  153. es_number_of_shards: "{{ openshift_logging_es_number_of_shards | default(None) or __es_num_of_shards }}"
  154. es_number_of_replicas: "{{ openshift_logging_es_number_of_replicas | default(None) or __es_num_of_replicas }}"
  155. es_kibana_index_mode: "{{ openshift_logging_elasticsearch_kibana_index_mode | default('unique') }}"
  156. when: es_config_contents is undefined
  157. changed_when: no
  158. - copy:
  159. content: "{{ es_logging_contents }}"
  160. dest: "{{ tempdir }}/elasticsearch-logging.yml"
  161. when: es_logging_contents is defined
  162. changed_when: no
  163. - copy:
  164. content: "{{ es_config_contents }}"
  165. dest: "{{ tempdir }}/elasticsearch.yml"
  166. when: es_config_contents is defined
  167. changed_when: no
  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. # secret
  177. - name: Set ES secret
  178. oc_secret:
  179. state: present
  180. name: "logging-elasticsearch"
  181. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  182. files:
  183. - name: key
  184. path: "{{ generated_certs_dir }}/logging-es.jks"
  185. - name: truststore
  186. path: "{{ generated_certs_dir }}/truststore.jks"
  187. - name: searchguard.key
  188. path: "{{ generated_certs_dir }}/elasticsearch.jks"
  189. - name: searchguard.truststore
  190. path: "{{ generated_certs_dir }}/truststore.jks"
  191. - name: admin-key
  192. path: "{{ generated_certs_dir }}/system.admin.key"
  193. - name: admin-cert
  194. path: "{{ generated_certs_dir }}/system.admin.crt"
  195. - name: admin-ca
  196. path: "{{ generated_certs_dir }}/ca.crt"
  197. - name: admin.jks
  198. path: "{{ generated_certs_dir }}/system.admin.jks"
  199. # services
  200. - name: Set logging-{{ es_component }}-cluster service
  201. oc_service:
  202. state: present
  203. name: "logging-{{ es_component }}-cluster"
  204. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  205. selector:
  206. component: "{{ es_component }}"
  207. provider: openshift
  208. labels:
  209. logging-infra: 'support'
  210. ports:
  211. - port: 9300
  212. - name: Set logging-{{ es_component }} service
  213. oc_service:
  214. state: present
  215. name: "logging-{{ es_component }}"
  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: 9200
  224. targetPort: "restapi"
  225. - name: Set logging-{{ es_component}}-prometheus service
  226. oc_service:
  227. state: present
  228. name: "logging-{{es_component}}-prometheus"
  229. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  230. labels:
  231. logging-infra: 'support'
  232. ports:
  233. - name: proxy
  234. port: 443
  235. targetPort: 4443
  236. selector:
  237. component: "{{ es_component }}"
  238. provider: openshift
  239. - oc_edit:
  240. kind: service
  241. name: "logging-{{es_component}}-prometheus"
  242. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  243. separator: '#'
  244. content:
  245. metadata#annotations#service.alpha.openshift.io/serving-cert-secret-name: "prometheus-tls"
  246. metadata#annotations#prometheus.io/scrape: "true"
  247. metadata#annotations#prometheus.io/scheme: "https"
  248. metadata#annotations#prometheus.io/path: "_prometheus/metrics"
  249. - name: Check to see if PVC already exists
  250. oc_obj:
  251. state: list
  252. kind: pvc
  253. name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  254. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  255. register: logging_elasticsearch_pvc
  256. # logging_elasticsearch_pvc.results.results | length > 0 returns a false positive
  257. # so we check for the presence of 'stderr' to determine if the obj exists or not
  258. # the RC for existing and not existing is both 0
  259. - when:
  260. - logging_elasticsearch_pvc.results.stderr is defined
  261. - openshift_logging_elasticsearch_storage_type == "pvc"
  262. block:
  263. # storageclasses are used by default but if static then disable
  264. # storageclasses with the storageClassName set to "" in pvc.j2
  265. - name: Creating ES storage template - static
  266. template:
  267. src: pvc.j2
  268. dest: "{{ tempdir }}/templates/logging-es-pvc.yml"
  269. vars:
  270. obj_name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  271. size: "{{ (openshift_logging_elasticsearch_pvc_size | trim | length == 0) | ternary('10Gi', openshift_logging_elasticsearch_pvc_size) }}"
  272. access_modes: "{{ openshift_logging_elasticsearch_pvc_access_modes | list }}"
  273. pv_selector: "{{ openshift_logging_elasticsearch_pvc_pv_selector }}"
  274. storage_class_name: "{{ openshift_logging_elasticsearch_pvc_storage_class_name | default('', true) }}"
  275. when:
  276. - not openshift_logging_elasticsearch_pvc_dynamic | bool
  277. # Storageclasses are used by default if configured
  278. - name: Creating ES storage template - dynamic
  279. template:
  280. src: pvc.j2
  281. dest: "{{ tempdir }}/templates/logging-es-pvc.yml"
  282. vars:
  283. obj_name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  284. size: "{{ (openshift_logging_elasticsearch_pvc_size | trim | length == 0) | ternary('10Gi', openshift_logging_elasticsearch_pvc_size) }}"
  285. access_modes: "{{ openshift_logging_elasticsearch_pvc_access_modes | list }}"
  286. pv_selector: "{{ openshift_logging_elasticsearch_pvc_pv_selector }}"
  287. when:
  288. - openshift_logging_elasticsearch_pvc_dynamic | bool
  289. - name: Set ES storage
  290. oc_obj:
  291. state: present
  292. kind: pvc
  293. name: "{{ openshift_logging_elasticsearch_pvc_name }}"
  294. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  295. files:
  296. - "{{ tempdir }}/templates/logging-es-pvc.yml"
  297. delete_after: true
  298. - set_fact:
  299. es_deploy_name: "logging-{{ es_component }}-{{ openshift_logging_elasticsearch_deployment_type }}-{{ 8 | oo_random_word('abcdefghijklmnopqrstuvwxyz0123456789') }}"
  300. when: openshift_logging_elasticsearch_deployment_name == ""
  301. - set_fact:
  302. es_deploy_name: "{{ openshift_logging_elasticsearch_deployment_name }}"
  303. when: openshift_logging_elasticsearch_deployment_name != ""
  304. # DC
  305. - name: Set ES dc templates
  306. template:
  307. src: es.j2
  308. dest: "{{ tempdir }}/templates/logging-es-dc.yml"
  309. vars:
  310. es_cluster_name: "{{ es_component }}"
  311. component: "{{ es_component }}"
  312. logging_component: elasticsearch
  313. deploy_name: "{{ es_deploy_name }}"
  314. image: "{{ openshift_logging_elasticsearch_image_prefix }}logging-elasticsearch:{{ openshift_logging_elasticsearch_image_version }}"
  315. proxy_image: "{{ openshift_logging_elasticsearch_proxy_image_prefix }}oauth-proxy:{{ openshift_logging_elasticsearch_proxy_image_version }}"
  316. es_cpu_limit: "{{ openshift_logging_elasticsearch_cpu_limit | default('') }}"
  317. es_cpu_request: "{{ openshift_logging_elasticsearch_cpu_request | min_cpu(openshift_logging_elasticsearch_cpu_limit | default(none)) }}"
  318. es_memory_limit: "{{ openshift_logging_elasticsearch_memory_limit }}"
  319. es_node_selector: "{{ openshift_logging_elasticsearch_nodeselector | default({}) }}"
  320. es_storage_groups: "{{ openshift_logging_elasticsearch_storage_group | default([]) }}"
  321. 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 }}"
  322. deploy_type: "{{ openshift_logging_elasticsearch_deployment_type }}"
  323. es_replicas: 1
  324. - name: Set ES dc
  325. oc_obj:
  326. state: present
  327. name: "{{ es_deploy_name }}"
  328. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  329. kind: dc
  330. files:
  331. - "{{ tempdir }}/templates/logging-es-dc.yml"
  332. delete_after: true
  333. - name: Retrieving the cert to use when generating secrets for the {{ es_component }} component
  334. slurp:
  335. src: "{{ generated_certs_dir }}/{{ item.file }}"
  336. register: key_pairs
  337. with_items:
  338. - { name: "ca_file", file: "ca.crt" }
  339. - { name: "es_key", file: "system.logging.es.key" }
  340. - { name: "es_cert", file: "system.logging.es.crt" }
  341. when: openshift_logging_es_allow_external | bool
  342. - set_fact:
  343. es_key: "{{ lookup('file', openshift_logging_es_key) | b64encode }}"
  344. when:
  345. - openshift_logging_es_key | trim | length > 0
  346. - openshift_logging_es_allow_external | bool
  347. changed_when: false
  348. - set_fact:
  349. es_cert: "{{ lookup('file', openshift_logging_es_cert) | b64encode }}"
  350. when:
  351. - openshift_logging_es_cert | trim | length > 0
  352. - openshift_logging_es_allow_external | bool
  353. changed_when: false
  354. - set_fact:
  355. es_ca: "{{ lookup('file', openshift_logging_es_ca_ext) | b64encode }}"
  356. when:
  357. - openshift_logging_es_ca_ext | trim | length > 0
  358. - openshift_logging_es_allow_external | bool
  359. changed_when: false
  360. - set_fact:
  361. es_ca: "{{ key_pairs | entry_from_named_pair('ca_file') }}"
  362. when:
  363. - es_ca is not defined
  364. - openshift_logging_es_allow_external | bool
  365. changed_when: false
  366. - name: Generating Elasticsearch {{ es_component }} route template
  367. template:
  368. src: route_reencrypt.j2
  369. dest: "{{mktemp.stdout}}/templates/logging-{{ es_component }}-route.yaml"
  370. vars:
  371. obj_name: "logging-{{ es_component }}"
  372. route_host: "{{ openshift_logging_es_hostname }}"
  373. service_name: "logging-{{ es_component }}"
  374. tls_key: "{{ es_key | default('') | b64decode }}"
  375. tls_cert: "{{ es_cert | default('') | b64decode }}"
  376. tls_ca_cert: "{{ es_ca | b64decode }}"
  377. tls_dest_ca_cert: "{{ key_pairs | entry_from_named_pair('ca_file') | b64decode }}"
  378. edge_term_policy: "{{ openshift_logging_es_edge_term_policy | default('') }}"
  379. labels:
  380. component: support
  381. logging-infra: support
  382. provider: openshift
  383. changed_when: no
  384. when: openshift_logging_es_allow_external | bool
  385. # This currently has an issue if the host name changes
  386. - name: Setting Elasticsearch {{ es_component }} route
  387. oc_obj:
  388. state: present
  389. name: "logging-{{ es_component }}"
  390. namespace: "{{ openshift_logging_elasticsearch_namespace }}"
  391. kind: route
  392. files:
  393. - "{{ tempdir }}/templates/logging-{{ es_component }}-route.yaml"
  394. when: openshift_logging_es_allow_external | bool
  395. ## Placeholder for migration when necessary ##
  396. - name: Delete temp directory
  397. file:
  398. name: "{{ tempdir }}"
  399. state: absent
  400. changed_when: False