joelbirchler
2020-02-28 222a14f6e10ed54e0b22b58a45366b443323aa14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
---
- name: Remove repo directory
  file:
    path: /tmp/do417
    state: absent
 
- name: Get ssh key for Github
  copy:
    src: "{{ github_ssh_key }}"
    dest: /tmp/github_ssh_key
    mode: 0400
    owner: "{{ ansible_user }}"
 
- name: Get lab source code
  git:
    clone: yes
    force: yes
    dest: "/tmp/do417"
    repo: "git@github.com:RedHatTraining/do417.git"
    key_file: /tmp/github_ssh_key
    accept_hostkey: true
 
- name: Remove ssh key for Github
  file:
    path: /tmp/github_ssh_key
    state: absent
 
- name: Remove .git directory
  file:
    path: /tmp/do417/.git
    state: absent
 
- name: Determining material directories
  command: find /tmp/do417/classroom/infrastructure/playbooks/files/src -maxdepth 1 -mindepth 1 -type d -printf '%f\n'
  register: repos_name
 
- name: Wait for Tower to be reachable
  wait_for:
    port: 443
    delay: 5
 
- name: Retrieve default organization ID
  uri:
    url:  "https://localhost/api/v2/organizations/?name=Default"
    method: GET
    user: admin
    password: "{{ tower_admin_password }}"
    body: ""
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
  register: organization_id
 
- name: Retrieve default inventory ID
  uri:
    url: "https://localhost/api/v2/inventories/?name=Default+inventory"
    method: GET
    user: admin
    password: "{{ tower_admin_password }}"
    body: ""
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
  register: inventory_id
 
- name: Create GitLab credentials
  uri:
    url:  "https://localhost/api/v2/credentials/"
    method: POST
    user: admin
    password: "{{ tower_admin_password }}"
    body: "{{ lookup('template','gitlab-creds.json.j2') }}"
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
      - 201
      - 400
  register: response
  changed_when: response.status == 201
 
- name: Retrieve credentials ID
  uri:
    url:  "https://localhost/api/v2/credentials/?name=GitLab"
    method: GET
    user: admin
    password: "{{ tower_admin_password }}"
    body: ""
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
  register: gitlab_credentials_id
 
- name: Create DevOps credentials
  uri:
    url:  "https://localhost/api/v2/credentials/"
    method: POST
    user: admin
    password: "{{ tower_admin_password }}"
    body: "{{ lookup('template','devops-creds.json.j2') }}"
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
      - 201
      - 400
  register: response
  changed_when: response.status == 201
 
- name: Retrieve credentials ID
  uri:
    url:  "https://localhost/api/v2/credentials/?name=DevOps"
    method: GET
    user: admin
    password: "{{ tower_admin_password }}"
    body: ""
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
  register: devops_credentials_id
 
- name: Create Tower credentials
  uri:
    url:  "https://localhost/api/v2/credentials/"
    method: POST
    user: admin
    password: "{{ tower_admin_password }}"
    body: "{{ lookup('template','tower-creds.json.j2') }}"
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
      - 201
      - 400
  register: response
  changed_when: response.status == 201
 
- name: Create Git projects
  uri:
    url:  "https://localhost/api/v2/projects/"
    method: POST
    user: admin
    password: "{{ tower_admin_password }}"
    body:
      name: "{{ item }} repository"
      description: "{{ item }} project"
      local_path: ""
      scm_type: "git"
      scm_url: "https://gitlab.{{ dns_domain_name }}/student/{{ item }}.git"
      scm_branch: "master"
      scm_clean: false
      scm_delete_on_update: false
      credential: "{{ gitlab_credentials_id.json.results[0].id }}"
      timeout: 0
      organization: "{{ organization_id.json.results[0].id }}"
      scm_update_on_launch: true
      scm_update_cache_timeout: 0
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
      - 201
      - 400
  register: response
  changed_when: response.status == 201
  with_items: "{{ repos_name.stdout_lines }}"
 
- name: Retrieve projects ID
  uri:
    url:  "https://localhost/api/v2/projects/?name={{ item }}+repository"
    method: GET
    user: admin
    password: "{{ tower_admin_password }}"
    body: ""
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
  with_items: "{{ repos_name.stdout_lines }}"
  register: projects_id
 
- name: Create job templates
  uri:
    url:  "https://localhost/api/v2/job_templates/"
    method: POST
    user: admin
    password: "{{ tower_admin_password }}"
    body:
      name: "Run {{ item.json.results[0].description }}"
      description: Use this job template to run your playbooks
      job_type: run
      inventory: "{{ inventory_id.json.results[0].id }}"
      project:  "{{ item.json.results[0].id }}"
      playbook: dummy.yml
      forks: 0
      verbosity: 0
      force_handlers: false
      timeout: 0
      use_fact_cache: false
      ask_diff_mode_on_launch: false
      ask_variables_on_launch: false
      ask_limit_on_launch: false
      ask_tags_on_launch: false
      ask_skip_tags_on_launch: false
      ask_job_type_on_launch: false
      ask_verbosity_on_launch: false
      ask_inventory_on_launch: false
      ask_credential_on_launch: false
      survey_enabled: true
      become_enabled: false
      diff_mode: false
      allow_simultaneous: false
      custom_virtualenv:
      job_slice_count: 1
      credential: "{{ devops_credentials_id.json.results[0].id }}"
      vault_credential:
    body_format: json
    validate_certs: False
    force_basic_auth: yes
    status_code:
      - 200
      - 201
      - 400
  register: response
  changed_when: response.status == 201
  with_items: "{{ projects_id.results }}"