main.yaml 16 KB

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