-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_setup.yml
140 lines (126 loc) · 4.43 KB
/
build_setup.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
---
- hosts: localhost,admin
tasks:
- name: Find all the Vagrant keys
find:
paths: /vagrant/.vagrant/machines/
patterns: "private_key"
recurse: yes
register: find_keys
- name: Match keys to key
set_fact:
machine_keys: |
{
{% for a_key in find_keys.files %}
"{{ a_key.path | regex_replace('^/vagrant/.vagrant/machines/([^/]+)/[^/]+/private_key$', '\1') }}": "{{ a_key.path }}",
{% endfor %}
}
- name: Read Vagrantfile to find details of the machines
set_fact:
vagrantfile: |-
{
{% set ns = namespace(machine = '', ip = '', fqdn = '') %}
{%- for line in lookup('file', 'Vagrantfile').splitlines() %}
{%- if line | regex_search('config.vm.define \"([^\"]+)') | regex_replace('config.vm.define \"', '') != "None" %}
{%- set ns.machine = line | regex_search('config.vm.define \"([^\"]+)') | regex_replace('config.vm.define \"', '') %}
{%- elif line | regex_search('.vm.network \"private_network\", ip:\s+\"([^\"]+)') | regex_replace('.vm.network \"private_network\", ip:\s+\"', '') != "None" %}
{%- set ns.ip = line| regex_search('.vm.network \"private_network\", ip:\s+\"([^\"]+)') | regex_replace('.vm.network \"private_network\", ip:\s+\"', '') %}
{%- elif line | regex_search('.vm.hostname\s*=\s*\"([^\"]+)') | regex_replace('.vm.hostname\s*=\s*\"', '') != "None" %}
{%- set ns.fqdn = line | regex_search('.vm.hostname\s*=\s*\"([^\"]+)') | regex_replace('.vm.hostname\s*=\s*\"', '') %}
{%- endif %}
{%- if ns.machine | length > 0 and ns.ip | length > 0 and ns.fqdn | length > 0 %}
"{{ ns.machine }}": {
"ip": "{{ ns.ip }}",
"fqdn": "{{ ns.fqdn }}",
"src_key": "{{ machine_keys[ns.machine] | default('') }}",
"private_key": "~/.ssh/{{ ns.machine }}"
},
{%- set ns.machine = '' %}{%- set ns.ip = '' %}{%- set ns.fqdn = '' %}
{%- endif %}
{%- endfor %}
}
- name: Create .ssh directory
file:
path: "{{ item }}"
mode: "0700"
owner: root
group: root
state: directory
loop:
- /root/.ssh/
- /root/.ssh/config.d/
- name: Create .ssh/config
copy:
dest: "/root/.ssh/config"
content: |
Include config.d/*
Host *
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
owner: root
group: root
mode: "0600"
- name: Import Vagrant Keys into Root Keys
copy:
remote_src: yes
src: "{{ item.value.src_key }}"
dest: "/root/.ssh/{{ item.key }}"
mode: "0600"
owner: root
group: root
loop: "{{ vagrantfile | dict2items }}"
loop_control:
label: "{{ item.key }}"
- name: Create SSH Config Values
copy:
dest: "/root/.ssh/config.d/{{ item.key }}"
content: |
Host {{ item.value.fqdn }} {{ item.key }} {{ item.value.ip }}
User vagrant
Hostname {{ item.value.ip }}
IdentityFile {{ item.value.private_key }}
owner: root
group: root
mode: "0600"
loop: "{{ vagrantfile | dict2items }}"
loop_control:
label: "{{ item.key }}"
- name: Create /etc/ansible/hosts
copy:
dest: "/etc/ansible/hosts"
content: |
[all]
{% for item in vagrantfile | dict2items %}
{{ item.key }}
{% endfor %}
[dns-group]
{% for item in vagrantfile | dict2items %}
{% if item.key == 'admin' %}
{{ item.key }} ansible_host={{ item.value.ip }}
{% endif %}
{% endfor %}
[web-group]
{% for item in vagrantfile | dict2items %}
{% if item.key == 'web-01' or item.key == 'snm' %}
{{ item.key }} ansible_host={{ item.value.ip }}
{% endif %}
{% endfor %}
[mail-group]
{% for item in vagrantfile | dict2items %}
{% if item.key == 'mail-in-01' %}
{{ item.key }} ansible_host={{ item.value.ip }}
{% endif %}
{% endfor %}
[mailman-group]
{% for item in vagrantfile | dict2items %}
{% if item.key == 'mailman' %}
{{ item.key }} ansible_host={{ item.value.ip }}
{% endif %}
{% endfor %}
owner: root
group: root
mode: "0644"
- name: Install python3-pip
apt:
name: python3-pip
state: present