-
Notifications
You must be signed in to change notification settings - Fork 7
/
playbook.yml
169 lines (143 loc) · 4.58 KB
/
playbook.yml
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
---
############################
# terraform playbook #
############################
- hosts: localhost
connection: local
gather_facts: no
vars:
tasks:
- name: create teraform.tfvars
include_role:
name: terra-provision
- name: init the terraform if .terraform is not there
shell: terraform init
args:
chdir: "{{ playbook_dir }}/terraform/"
creates: "{{ playbook_dir }}/terraform/.terraform/"
- name: run the terraform script
terraform:
project_path: "{{ playbook_dir }}/terraform/"
state: "{{ aws_instance_state }}"
variables:
aws_region: "{{ aws_region }}"
aws_access_key_id: "{{ aws_access_key_id }}"
aws_secret_access_key: "{{ aws_secret_access_key }}"
aws_ami_id: "{{ aws_ami_id }}"
public_key: "{{ public_key }}"
register: terra_result
- name: show terra_result
debug:
var: terra_result
- name: set vm_ip / name
set_fact:
vm_ip_web: "{{ terra_result.outputs.public_ip_web.value }}"
vm_ip_db: "{{ terra_result.outputs.public_ip_db.value }}"
when:
- terra_result.outputs.state_web is defined
- terra_result.outputs.state_db is defined
- name: create the dynamic inventory
block:
- name: remove old dynamic group_vars file
file:
path: "{{ item }}"
state: absent
with_items:
- group_vars/dynamic_web.yml
- group_vars/dynamic_db.yml
- name: create new centos group_vars file
file:
path: "{{ item }}"
state: touch
with_items:
- group_vars/dynamic_web.yml
- group_vars/dynamic_db.yml
- name: create the inventory directory
file:
path: inventory/
state: directory
- name: remove old dynamic host file
file:
path: inventory/hosts
state: absent
- name: create new dynamic host file
file:
path: inventory/hosts
state: touch
- name: add retrieved IP to file
blockinfile:
path: group_vars/dynamic_web.yml
marker: ""
block: |
---
ansible_host: {{ vm_ip_web }}
ansible_user: {{ remote_user[hypervisor] }}
become_user: {{ remote_user[hypervisor] }}
remote_user: {{ remote_user[hypervisor] }}
become: true
...
- name: add retrieved IP to file
blockinfile:
path: group_vars/dynamic_db.yml
marker: ""
block: |
---
ansible_host: {{ vm_ip_db }}
ansible_user: {{ remote_user[hypervisor] }}
become_user: {{ remote_user[hypervisor] }}
remote_user: {{ remote_user[hypervisor] }}
become: true
...
- name: add retrieved IP to file
blockinfile:
path: "inventory/hosts"
marker: ""
block: |
[dynamic_web]
{{ vm_ip_web }}
[dynamic_db]
{{ vm_ip_db }}
- name: Add host
add_host:
hostname: "{{ vm_ip_web }}"
groupname: dynamic_web
remote_user: "{{ remote_user[hypervisor] }}"
- name: Add host
add_host:
hostname: "{{ vm_ip_db }}"
groupname: dynamic_db
remote_user: "{{ remote_user[hypervisor] }}"
when:
- terra_result.outputs.state_web is defined
- terra_result.outputs.state_db is defined
- name: Collect facts again
setup:
################################
# pause #
################################
- name: Wait 300 seconds for port 22 to become open and contains the string "OpenSSH"
wait_for:
port: 22
host: '{{ vm_ip_web }}'
search_regex: OpenSSH
delay: 10
vars:
ansible_connection: local
when: vm_ip_web is defined
###############################################
# playbook for configuration of the webserver #
###############################################
- hosts: dynamic_web
tasks:
- name: create a website
include_role:
name: webserver
###############################################
# playbook for configuration of the dbserver #
###############################################
- hosts: dynamic_db
tasks:
- name: create a database
include_role:
name: databaseserver
...