add_host.yml 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. - name: Initialise host description
  16. set_fact:
  17. host_description: "Description Unavailable"
  18. - name: Fetch description
  19. set_fact:
  20. host_description: "Service Tag: {{ service_tag }}, Operating System: {{ ansible_distribution }}"
  21. failed_when: false
  22. when: hostname_check.stdout is defined
  23. - block:
  24. - name: Fetch the hosts in awx node inventory
  25. command: >-
  26. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  27. --conf.insecure hosts list --inventory node_inventory
  28. changed_when: false
  29. delegate_to: localhost
  30. no_log: true
  31. run_once: true
  32. register: fetch_hosts
  33. rescue:
  34. - name: Failed to fetch hosts in AWX
  35. fail:
  36. msg: "{{ fetch_hosts.stderr }}"
  37. - block:
  38. - name: Add the host to awx node inventory if not present
  39. command: >-
  40. awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
  41. --conf.insecure hosts create --name {{ inventory_hostname }} --description "{{ host_description }}" --inventory node_inventory
  42. changed_when: true
  43. register: add_host_awx
  44. delegate_to: localhost
  45. no_log: true
  46. when:
  47. - hostname_check.stdout is defined
  48. - fetch_hosts.stdout is defined
  49. - inventory_hostname not in fetch_hosts.stdout
  50. rescue:
  51. - name: Failed to add host to AWX
  52. fail:
  53. msg: "{{ add_host_awx.stderr }}"
  54. when: add_host_awx is defined
  55. - name: Host added msg
  56. debug:
  57. msg: "{{ hostvars['localhost']['host_added_msg'] + inventory_hostname }}"
  58. when:
  59. - host_description != "Description Unavailable"
  60. - add_host_awx is defined
  61. - add_host_awx is not failed