0
votes

I wrote a playbook/roles to create list of vhost on a webserver.

I have a big dict describing all the sites. It's not easily maintainable and I would like to define each vhost in a separate vars file and loop on these files. But I don't really know how to do this.

Here is my actual code:

    - name: "Create domain configuration"
  include_role:
    name: webserver/domain
  loop: "{{ list_domains | dict2items }}"
  loop_control:
    loop_var: domain

With the dict definition :

list_domains:
  domain1:
    server_name: "domain1.com"
    server_alias:
      - "domain1.fr"
    certificate:
      method: "webroot"
    php_custom_values:
      memory_limit: "128M"
  database:
    type: "mysql"
  cms:
    type: "prestashop"
  domain2:
    server_name: domain2.com
    php_custom_values:
      memory_limit: "512M"

The goal is to have a directory with domain1.yml and domain2.yml with the domains definitions

1

1 Answers

0
votes

Use host_vars directory creating a file with the name of each of your hosts.

Example:

Directory

 host_vars/
   - domain1.com
   - domain2.comq
 your_playbook.yml
 roles/
   - role1/
    ......

domain1.com file:

    server_name: "domain1.com"
    server_alias:
      - "domain1.fr"
    certificate:
      method: "webroot"
    php_custom_values:
      memory_limit: "128M"
    database:
      type: "mysql"
    cms:
      type: "prestashop"

domain2.com file:

    server_name: domain2.com
    php_custom_values:
      memory_limit: "512M"

Each host has its own variables, then modify your playbook as below

- hosts: domain1.com, domain2.com
  tasks:

  - name: "Create domain configuration"
    include_role:
      name: webserver/domain