diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..de6b268 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,191 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + Copyright 2020 Kamal Nasser + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md index e69de29..0a7d45e 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,177 @@ +# 📗 do-ansible-inventory + +do-ansible-inventory is a tool that generates an [Ansible inventory](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html) file with your DigitalOcean Droplets. It is an alternative to [dynamic inventories](https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html#intro-dynamic-inventory) as you can run do-ansible-inventory once and receive a static inventory file that you can use anywhere, copy, or modify. + +## Installation + +1. Download the latest release from [the releases page](https://github.com/do-community/do-ansible-inventory/releases). +2. Extract the downloaded archive and place the binary `do-ansible-inventory` wherever you like. Preferably to any directory in your `$PATH` such as `~/bin` if it exists or `/usr/local/bin` so you can easily access it. + +## Usage + +To use do-ansible-inventory, run: + +``` +do-ansible-inventory +``` + +By default, without setting any options, do-ansible-inventory will attempt to look up the currently configured [doctl](https://github.com/digitalocean/doctl) access token and generate an ansible inventory with: + +* All of your Droplets as Ansible hosts +* Each DigitalOcean region as an Ansible group containing any Droplets in that region +* Each Droplet tag as a group containing any Droplets with that tag + +If you do not have `doctl` installed or would like to explicitly specify the access token, run `do-ansible-inventory --access-token ACCESS_TOKEN` or use the `DIGITALOCEAN_ACCESS_TOKEN` environment variable. + +The resulting inventory will be printed to the console. You can save it to a file instead by passing `--out inventory` or redirecting the output like so: `do-ansible-inventory > inventory`. + +### Supported Options + +* `-t TOKEN`, `--access-token TOKEN` - DigitalOcean API Token - if unset, do-ansible-inventory attempts to use doctl's stored token of its current default context. Alternatively, use the environment variable `DIGITALOCEAN_ACCESS_TOKEN` +* `--ssh-user USER` - sets the `ansible_user` property on the hosts (Droplets) +* `--ssh-port PORT` - sets the `ansible_port` property on the hosts (Droplets) +* `--tag TAG` - limits the inventory to only Droplets with the specified tag +* `--ignore HOSTNAME` - pass a Droplet's hostname to exclude it from the inventory. **This option can be used multiple times** +* `--group-by-region` - create groups for each DigitalOcean region. Default behavior. + * `--no-group-by-region` - do not create groups for each DigitalOcean region. +* `--group-by-tag` - create groups for each Droplet tag. Default behavior. + * `--no-group-by-tag` - do not create groups for each Droplet tag. +* `--group-by-project` - create groups for each Droplet projects. Default behavior. + * `--no-group-by-project` - do not create groups for each Droplet project. +* `--private-ips` - use private Droplet IPs instead of public IPs +* `--out FILE` - write the ansible inventory to this file - if unset, print to stdout +* `--timeout=2m` - timeout for total runtime of the command, defaults to `2m` + +## Example + +Running: + +``` +$ do-ansible-inventory --out ./inventory +``` + +will output: + +``` + • no access token provided, attempting to look up doctl's access token + • using doctl access token context=default + • processing droplet=speedtest-ams2.digitalocean.com + • processing droplet=speedtest-nyc2.digitalocean.com + • processing droplet=speedtest-sfo1.digitalocean.com + • processing droplet=speedtest-ams3.digitalocean.com + • processing droplet=speedtest-blr1.digitalocean.com + • processing droplet=speedtest-fra1.digitalocean.com + • processing droplet=speedtest-lon1.digitalocean.com + • processing droplet=speedtest-nyc1.digitalocean.com + • processing droplet=speedtest-nyc3.digitalocean.com + • processing droplet=speedtest-sfo2.digitalocean.com + • processing droplet=speedtest-sgp1.digitalocean.com + • processing droplet=speedtest-tor1.digitalocean.com + • building region group region=ams1 + • building region group region=ams2 + • building region group region=ams3 + • building region group region=blr1 + • building region group region=fra1 + • building region group region=lon1 + • building region group region=nyc1 + • building region group region=nyc2 + • building region group region=nyc3 + • building region group region=sfo1 + • building region group region=sfo2 + • building region group region=sfo3 + • building region group region=sgp1 + • building region group region=tor1 + • building tag group tag=speedtests + • done! +``` + +with the resulting file `inventory` containing: + +``` +speedtest-ams2.digitalocean.com ansible_host=188.226.175.227 +speedtest-nyc2.digitalocean.com ansible_host=192.241.184.88 +speedtest-sfo1.digitalocean.com ansible_host=107.170.223.15 +speedtest-ams3.digitalocean.com ansible_host=178.62.216.76 +speedtest-blr1.digitalocean.com ansible_host=139.59.80.215 +speedtest-fra1.digitalocean.com ansible_host=46.101.218.147 +speedtest-lon1.digitalocean.com ansible_host=46.101.44.214 +speedtest-nyc1.digitalocean.com ansible_host=165.227.194.167 +speedtest-nyc3.digitalocean.com ansible_host=174.138.51.137 +speedtest-sfo2.digitalocean.com ansible_host=165.227.29.84 +speedtest-sgp1.digitalocean.com ansible_host=159.89.192.182 +speedtest-tor1.digitalocean.com ansible_host=159.203.57.38 + +[ams1] + +[ams2] +speedtest-ams2.digitalocean.com + +[ams3] +speedtest-ams3.digitalocean.com + +[blr1] +speedtest-blr1.digitalocean.com + +[fra1] +speedtest-fra1.digitalocean.com + +[lon1] +speedtest-lon1.digitalocean.com + +[nyc1] +speedtest-nyc1.digitalocean.com + +[nyc2] +speedtest-nyc2.digitalocean.com + +[nyc3] +speedtest-nyc3.digitalocean.com + +[sfo1] +speedtest-sfo1.digitalocean.com + +[sfo2] +speedtest-sfo2.digitalocean.com + +[sfo3] + +[sgp1] +speedtest-sgp1.digitalocean.com + +[tor1] + +[speedtests] +speedtest-ams2.digitalocean.com +speedtest-nyc2.digitalocean.com +speedtest-sfo1.digitalocean.com +speedtest-ams3.digitalocean.com +speedtest-blr1.digitalocean.com +speedtest-fra1.digitalocean.com +speedtest-lon1.digitalocean.com +speedtest-nyc1.digitalocean.com +speedtest-nyc3.digitalocean.com +speedtest-sfo2.digitalocean.com +speedtest-sgp1.digitalocean.com +speedtest-tor1.digitalocean.com +``` + +--- + +``` +usage: do-ansible-inventory [] + +Flags: + -t, --access-token=ACCESS-TOKEN + DigitalOcean API Token - if unset, attempts to use doctl's stored token of its current default context. env var: + DIGITALOCEAN_ACCESS_TOKEN + --ssh-user=SSH-USER default ssh user + --ssh-port=SSH-PORT default ssh port + --tag=TAG filter droplets by tag + --ignore=IGNORE ... ignore a Droplet by name, can be specified multiple times + --group-by-region group hosts by region, defaults to true + --group-by-tag group hosts by their Droplet tags, defaults to true + --group-by-project group hosts by their Projects, defaults to true + --private-ips use private Droplet IPs instead of public IPs + --out=OUT write the ansible inventory to this file - if unset, print to stdout + --timeout=2m timeout for total runtime of the command, defaults to 2m +``` diff --git a/ansible.cfg b/ansible.cfg index 14c8065..84a54f2 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,2 +1,3 @@ [defaults] host_key_checking = False +enable_plugins = community.digitalocean.digitalocean diff --git a/hosts b/hosts new file mode 100644 index 0000000..31252ca --- /dev/null +++ b/hosts @@ -0,0 +1,109 @@ +athena ansible_host=167.71.188.165 +Zeus ansible_host=68.183.50.72 +Hades ansible_host=104.131.0.100 +Atreyu ansible_host=159.65.178.119 +nyc3-mpdf1 ansible_host=138.197.103.140 +nyc3-mpdf2 ansible_host=165.227.113.33 +nyc3-mpdb1 ansible_host=167.99.48.86 +nyc3-mpdb2 ansible_host=104.248.7.84 + +[ams1] + +[ams2] + +[ams3] + +[blr1] + +[fra1] + +[lon1] + +[nyc1] + +[nyc2] + +[nyc3] +athena +Zeus +Hades +Atreyu +nyc3-mpdf1 +nyc3-mpdf2 +nyc3-mpdb1 +nyc3-mpdb2 + +[sfo1] + +[sfo2] + +[sfo3] + +[sgp1] + +[tor1] + +[synapse] +athena + +[prometheus] +athena + +[Git] +Zeus + +[nextcloud] +Zeus + +[mpdb] +nyc3-mpdb1 +nyc3-mpdb2 + +[matrix] +athena + +[salt_minion] +athena +Zeus + +[ansible] +athena +Atreyu +nyc3-mpdf1 +nyc3-mpdf2 +nyc3-mpdb1 +nyc3-mpdb2 + +[mpdf] +nyc3-mpdf1 +nyc3-mpdf2 + +[consul] +athena +Hades + +[www_apache] +Zeus + +[controller] +Atreyu + +[Saltmaster] +Zeus + +[salt_minon] +Hades + +[alertmanager] +athena + +[Thrace_Test] +nyc3-mpdb2 +nyc3-mpdb1 +nyc3-mpdf2 +nyc3-mpdf1 +Atreyu +Hades +Zeus +athena + diff --git a/inventory.yml b/inventory.yml new file mode 100644 index 0000000..50b45bd --- /dev/null +++ b/inventory.yml @@ -0,0 +1,109 @@ +athena ansible_host=167.71.188.165 +Zeus ansible_host=68.183.50.72 +Hades ansible_host=104.131.0.100 +Atreyu ansible_host=159.65.178.119 +nyc3-mpdf1 ansible_host=104.131.72.115 +nyc3-mpdf2 ansible_host=64.225.31.87 +nyc3-mpdb1 ansible_host=167.172.229.71 +nyc3-mpdb2 ansible_host=104.131.77.133 + +[ams1] + +[ams2] + +[ams3] + +[blr1] + +[fra1] + +[lon1] + +[nyc1] + +[nyc2] + +[nyc3] +athena +Zeus +Hades +Atreyu +nyc3-mpdf1 +nyc3-mpdf2 +nyc3-mpdb1 +nyc3-mpdb2 + +[sfo1] + +[sfo2] + +[sfo3] + +[sgp1] + +[tor1] + +[Saltmaster] +Zeus + +[Git] +Zeus + +[nextcloud] +Zeus + +[mpdf] +nyc3-mpdf1 +nyc3-mpdf2 + +[matrix] +athena + +[consul] +athena +Hades + +[salt_minion] +athena +Zeus + +[ansible] +athena +Atreyu +nyc3-mpdf1 +nyc3-mpdf2 +nyc3-mpdb1 +nyc3-mpdb2 + +[mpdb] +nyc3-mpdb1 +nyc3-mpdb2 + +[salt_minon] +Hades + +[controller] +Atreyu + +[synapse] +athena + +[prometheus] +athena + +[alertmanager] +athena + +[www_apache] +Zeus + +[Thrace_Test] +nyc3-mpdb2 +nyc3-mpdb1 +nyc3-mpdf2 +nyc3-mpdf1 +Atreyu +Hades +Zeus +athena + diff --git a/playbooks/create_droplet.yml b/playbooks/create_droplet.yml index 57fd5f5..d50593f 100644 --- a/playbooks/create_droplet.yml +++ b/playbooks/create_droplet.yml @@ -1,5 +1,5 @@ --- -- hosts: all +- hosts: localhost connection: local gather_facts: false vars: @@ -22,6 +22,9 @@ state: present oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}" ssh_keys: [ '94:5e:bc:d4:fe:5a:b0:74:7f:06:fc:4e:3d:f7:3e:ea' ] # <---- put your numeric ssh key in here + tags: + - 'ansible' + - 'mpdf' register: created_droplets loop: "{{ mpdf | product(number) }}" @@ -36,10 +39,9 @@ state: present oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}" ssh_keys: [ '94:5e:bc:d4:fe:5a:b0:74:7f:06:fc:4e:3d:f7:3e:ea' ] # <---- put your numeric ssh key in here + tags: + - 'ansible' + - 'mpdb' register: created_droplets loop: "{{ mpdb | product(number) }}" - - name: Install mpd on hosts - apt: - name: mpd - state: present - + diff --git a/playbooks/delete_droplet.yml b/playbooks/delete_droplet.yml new file mode 100644 index 0000000..c669745 --- /dev/null +++ b/playbooks/delete_droplet.yml @@ -0,0 +1,41 @@ +--- +- hosts: localhost + connection: local + gather_facts: false + vars: + mpdf: + - 'nyc3-mpdf' + number: + - '1' + - '2' + mpdb: + - 'nyc3-mpdb' + tasks: + - name: delete frontend droplets + digital_ocean_droplet: + unique_name: yes + region: nyc3 + image: debian-11-x64 + wait_timeout: 100 + name: "{{ item.0 }}{{item.1 }}" + size_id: s-1vcpu-1gb + state: absent + oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}" + ssh_keys: [ '94:5e:bc:d4:fe:5a:b0:74:7f:06:fc:4e:3d:f7:3e:ea' ] # <---- put your numeric ssh key in here + register: deleted_droplets + loop: "{{ mpdf | product(number) }}" + + - name: delete backend droplets + digital_ocean_droplet: + unique_name: yes + region: nyc3 + image: debian-11-x64 + wait_timeout: 100 + name: "{{ item.0 }}{{item.1 }}" + size_id: s-1vcpu-1gb + state: absent + oauth_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}" + ssh_keys: [ '94:5e:bc:d4:fe:5a:b0:74:7f:06:fc:4e:3d:f7:3e:ea' ] # <---- put your numeric ssh key in here + register: deleted_droplets + loop: "{{ mpdb | product(number) }}" + diff --git a/playbooks/do_hosts.yaml b/playbooks/do_hosts.yaml new file mode 100644 index 0000000..c9d466b --- /dev/null +++ b/playbooks/do_hosts.yaml @@ -0,0 +1,30 @@ +# Using keyed groups and compose for hostvars +plugin: community.digitalocean.digitalocean +api_token: "{{ lookup('ansible.builtin.env', 'DO_API_TOKEN') }}" +attributes: + - id + - name + - memory + - vcpus + - disk + - size + - image + - networks + - volume_ids + - tags + - region +keyed_groups: + - key: do_region.slug + prefix: 'region' + separator: '_' + - key: do_tags | lower + prefix: '' + separator: '' +compose: + ansible_host: do_networks.v4 | selectattr('type','eq','public') + | map(attribute='ip_address') | first + class: do_size.description | lower + distro: do_image.distribution | lower +filters: + - '"kubernetes" in do_tags' + - 'do_region.slug == "fra1"' diff --git a/playbooks/mpdb.yaml b/playbooks/mpdb.yaml new file mode 100644 index 0000000..4bf2edd --- /dev/null +++ b/playbooks/mpdb.yaml @@ -0,0 +1,8 @@ +- hosts: all + gather_facts: false + vars: + tasks: + - name: Install mpd on hosts + apt: + name: mpd + state: present diff --git a/playbooks/mpdf.yaml b/playbooks/mpdf.yaml new file mode 100644 index 0000000..4bf2edd --- /dev/null +++ b/playbooks/mpdf.yaml @@ -0,0 +1,8 @@ +- hosts: all + gather_facts: false + vars: + tasks: + - name: Install mpd on hosts + apt: + name: mpd + state: present