role need a name prefix, a number of containers to create

This commit is contained in:
2023-10-12 22:34:30 +02:00
parent bec5624f75
commit 43abe12bbd
4 changed files with 36 additions and 17 deletions

View File

@@ -14,9 +14,13 @@ Role Variables
scw_api_secret_key:
scw_api_default_project_id:
scw_type: DEV1-S
container_action; delete or running
container_list: list of container
container_number: number of container to create
container_prefix: name of prefix to container #
container_domain: default formation.opendoor.fr
scw_gen_hostlist: default true, wether we create a hostlist.yml file containing containers name and ip (to be fed to dns role to create records)
container_image: id of image default alma9 26e4c50e-91d6-41a5-8898-9e40920e819d
Dependencies
------------
@@ -31,14 +35,14 @@ Example Playbook
- hosts: localhost
vars:
container_action: running
container_list:
- test1
- test2
- test3
container_number: 4
container_prefix: plop
container_image: lezknfe2324
roles:
- tco.scw_container
```
will create containers plop{1..4}.formation.opendoor.fr
License
-------

View File

@@ -1,3 +1,6 @@
---
# defaults file for tco.scw_container
scw_gen_hostlist: true
scw_type: DEV1-S
container_domain: formation.opendoor.fr
container_image: 26e4c50e-91d6-41a5-8898-9e40920e819d

View File

@@ -1,22 +1,30 @@
---
- community.general.scaleway_compute:
commercial_type: DEV1-S
commercial_type: "{{ item.host_type|default( scw_type )}}"
region: par1
name: "{{ item}}"
name: "{{ container_prefix+'%02x' | format(item) }}"
state: "{{ (container_action == 'delete' ) | ternary( 'absent', 'running' ) }}"
api_token: "{{ scw_api_secret_key }}"
image: 26e4c50e-91d6-41a5-8898-9e40920e819d
image: "{{ container_image}}"
project: "{{ scw_api_default_project_id }}"
public_ip: dynamic
loop: "{{ container_list }}"
wait: true
loop: "{{ range(0, container_number , 1)|list }}"
register: "container_info"
- name: "generate host list"
ansible.builtin.lineinfile:
create: yes
path: "hostlist.yml"
line: "- { record_name: {{ item.msg.name}}, record_type: A, record_value: {{ item.msg.public_ip.address }} }"
state: present
loop: "{{ container_info.results }}"
block:
- ansible.builtin.lineinfile:
create: yes
path: "hostlist.yml"
line: "dns_records:"
insertbefore: BOF
- ansible.builtin.lineinfile:
create: yes
path: "hostlist.yml"
line: " - { record_name: {{ item.msg.name}}, record_type: A, record_value: {{ item.msg.public_ip.address }} }"
state: present
loop: "{{ container_info.results }}"
when: scw_gen_hostlist is true and container_action !='delete'

View File

@@ -1,5 +1,9 @@
---
- hosts: localhost
remote_user: root
vars:
container_action: running
container_number: 4
container_prefix: plop
container_image: lezknfe2324
roles:
- tco.scw_container
- tco.scw_container