main.yml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. - name: Set Facts
  16. set_fact:
  17. ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  18. - name: Fetch the execution environment
  19. command: hostname
  20. register: host_name
  21. changed_when: false
  22. - name: Install sshpass
  23. package:
  24. name: sshpass
  25. state: present
  26. when: awx_search_key not in host_name.stdout
  27. - name: Check if provisioned host file exists
  28. stat:
  29. path: "{{ provisioned_hosts_file }}"
  30. register: provisioned_file
  31. - name: Include variable file base_vars.yml
  32. include_vars: "{{ base_vars_file }}"
  33. - name: Update inventory
  34. block:
  35. - name: Check if {{ login_vars_file }} file is encrypted
  36. command: cat {{ login_vars_file }}
  37. changed_when: false
  38. no_log: true
  39. register: config_content
  40. run_once: true
  41. - name: Decrpyt {{ login_vars_file }}
  42. command: >-
  43. ansible-vault decrypt {{ login_vars_file }}
  44. --vault-password-file {{ login_vault_file }}
  45. when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  46. changed_when: false
  47. run_once: true
  48. - name: Include variable file {{ login_vars_file }}
  49. include_vars: "{{ login_vars_file }}"
  50. no_log: true
  51. run_once: true
  52. - name: Save input variables from file
  53. set_fact:
  54. mapping_file: false
  55. - name: Check the status for mapping file
  56. set_fact:
  57. mapping_file: true
  58. when: host_mapping_file_path
  59. - name: Encrypt {{ login_vars_file }}
  60. command: >-
  61. ansible-vault encrypt {{ login_vars_file }}
  62. --vault-password-file {{ login_vault_file }}
  63. changed_when: false
  64. when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  65. run_once: true
  66. - name: Add inventory playbook
  67. block:
  68. - name: add hosts with description to inventory file
  69. command: >-
  70. ansible-playbook -i {{ provisioned_hosts_file }}
  71. {{ role_path }}/files/create_inventory.yml
  72. --extra-vars "host_username={{ host_username }} host_password={{ provision_password }} mapping_file={{ mapping_file | bool }}"
  73. no_log: True
  74. register: register_error
  75. rescue:
  76. - name: Fail if host addition was not successful
  77. fail:
  78. msg: "{{ register_error.stderr + register_error.stdout | regex_replace(host_username) | regex_replace(provision_password) }}"
  79. when: provisioned_file.stat.exists