awx_configuration.yml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright 2020 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. # Playbook to configure AWX
  16. - name: Configure AWX
  17. hosts: localhost
  18. connection: local
  19. gather_facts: no
  20. tasks:
  21. - name: Include vars file
  22. include_vars: ../vars/main.yml
  23. # Get Current AWX configuration
  24. - name: Get organization list
  25. command: >-
  26. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  27. organizations list -f human
  28. register: organizations_list
  29. - name: Get project list
  30. command: >-
  31. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  32. projects list -f human
  33. register: projects_list
  34. - name: Get inventory list
  35. command: >-
  36. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  37. inventory list -f human
  38. register: inventory_list
  39. - name: Get template list
  40. command: >-
  41. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  42. job_templates list -f human
  43. register: job_templates_list
  44. - name: If omnia-inventory exists, fetch group names in the inventory
  45. command: >-
  46. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  47. groups list --inventory "{{ omnia_inventory_name }}" -f human
  48. register: groups_list
  49. when: omnia_inventory_name in inventory_list.stdout
  50. - name: Get schedules list
  51. command: >-
  52. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  53. schedules list -f human
  54. register: schedules_list
  55. # Delete Default Configurations
  56. - name: Delete default organization
  57. command: >-
  58. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  59. organizations delete "{{ default_org }}"
  60. when: default_org in organizations_list.stdout
  61. - name: Delete default job template
  62. command: >-
  63. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  64. job_templates delete "{{ default_template }}"
  65. when: default_template in job_templates_list.stdout
  66. - name: Delete default project
  67. command: >-
  68. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  69. projects delete "{{ default_projects }}"
  70. when: default_projects in projects_list.stdout
  71. # Create required configuration if not present
  72. - name: Create organisation
  73. command: >-
  74. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  75. organizations create --name "{{ organization_name }}"
  76. when: organization_name not in organizations_list.stdout
  77. - name: Create new project
  78. command: >-
  79. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  80. projects create --name "{{ project_name }}" --organization "{{ organization_name }}"
  81. --local_path "{{ dir_name }}"
  82. when: project_name not in projects_list.stdout
  83. - name: Create new omnia inventory
  84. command: >-
  85. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  86. inventory create --name "{{ omnia_inventory_name }}" --organization "{{ organization_name }}"
  87. when: omnia_inventory_name not in inventory_list.stdout
  88. - name: Create groups in omnia inventory
  89. command: >-
  90. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  91. groups create --name "{{ item }}" --inventory "{{ omnia_inventory_name }}"
  92. when: omnia_inventory_name not in inventory_list.stdout or item not in groups_list.stdout
  93. loop: "{{ group_names }}"
  94. - name: Create template to deploy omnia
  95. command: >-
  96. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  97. job_templates create
  98. --name "{{ omnia_template_name }}"
  99. --job_type run
  100. --inventory "{{ omnia_inventory_name }}"
  101. --project "{{ project_name }}"
  102. --playbook "{{ omnia_playbook }}"
  103. --verbosity "{{ playbooks_verbosity }}"
  104. --ask_skip_tags_on_launch true
  105. when: omnia_template_name not in job_templates_list.stdout
  106. - name: Create template to fetch dynamic inventory
  107. command: >-
  108. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  109. job_templates create
  110. --name "{{ inventory_template_name }}"
  111. --job_type run
  112. --inventory "{{ omnia_inventory_name }}"
  113. --project "{{ project_name }}"
  114. --playbook "{{ inventory_playbook }}"
  115. --verbosity "{{ playbooks_verbosity }}"
  116. --use_fact_cache true
  117. when: inventory_template_name not in job_templates_list.stdout
  118. - name: Schedule dynamic inventory template
  119. block:
  120. - name: Get unified job template list
  121. command: >-
  122. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  123. unified_job_templates list --name "{{ inventory_template_name }}" -f human
  124. register: unified_job_template_list
  125. - name: Get job ID
  126. set_fact:
  127. job_id: "{{ unified_job_template_list.stdout | regex_search('[0-9]+') }}"
  128. - name: Schedule dynamic inventory job
  129. command: >-
  130. awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
  131. schedules create --name "{{ schedule_name }}"
  132. --unified_job_template="{{ job_id }}" --rrule="{{ schedule_rule }}"
  133. when: schedule_name not in schedules_list.stdout