· 4 years ago · Oct 16, 2020, 03:30 PM
1# inventories/group_vars/all/vars.yml
2[...]
3web_root: /var/www/html
4do_droplets:
5 - short_name: <short name here>
6 long_name: <long name here>
7 domain: <domain name here>
8 d_index: DirectoryIndex default.htm index.php index.html
9 doc_root: "{{ web_root }}/public"
10 framework: laravel
11 - short_name: <another short name here>
12 [...]
13[...]
14
15# playbooks/provision.yml
16---
17- name: Provision the droplets
18 hosts: localhost
19 tags:
20 - droplet
21 - laravel
22 connection: local
23 gather_facts: false
24
25 tasks:
26 - name: Ensure droplets exist
27 digital_ocean_droplet:
28 oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
29 state: present
30 name: "{{ item.short_name | mandatory }}-01"
31 backups: yes
32 private_networking: yes
33 size: s-1vcpu-1gb
34 image: lamp-20-04
35 region: nyc3
36 ipv6: yes
37 ssh_keys: <hidden>
38 unique_name: yes
39 wait: yes
40 wait_timeout: 300
41 tags:
42 - "{{ item.short_name | mandatory }}"
43 loop: "{{ do_droplets }}"
44 register: droplet
45
46 - name: Add new hosts to inventory
47 add_host:
48 name: "{{ item.data.droplet.name | mandatory }}"
49 ansible_host: "{{ item.data.ip_address | mandatory }}"
50 groups:
51 - do
52 - do_{{ item.item.short_name | mandatory }}
53 - "{{ item.item.framework | default('static_html') }}"
54 short_name: "{{ item.item.short_name | mandatory }}"
55 long_name: "{{ item.item.long_name | default('') }}"
56 domain: "{{ item.item.domain | mandatory }}"
57 d_index: "{{ item.item.d_index | default('') }}"
58 doc_root: "{{ item.item.doc_root | default('/var/www/html') }}"
59 when: item.data is defined
60 changed_when: false
61 loop: "{{ droplet.results }}"
62 loop_control:
63 label: "{{ item.data.droplet.name }}"
64