main.yaml 17 KB

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