create_inventory.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.​0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. ---
  15. # This role will not group the devices if user provides invalid credentials
  16. - name: Create inventory in awx
  17. hosts: device_inventory
  18. connection: local
  19. gather_facts: false
  20. tasks:
  21. - name: Include collect_device_info vars
  22. include_vars: "{{ playbook_dir }}/../vars/main.yml"
  23. run_once: true
  24. - name: Include variable file base_vars.yml
  25. include_vars: "{{ base_vars_file }}"
  26. run_once: true
  27. - name: Check if tower_config_file file is encrypted
  28. command: cat "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  29. changed_when: false
  30. no_log: true
  31. register: tower_config_content
  32. run_once: true
  33. - name: Decrypt tower_config_file
  34. command: >-
  35. ansible-vault decrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  36. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  37. changed_when: false
  38. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  39. run_once: true
  40. - name: Change file permissions
  41. file:
  42. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  43. mode: "{{ file_perm }}"
  44. run_once: true
  45. - name: Fetch awx host
  46. command: grep "host:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  47. register: fetch_awx_host
  48. changed_when: false
  49. run_once: true
  50. - name: Fetch awx username
  51. command: grep "username:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  52. register: fetch_awx_username
  53. changed_when: false
  54. run_once: true
  55. no_log: true
  56. - name: Fetch awx password
  57. command: grep "password:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  58. register: fetch_awx_password
  59. changed_when: false
  60. run_once: true
  61. no_log: true
  62. - name: Set awx variables
  63. set_fact:
  64. awx_host: "{{ fetch_awx_host.stdout | regex_replace('host: ','') }}"
  65. awx_username: "{{ fetch_awx_username.stdout | regex_replace('username: ','') }}"
  66. awx_password: "{{ fetch_awx_password.stdout | regex_replace('password: ','') }}"
  67. no_log: true
  68. - name: Encrypt tower_config_file
  69. command: >-
  70. ansible-vault encrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  71. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  72. changed_when: false
  73. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  74. run_once: true
  75. - name: Change file permissions
  76. file:
  77. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  78. mode: "{{ file_perm }}"
  79. run_once: true
  80. - name: Check if {{ login_vars_file }} file is encrypted
  81. command: cat {{ login_vars_file }}
  82. changed_when: false
  83. no_log: true
  84. register: config_content
  85. run_once: true
  86. - name: Decrpyt {{ login_vars_file }}
  87. command: >-
  88. ansible-vault decrypt {{ login_vars_file }}
  89. --vault-password-file {{ login_vault_file }}
  90. when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  91. changed_when: false
  92. run_once: true
  93. - name: Include variable file {{ login_vars_file }}
  94. include_vars: "{{ login_vars_file }}"
  95. no_log: true
  96. run_once: true
  97. - name: Encrypt {{ login_vars_file }}
  98. command: >-
  99. ansible-vault encrypt {{ login_vars_file }}
  100. --vault-password-file {{ login_vault_file }}
  101. changed_when: false
  102. when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  103. run_once: true
  104. - name: Initialize variables
  105. set_fact:
  106. idrac_inventory_status: false
  107. ethernet_inventory_status: false
  108. ib_inventory_status: false
  109. powervault_me4_status: false
  110. - name: idrac_inventory validation tasks
  111. block:
  112. - name: Fetch the hosts in idrac_inventory
  113. command: >-
  114. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  115. --conf.insecure hosts list --inventory idrac_inventory -f human --filter "name"
  116. changed_when: false
  117. no_log: true
  118. run_once: true
  119. register: idrac_hosts
  120. - name: Assert idrac IP
  121. dellemc.openmanage.idrac_system_info:
  122. idrac_ip: "{{ inventory_hostname }}"
  123. idrac_user: "{{ idrac_username }}"
  124. idrac_password: "{{ idrac_password }}"
  125. register: idrac_info
  126. when: inventory_hostname not in idrac_hosts.stdout
  127. - name: Set idrac_inventory_status
  128. set_fact:
  129. idrac_inventory_status: true
  130. when:
  131. - inventory_hostname not in idrac_hosts.stdout
  132. - idrac_search_key in idrac_info.system_info.iDRAC[0].ProductInfo
  133. rescue:
  134. - name: Failed while adding device to idrac_inventory
  135. debug:
  136. msg: "{{ idrac_inventory_fail_msg }}"
  137. when: not idrac_inventory_status
  138. - name: Add host to awx idrac_inventory
  139. block:
  140. - name: Add the host to awx idrac_inventory if not present
  141. command: >-
  142. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  143. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory idrac_inventory
  144. changed_when: true
  145. no_log: true
  146. rescue:
  147. - name: Failed while adding device to idrac_inventory
  148. debug:
  149. msg: "{{ idrac_inventory_fail_msg }}"
  150. when: idrac_inventory_status
  151. - name: ethernet_inventory validation tasks
  152. block:
  153. - name: Fetch the hosts in ethernet inventory
  154. command: >-
  155. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  156. --conf.insecure hosts list --inventory ethernet_inventory -f human --filter "name"
  157. changed_when: false
  158. no_log: true
  159. run_once: true
  160. register: ethernet_switches
  161. - name: Assert ethernet switch
  162. dellos10_command:
  163. provider:
  164. host: "{{ inventory_hostname }}"
  165. username: "{{ ethernet_switch_username }}"
  166. password: "{{ ethernet_switch_password }}"
  167. commands: ['show version']
  168. when: inventory_hostname not in ethernet_switches.stdout
  169. register: dellswitch_info
  170. no_log: true
  171. - name: Set ethernet_inventory_status
  172. set_fact:
  173. ethernet_inventory_status: true
  174. when:
  175. - inventory_hostname not in ethernet_switches.stdout
  176. - dellswitch_info.stdout | regex_search(ethernet_search_key)
  177. rescue:
  178. - name: Failed while adding device to ethernet_inventory
  179. debug:
  180. msg: "{{ ethernet_inventory_fail_msg }}"
  181. when:
  182. - not idrac_inventory_status
  183. - not ethernet_inventory_status
  184. - inventory_hostname not in idrac_hosts.stdout
  185. - name: Add the host to awx ethernet inventory
  186. block:
  187. - name: Add the host to awx ethernet inventory if not present
  188. command: >-
  189. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  190. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory ethernet_inventory
  191. changed_when: true
  192. no_log: true
  193. rescue:
  194. - name: Failed while adding device to ethernet_inventory
  195. debug:
  196. msg: "{{ ethernet_inventory_fail_msg }}"
  197. when: ethernet_inventory_status
  198. - name: ib_inventory validation tasks
  199. block:
  200. - name: Fetch the hosts in infiniband inventory
  201. command: >-
  202. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  203. --conf.insecure hosts list --inventory infiniband_inventory -f human --filter "name"
  204. changed_when: false
  205. no_log: true
  206. run_once: true
  207. register: infiniband_switches
  208. - name: Authenticate infiniband Switch
  209. uri:
  210. url: http://{{ inventory_hostname }}/admin/launch?script=rh&template=login&action=login
  211. method: POST
  212. body_format: form-urlencoded
  213. body:
  214. f_user_id: "{{ ib_username }}"
  215. f_password: "{{ ib_password }}"
  216. enter: Sign in
  217. status_code: "{{ infiniband_status_code }}"
  218. no_log: true
  219. register: login
  220. when: inventory_hostname not in infiniband_switches.stdout
  221. - name: Assert infiniband switch
  222. uri:
  223. url: http://{{ inventory_hostname }}/admin/launch?script=json
  224. method: POST
  225. body_format: json
  226. headers:
  227. Cookie: "{{ login.set_cookie.split(';')[0] }}"
  228. body:
  229. {
  230. "commands":
  231. [
  232. "show version"
  233. ]
  234. }
  235. register: infinibandswitch_info
  236. when:
  237. - inventory_hostname not in infiniband_switches.stdout
  238. - not login.failed
  239. - name: Set ib_inventory_status
  240. set_fact:
  241. ib_inventory_status: true
  242. when:
  243. - inventory_hostname not in infiniband_switches.stdout
  244. - not login.failed
  245. - infinibandswitch_info.json.data['Product name'] == infiniband_search_key
  246. rescue:
  247. - name: Failed while adding device to ib_inventory
  248. debug:
  249. msg: "{{ ib_inventory_fail_msg }}"
  250. when:
  251. - not idrac_inventory_status
  252. - not ethernet_inventory_status
  253. - not ib_inventory_status
  254. - inventory_hostname not in idrac_hosts.stdout
  255. - name: Add the host to awx infiniband_inventory
  256. block:
  257. - name: Add the host to awx infiniband_inventory if not present
  258. command: >-
  259. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  260. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory infiniband_inventory
  261. changed_when: true
  262. no_log: true
  263. rescue:
  264. - name: Failed while adding device to ib_inventory
  265. debug:
  266. msg: "{{ ib_inventory_fail_msg }}"
  267. when: ib_inventory_status
  268. - name: powervault_me4_inventory validation tasks
  269. block:
  270. - name: Fetch the hosts in powervault me4 inventory
  271. command: >-
  272. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  273. --conf.insecure hosts list --inventory powervault_me4_inventory -f human --filter "name"
  274. changed_when: false
  275. no_log: true
  276. run_once: true
  277. register: me4_storage
  278. - name: Get auth string for powervault
  279. shell: echo -n {{ powervault_me4_username }}_{{ powervault_me4_password }} | sha256sum
  280. changed_when: false
  281. register: auth_string
  282. no_log: true
  283. when: inventory_hostname not in me4_storage.stdout
  284. - name: Get session key for powervault
  285. uri:
  286. url: https://{{ inventory_hostname }}/api/login/{{ auth_string.stdout | replace(" -", "") }}
  287. method: GET
  288. headers:
  289. {'datatype': 'json'}
  290. validate_certs: no
  291. register: session_key
  292. when: inventory_hostname not in me4_storage.stdout
  293. - name: Assert me4_powervault
  294. uri:
  295. url: https://{{ inventory_hostname }}/api/show/system
  296. method: GET
  297. body_format: json
  298. validate_certs: no
  299. use_proxy: no
  300. headers:
  301. {'sessionKey': "{{ session_key.json.status[0].response }}", 'datatype':'json'}
  302. register: system_info
  303. when: inventory_hostname not in me4_storage.stdout
  304. - name: Set powervault_me4_status
  305. set_fact:
  306. powervault_me4_status: true
  307. when:
  308. - inventory_hostname not in me4_storage.stdout
  309. - me4_powervault_search_key in system_info.json.system[0]['scsi-product-id']
  310. rescue:
  311. - name: Failed while adding device to powervault_me4_inventory
  312. debug:
  313. msg: "{{ powervault_me4_fail_msg }}"
  314. when:
  315. - not idrac_inventory_status
  316. - not ethernet_inventory_status
  317. - not ib_inventory_status
  318. - not powervault_me4_status
  319. - inventory_hostname not in idrac_hosts.stdout
  320. - name: Add the host to awx powervault_me4_inventory
  321. block:
  322. - name: Add the host to awx powervault_me4_inventory if not present
  323. command: >-
  324. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  325. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory powervault_me4_inventory
  326. changed_when: true
  327. no_log: true
  328. rescue:
  329. - name: Failed while adding device to powervault_me4_inventory
  330. debug:
  331. msg: "{{ powervault_me4_fail_msg }}"
  332. when: powervault_me4_status