create_inventory.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # Copyright 2022 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: Install paramiko
  105. command: pip3 install paramiko -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
  106. delegate_to: localhost
  107. run_once: true
  108. changed_when: false
  109. - name: Initialize variables
  110. set_fact:
  111. idrac_inventory_status: false
  112. ethernet_inventory_status: false
  113. ib_inventory_status: false
  114. powervault_me4_status: false
  115. - name: idrac_inventory validation tasks
  116. block:
  117. - name: Fetch the hosts in idrac_inventory
  118. command: >-
  119. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  120. --conf.insecure hosts list --inventory idrac_inventory -f human --filter "name"
  121. changed_when: false
  122. no_log: true
  123. run_once: true
  124. register: idrac_hosts
  125. - name: Assert idrac IP
  126. dellemc.openmanage.idrac_system_info:
  127. idrac_ip: "{{ inventory_hostname }}"
  128. idrac_user: "{{ idrac_username }}"
  129. idrac_password: "{{ idrac_password }}"
  130. register: idrac_info
  131. when: inventory_hostname not in idrac_hosts.stdout
  132. - name: Set idrac_inventory_status
  133. set_fact:
  134. idrac_inventory_status: true
  135. when:
  136. - inventory_hostname not in idrac_hosts.stdout
  137. - idrac_search_key in idrac_info.system_info.iDRAC[0].ProductInfo
  138. rescue:
  139. - name: Failed while adding device to idrac_inventory
  140. debug:
  141. msg: "{{ idrac_inventory_fail_msg }}"
  142. when: not idrac_inventory_status
  143. - name: Add host to awx idrac_inventory
  144. block:
  145. - name: Add the host to awx idrac_inventory if not present
  146. command: >-
  147. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  148. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory idrac_inventory
  149. changed_when: true
  150. no_log: true
  151. rescue:
  152. - name: Failed while adding device to idrac_inventory
  153. debug:
  154. msg: "{{ idrac_inventory_fail_msg }}"
  155. when: idrac_inventory_status
  156. - name: ethernet_inventory validation tasks
  157. block:
  158. - name: Fetch the hosts in ethernet inventory
  159. command: >-
  160. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  161. --conf.insecure hosts list --inventory ethernet_inventory -f human --filter "name"
  162. changed_when: false
  163. no_log: true
  164. register: ethernet_switches
  165. - name: Assert ethernet switch
  166. dellos10_command:
  167. provider:
  168. host: "{{ inventory_hostname }}"
  169. username: "{{ ethernet_switch_username }}"
  170. password: "{{ ethernet_switch_password }}"
  171. commands: ['show version']
  172. when: inventory_hostname not in ethernet_switches.stdout
  173. register: dellswitch_info
  174. no_log: true
  175. - name: Set ethernet_inventory_status
  176. set_fact:
  177. ethernet_inventory_status: true
  178. when:
  179. - inventory_hostname not in ethernet_switches.stdout
  180. - dellswitch_info.stdout | regex_search(ethernet_search_key)
  181. rescue:
  182. - name: Failed while adding device to ethernet_inventory
  183. debug:
  184. msg: "{{ ethernet_inventory_fail_msg }}"
  185. when:
  186. - not idrac_inventory_status
  187. - not ethernet_inventory_status
  188. - inventory_hostname not in idrac_hosts.stdout
  189. - name: Add the host to awx ethernet inventory
  190. block:
  191. - name: Add the host to awx ethernet inventory if not present
  192. command: >-
  193. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  194. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory ethernet_inventory
  195. changed_when: true
  196. no_log: true
  197. rescue:
  198. - name: Failed while adding device to ethernet_inventory
  199. debug:
  200. msg: "{{ ethernet_inventory_fail_msg }}"
  201. when: ethernet_inventory_status
  202. - name: ib_inventory validation tasks
  203. block:
  204. - name: Fetch the hosts in infiniband inventory
  205. command: >-
  206. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  207. --conf.insecure hosts list --inventory infiniband_inventory -f human --filter "name"
  208. changed_when: false
  209. no_log: true
  210. register: infiniband_switches
  211. - name: Authenticate infiniband Switch
  212. uri:
  213. url: http://{{ inventory_hostname }}/admin/launch?script=rh&template=login&action=login
  214. method: POST
  215. body_format: form-urlencoded
  216. body:
  217. f_user_id: "{{ ib_username }}"
  218. f_password: "{{ ib_password }}"
  219. enter: Sign in
  220. status_code: "{{ infiniband_status_code }}"
  221. no_log: true
  222. register: login
  223. when: inventory_hostname not in infiniband_switches.stdout
  224. - name: Assert infiniband switch
  225. uri:
  226. url: http://{{ inventory_hostname }}/admin/launch?script=json
  227. method: POST
  228. body_format: json
  229. headers:
  230. Cookie: "{{ login.set_cookie.split(';')[0] }}"
  231. body:
  232. {
  233. "commands":
  234. [
  235. "show version"
  236. ]
  237. }
  238. register: infinibandswitch_info
  239. when:
  240. - inventory_hostname not in infiniband_switches.stdout
  241. - not login.failed
  242. - name: Set ib_inventory_status
  243. set_fact:
  244. ib_inventory_status: true
  245. when:
  246. - inventory_hostname not in infiniband_switches.stdout
  247. - not login.failed
  248. - (infinibandswitch_info.json.results is defined and infinibandswitch_info.json.results[0].data['Product name'] == infiniband_search_key) or (infinibandswitch_info.json.data is defined and infinibandswitch_info.json.data['Product name'] == infiniband_search_key)
  249. rescue:
  250. - name: Failed while adding device to ib_inventory
  251. debug:
  252. msg: "{{ ib_inventory_fail_msg }}"
  253. when:
  254. - not idrac_inventory_status
  255. - not ethernet_inventory_status
  256. - not ib_inventory_status
  257. - inventory_hostname not in idrac_hosts.stdout
  258. - name: Add the host to awx infiniband_inventory
  259. block:
  260. - name: Add the host to awx infiniband_inventory if not present
  261. command: >-
  262. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  263. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory infiniband_inventory
  264. changed_when: true
  265. no_log: true
  266. rescue:
  267. - name: Failed while adding device to ib_inventory
  268. debug:
  269. msg: "{{ ib_inventory_fail_msg }}"
  270. when: ib_inventory_status
  271. - name: powervault_me4_inventory validation tasks
  272. block:
  273. - name: Fetch the hosts in powervault me4 inventory
  274. command: >-
  275. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  276. --conf.insecure hosts list --inventory powervault_me4_inventory -f human --filter "name"
  277. changed_when: false
  278. no_log: true
  279. register: me4_storage
  280. - name: Get auth string for powervault
  281. shell: echo -n {{ powervault_me4_username }}_{{ powervault_me4_password }} | sha256sum
  282. changed_when: false
  283. register: auth_string
  284. no_log: true
  285. when: inventory_hostname not in me4_storage.stdout
  286. - name: Get session key for powervault
  287. uri:
  288. url: https://{{ inventory_hostname }}/api/login/{{ auth_string.stdout | replace(" -", "") }}
  289. method: GET
  290. headers:
  291. {'datatype': 'json'}
  292. validate_certs: no
  293. register: session_key
  294. when: inventory_hostname not in me4_storage.stdout
  295. - name: Assert me4_powervault
  296. uri:
  297. url: https://{{ inventory_hostname }}/api/show/system
  298. method: GET
  299. body_format: json
  300. validate_certs: no
  301. use_proxy: no
  302. headers:
  303. {'sessionKey': "{{ session_key.json.status[0].response }}", 'datatype':'json'}
  304. register: system_info
  305. when: inventory_hostname not in me4_storage.stdout
  306. - name: Set powervault_me4_status
  307. set_fact:
  308. powervault_me4_status: true
  309. when:
  310. - inventory_hostname not in me4_storage.stdout
  311. - me4_powervault_search_key in system_info.json.system[0]['scsi-product-id']
  312. rescue:
  313. - name: Failed while adding device to powervault_me4_inventory
  314. debug:
  315. msg: "{{ powervault_me4_fail_msg }}"
  316. when:
  317. - not idrac_inventory_status
  318. - not ethernet_inventory_status
  319. - not ib_inventory_status
  320. - not powervault_me4_status
  321. - inventory_hostname not in idrac_hosts.stdout
  322. - name: Add the host to awx powervault_me4_inventory
  323. block:
  324. - name: Add the host to awx powervault_me4_inventory if not present
  325. command: >-
  326. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  327. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory powervault_me4_inventory
  328. changed_when: true
  329. no_log: true
  330. rescue:
  331. - name: Failed while adding device to powervault_me4_inventory
  332. debug:
  333. msg: "{{ powervault_me4_fail_msg }}"
  334. when: powervault_me4_status