create_inventory.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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: 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. run_once: true
  165. register: ethernet_switches
  166. - name: Assert ethernet switch
  167. dellos10_command:
  168. provider:
  169. host: "{{ inventory_hostname }}"
  170. username: "{{ ethernet_switch_username }}"
  171. password: "{{ ethernet_switch_password }}"
  172. commands: ['show version']
  173. when: inventory_hostname not in ethernet_switches.stdout
  174. register: dellswitch_info
  175. no_log: true
  176. - name: Set ethernet_inventory_status
  177. set_fact:
  178. ethernet_inventory_status: true
  179. when:
  180. - inventory_hostname not in ethernet_switches.stdout
  181. - dellswitch_info.stdout | regex_search(ethernet_search_key)
  182. rescue:
  183. - name: Failed while adding device to ethernet_inventory
  184. debug:
  185. msg: "{{ ethernet_inventory_fail_msg }}"
  186. when:
  187. - not idrac_inventory_status
  188. - not ethernet_inventory_status
  189. - inventory_hostname not in idrac_hosts.stdout
  190. - name: Add the host to awx ethernet inventory
  191. block:
  192. - name: Add the host to awx ethernet inventory if not present
  193. command: >-
  194. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  195. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory ethernet_inventory
  196. changed_when: true
  197. no_log: true
  198. rescue:
  199. - name: Failed while adding device to ethernet_inventory
  200. debug:
  201. msg: "{{ ethernet_inventory_fail_msg }}"
  202. when: ethernet_inventory_status
  203. - name: ib_inventory validation tasks
  204. block:
  205. - name: Fetch the hosts in infiniband inventory
  206. command: >-
  207. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  208. --conf.insecure hosts list --inventory infiniband_inventory -f human --filter "name"
  209. changed_when: false
  210. no_log: true
  211. run_once: true
  212. register: infiniband_switches
  213. - name: Authenticate infiniband Switch
  214. uri:
  215. url: http://{{ inventory_hostname }}/admin/launch?script=rh&template=login&action=login
  216. method: POST
  217. body_format: form-urlencoded
  218. body:
  219. f_user_id: "{{ ib_username }}"
  220. f_password: "{{ ib_password }}"
  221. enter: Sign in
  222. status_code: "{{ infiniband_status_code }}"
  223. no_log: true
  224. register: login
  225. when: inventory_hostname not in infiniband_switches.stdout
  226. - name: Assert infiniband switch
  227. uri:
  228. url: http://{{ inventory_hostname }}/admin/launch?script=json
  229. method: POST
  230. body_format: json
  231. headers:
  232. Cookie: "{{ login.set_cookie.split(';')[0] }}"
  233. body:
  234. {
  235. "commands":
  236. [
  237. "show version"
  238. ]
  239. }
  240. register: infinibandswitch_info
  241. when:
  242. - inventory_hostname not in infiniband_switches.stdout
  243. - not login.failed
  244. - name: Set ib_inventory_status
  245. set_fact:
  246. ib_inventory_status: true
  247. when:
  248. - inventory_hostname not in infiniband_switches.stdout
  249. - not login.failed
  250. - infinibandswitch_info.json.data['Product name'] == infiniband_search_key
  251. rescue:
  252. - name: Failed while adding device to ib_inventory
  253. debug:
  254. msg: "{{ ib_inventory_fail_msg }}"
  255. when:
  256. - not idrac_inventory_status
  257. - not ethernet_inventory_status
  258. - not ib_inventory_status
  259. - inventory_hostname not in idrac_hosts.stdout
  260. - name: Add the host to awx infiniband_inventory
  261. block:
  262. - name: Add the host to awx infiniband_inventory if not present
  263. command: >-
  264. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  265. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory infiniband_inventory
  266. changed_when: true
  267. no_log: true
  268. rescue:
  269. - name: Failed while adding device to ib_inventory
  270. debug:
  271. msg: "{{ ib_inventory_fail_msg }}"
  272. when: ib_inventory_status
  273. - name: powervault_me4_inventory validation tasks
  274. block:
  275. - name: Fetch the hosts in powervault me4 inventory
  276. command: >-
  277. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  278. --conf.insecure hosts list --inventory powervault_me4_inventory -f human --filter "name"
  279. changed_when: false
  280. no_log: true
  281. run_once: true
  282. register: me4_storage
  283. - name: Get auth string for powervault
  284. shell: echo -n {{ powervault_me4_username }}_{{ powervault_me4_password }} | sha256sum
  285. changed_when: false
  286. register: auth_string
  287. no_log: true
  288. when: inventory_hostname not in me4_storage.stdout
  289. - name: Get session key for powervault
  290. uri:
  291. url: https://{{ inventory_hostname }}/api/login/{{ auth_string.stdout | replace(" -", "") }}
  292. method: GET
  293. headers:
  294. {'datatype': 'json'}
  295. validate_certs: no
  296. register: session_key
  297. when: inventory_hostname not in me4_storage.stdout
  298. - name: Assert me4_powervault
  299. uri:
  300. url: https://{{ inventory_hostname }}/api/show/system
  301. method: GET
  302. body_format: json
  303. validate_certs: no
  304. use_proxy: no
  305. headers:
  306. {'sessionKey': "{{ session_key.json.status[0].response }}", 'datatype':'json'}
  307. register: system_info
  308. when: inventory_hostname not in me4_storage.stdout
  309. - name: Set powervault_me4_status
  310. set_fact:
  311. powervault_me4_status: true
  312. when:
  313. - inventory_hostname not in me4_storage.stdout
  314. - me4_powervault_search_key in system_info.json.system[0]['scsi-product-id']
  315. rescue:
  316. - name: Failed while adding device to powervault_me4_inventory
  317. debug:
  318. msg: "{{ powervault_me4_fail_msg }}"
  319. when:
  320. - not idrac_inventory_status
  321. - not ethernet_inventory_status
  322. - not ib_inventory_status
  323. - not powervault_me4_status
  324. - inventory_hostname not in idrac_hosts.stdout
  325. - name: Add the host to awx powervault_me4_inventory
  326. block:
  327. - name: Add the host to awx powervault_me4_inventory if not present
  328. command: >-
  329. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  330. --conf.insecure hosts create --name {{ inventory_hostname }} --inventory powervault_me4_inventory
  331. changed_when: true
  332. no_log: true
  333. rescue:
  334. - name: Failed while adding device to powervault_me4_inventory
  335. debug:
  336. msg: "{{ powervault_me4_fail_msg }}"
  337. when: powervault_me4_status