diff --git a/Readme.md b/Readme.md index 6c600be..34099a7 100644 --- a/Readme.md +++ b/Readme.md @@ -1,46 +1,13 @@ # ansible API -## Objectif - -Utiliser ansible pour créer un container sur le cloud scaleway - -## Documentation - -https://docs.ansible.com/ansible/2.9/modules/scaleway_compute_module.html#scaleway-compute-module - -## Informations utiles - - * Id du projet: 8cf65c8f-64c0-47d3-9f50-fbe249b76aae - * Id de l'image ( Alma Linux 9 ) : 26e4c50e-91d6-41a5-8898-9e40920e819d - * Type commercial: DEV1-S - * Zone: par1 - -clé secrète: **CHANGEME** ```yaml -access_key: SCWP03YRZY6G8WTRHY9Z -scw_api_secret_key: dc743230-9c09-43bc-bcca-7027a6d609e6 -default_organization_id: 6357800e-8b77-47a3-af7a-84ee93f019c2 -scw_api_default_project_id: 8cf65c8f-64c0-47d3-9f50-fbe249b76aae -``` - -## Instructions: - -Écrire un rôle permettant de créer un container sur le cloud scaleway, en utilisant les informations ci-dessus. - -Le rôle doit permettre de spécifier le nom du container que l'on souhaite créer - -Les données sensibles doivent être stockées dans un vault. - -Une fois créé, le container doit être lancé. - -Le rôle doit ensuite générer un fichier contenant: - * le nom du container - * son adresse ip - -Ex: - -```text - - { record_name: mzaldjmaz00, record_value: 51.158.125.92 } - - { record_name: mzaldjmaz01, record_value: 51.158.118.75 } +--- +- hosts: localhost + vars: + scw_api_secret_key: CHANGEME + scw_api_default_project_id: 8cf65c8f-64c0-47d3-9f50-fbe249b76aae + container_name: evivbulgroz + roles: + - sib.scw_container ``` \ No newline at end of file diff --git a/sib.scw_container/README.md b/sib.scw_container/README.md new file mode 100644 index 0000000..293c98b --- /dev/null +++ b/sib.scw_container/README.md @@ -0,0 +1,45 @@ +Role Name +========= + +Create container on scaleaway cloud + +Requirements +------------ + +None + +Role Variables +-------------- + +container_name: +scw_api_secret_key: +scw_api_default_project_id: + +scw_type: DEV1-S +scw_image: 26e4c50e-91d6-41a5-8898-9e40920e819d + +Dependencies +------------ + +None + +Example Playbook +---------------- + +```yaml +--- +- hosts: localhost + roles: + - tco.scw_container +``` + +License +------- + +BSD + +Author Information +------------------ + + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/sib.scw_container/defaults/main.yml b/sib.scw_container/defaults/main.yml new file mode 100644 index 0000000..0376500 --- /dev/null +++ b/sib.scw_container/defaults/main.yml @@ -0,0 +1,5 @@ +--- +# defaults file for tco.scw_container +scw_type: DEV1-S +scw_image: 26e4c50e-91d6-41a5-8898-9e40920e819d +scw_region: par1 diff --git a/sib.scw_container/dns.yml b/sib.scw_container/dns.yml new file mode 100644 index 0000000..192e761 --- /dev/null +++ b/sib.scw_container/dns.yml @@ -0,0 +1,11 @@ +# remove opendoor.fr[.] d'un ndm si besoin +# +--- +- hosts: localhost + vars: + zone: "opendoor.fr" + tasks: + - debug: + msg: "{{ item.record_name | ansible.builtin.regex_replace( '.'+zone, '' ) }}" + #msg: "{{ item.record_name | ansible.builtin.regex_replace( '.'+zone+'.?', '' ) }}" + loop: '{{ dns_records }}' \ No newline at end of file diff --git a/sib.scw_container/handlers/main.yml b/sib.scw_container/handlers/main.yml new file mode 100644 index 0000000..687dc78 --- /dev/null +++ b/sib.scw_container/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for tco.scw_container diff --git a/sib.scw_container/meta/main.yml b/sib.scw_container/meta/main.yml new file mode 100644 index 0000000..c572acc --- /dev/null +++ b/sib.scw_container/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/sib.scw_container/tasks/main.yml b/sib.scw_container/tasks/main.yml new file mode 100644 index 0000000..0ebd901 --- /dev/null +++ b/sib.scw_container/tasks/main.yml @@ -0,0 +1,22 @@ +--- + - name: create container + community.general.scaleway_compute: + commercial_type: "{{ scw_type }}" + name: "{{ container_name }}" + state: running + api_token: "{{ scw_api_secret_key }}" + image: "{{ scw_image}}" + project: "{{ scw_api_default_project_id }}" + public_ip: dynamic + wait: true + region: "{{ scw_region }}" + register: "container_info" + + - name: "generate host list" + ansible.builtin.lineinfile: + create: yes + path: "hostlist.yml" + line: " - { record_name: {{ item.msg.name}}, record_value: {{ item.msg.public_ip.address }} }" + state: present + loop: "{{ container_info.results }}" + diff --git a/sib.scw_container/tests/hostlist.yml b/sib.scw_container/tests/hostlist.yml new file mode 100644 index 0000000..6ea7a03 --- /dev/null +++ b/sib.scw_container/tests/hostlist.yml @@ -0,0 +1,7 @@ +dns_records: + - { record_name: test3.formation.opendoor.fr., record_type: A, record_value: 51.15.243.244 } + - { record_name: test4.formation.opendoor.fr., record_type: A, record_value: 212.47.251.35 } + - { record_name: test5.formation.opendoor.fr., record_type: A, record_value: 51.15.142.152 } + - { record_name: test3.formation.opendoor.fr, record_type: A, record_value: 51.15.243.244 } + - { record_name: test4.formation.opendoor.fr, record_type: A, record_value: 212.47.251.35 } + - { record_name: test5.formation.opendoor.fr, record_type: A, record_value: 51.15.142.152 } diff --git a/sib.scw_container/tests/inventory b/sib.scw_container/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/sib.scw_container/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/sib.scw_container/tests/range.yml b/sib.scw_container/tests/range.yml new file mode 100644 index 0000000..c1f4c71 --- /dev/null +++ b/sib.scw_container/tests/range.yml @@ -0,0 +1,7 @@ +--- +- hosts: localhost + tasks: + - name: with_sequence -> loop + ansible.builtin.debug: + msg: "{{ 'testuser%02x' | format(item) }}" + loop: "{{ range(0, 4 + 1, 2)|list }}" diff --git a/sib.scw_container/tests/test.yml b/sib.scw_container/tests/test.yml new file mode 100644 index 0000000..e07e099 --- /dev/null +++ b/sib.scw_container/tests/test.yml @@ -0,0 +1,12 @@ +# call me with --extra-vars=@hostlist.yml +--- +- hosts: localhost + vars: + container_list: + - { host_name: test3.formation.opendoor.fr, host_type: DEV1-S, host_image: 26e4c50e-91d6-41a5-8898-9e40920e819d } + - { host_name: test4.formation.opendoor.fr, host_type: DEV1-S, host_image: 26e4c50e-91d6-41a5-8898-9e40920e819d } + - { host_name: test5.formation.opendoor.fr, host_type: DEV1-S, host_image: 26e4c50e-91d6-41a5-8898-9e40920e819d } + container_action: running + roles: + - tco.scw_container + - { role: tco.dns, dns_action: "add" } diff --git a/sib.scw_container/vars/main.yml b/sib.scw_container/vars/main.yml new file mode 100644 index 0000000..e016ebf --- /dev/null +++ b/sib.scw_container/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for tco.scw_container