<p><a href="https://github.com/030/ansible-certbot" rel="nofollow noreferrer">https://github.com/030/ansible-certbot</a></p>

<p>main.yml</p>

<pre><code>- include: Debian.yml
when: ansible_os_family == 'Debian'
</code></pre>

<p>put everything that is required on debian like apt in Debian.yml and yum in Centos.yml.</p>

<p>In summary, it is possible to let all OS types use the same role if the OS specific things are put in separate files.</p>

<p>This is a more comprehensive example <a href="https://github.com/geerlingguy/ansible-role-mysql/blob/master/tasks/main.yml" rel="nofollow noreferrer">https://github.com/geerlingguy/ansible-role-mysql/blob/master/tasks/main.yml</a></p>

CC BY-SA 3.0
2017-09-15T14:25:34.553Z
I have already done this at a lower level, as each of the `build-machine-test-1` roles has their own separate builds based off `os_family`. My question is for a higher level where the build machines themselves need to be separated out before the roles are applied...
Rekovni
CC BY-SA 3.0
2017-09-15T14:23:24.017Z
2017-09-15T14:23:24.017Z
030
<p>So I managed to keep the same structure roughly, and managed to separate how the machines are generated, however still is a bit fuzzy and can probably be improved to be more efficient!</p>

<hr>

<p><strong>site.yml</strong></p>

<pre><code>- name: Apply common configuration to all nodes
  hosts: all
  roles:
    - common

- name: Configure and deploy test node 1
  hosts: build-test-node
  roles:
    - build-machine-test-1

- name: Configure and deploy test node 2
  hosts: build-test-node-2
  roles:
    - build-machine-test-2
</code></pre>

<hr>

<p><strong>environments/test/groups</strong></p>

<pre><code>[win_test_1_nodes]
win_build_machine_1

[mac_test_1_nodes]
mac_build_machine_1

[win_test_2_nodes]
win_build_machine_2

[mac_test_2_nodes]
mac_build_machine_2
</code></pre>

<hr>

<p><strong>environments/test/hosts</strong></p>

<pre><code>win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......
mac_build_machine_2   ansible_host=......
</code></pre>

<hr>

<p><strong>environments/test/meta</strong></p>

<pre><code>[win_test_1_nodes]
[mac_test_1_nodes]
[win_test_2_nodes]
[mac_test_2_nodes]

[win:children]
win_test_1_nodes
win_test_2_nodes

[mac:children]
mac_test_1_nodes
mac_test_2_nodes

[build-machine-test-1:children]
win_test_1_nodes
mac_test_1_nodes

[build-machine-test-2:children]
win_test_2_nodes
mac_test_2_nodes
</code></pre>

<hr>

<p>This solution allows more test nodes to be generated with separate roles added to them, and can just be added to the correct group for the machine to be provisioned.</p>

CC BY-SA 3.0
2017-09-15T16:32:08.357Z
2017-09-15T16:32:08.357Z
Rekovni
<p>I'm having issues working out how to best apply hosts/roles to different operating systems in Ansible.</p>

<p>I'm currently using Ansible to generate Windows/Mac/Linux VMs, and each of the VMs have different roles which need to be applied to them.</p>

<p>I've read the best practices and have tried to follow them, especially to use the grouping method, but I'm still unsure as they mostly mention webservers and dbservers.</p>

<hr>

<p>site.yml</p>

<pre><code>- name: Apply common configuration to all nodes
  hosts: all
  roles:
    - common

- name: Configure and deploy test node 1
  hosts: build-test-node
  roles:
    - build-machine-test-1

- name: Configure and deploy test node 2
  hosts: build-test-node-2
  roles:
    - build-machine-test-2
</code></pre>

<hr>

<p>environments/test/groups</p>

<pre><code>[win_build_nodes]
win_build_machine_1
win_build_machine_2

[mac_build_nodes]
mac_build_machine_1
</code></pre>

<hr>

<p>environments/test/hosts</p>

<pre><code>win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......
</code></pre>

<hr>

<p>environments/test/meta</p>

<pre><code>[mac_build_nodes]
[win_build_nodes]

[win:children]
win_build_nodes

[mac:children]
mac_build_nodes

[linux:children]

[build_nodes:children]
win_build_nodes
mac_build_nodes
</code></pre>

<p>The <code>environment/test/meta</code> file I believe needs to be changed with the <code>build_nodes:children</code>, but I'm not sure what to. Previously I was just having the VMs have all the roles built to it with the following:</p>

<p>Old site.yml</p>

<pre><code>- name: Apply everything
  hosts: build_nodes
  roles:
    - common
    - build-machine-test-1
    - build-machine-test-2
</code></pre>

<p>But then realised I didn't want everything being built on the same machine. Note, <code>build-machine-test-1</code> and <code>2</code> both have separate Windows/Mac/Linux builds as they detect what to include based on the operating system.</p>

<p>Can anyone suggest a better way of separating the VMs based on operating systems?</p>

<ansible><environments>
Deploying VMs using roles and hosts in Ansible based on operating system
Rekovni
# Deploying VMs using roles and hosts in Ansible based on operating system

(Asked by: username_0 on 2017-09-11)

I'm having issues working out how to best apply hosts/roles to different operating systems in Ansible.
I'm currently using Ansible to generate Windows/Mac/Linux VMs, and each of the VMs have different roles which need to be applied to them.
I've read the best practices and have tried to follow them, especially to use the grouping method, but I'm still unsure as they mostly mention webservers and dbservers.

site.yml
- name: Apply common configuration to all nodes
  hosts: all
  roles:
    - common

- name: Configure and deploy test node 1
  hosts: build-test-node
  roles:
    - build-machine-test-1

- name: Configure and deploy test node 2
  hosts: build-test-node-2
  roles:
    - build-machine-test-2


environments/test/groups
[win_build_nodes]
win_build_machine_1
win_build_machine_2

[mac_build_nodes]
mac_build_machine_1


environments/test/hosts
win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......


environments/test/meta
[mac_build_nodes]
[win_build_nodes]

[win:children]
win_build_nodes

[mac:children]
mac_build_nodes

[linux:children]

[build_nodes:children]
win_build_nodes
mac_build_nodes

The environment/test/meta file I believe needs to be changed with the build_nodes:children, but I'm not sure what to. Previously I was just having the VMs have all the roles built to it with the following:
Old site.yml
- name: Apply everything
  hosts: build_nodes
  roles:
    - common
    - build-machine-test-1
    - build-machine-test-2

But then realised I didn't want everything being built on the same machine. Note, build-machine-test-1 and 2 both have separate Windows/Mac/Linux builds as they detect what to include based on the operating system.
Can anyone suggest a better way of separating the VMs based on operating systems?

**Tags:** ansible, environments


---

## 2 answers

### Answer by username_0 on 2017-09-15. Score: 6 | Accepted answer

So I managed to keep the same structure roughly, and managed to separate how the machines are generated, however still is a bit fuzzy and can probably be improved to be more efficient!

site.yml
- name: Apply common configuration to all nodes
  hosts: all
  roles:
    - common

- name: Configure and deploy test node 1
  hosts: build-test-node
  roles:
    - build-machine-test-1

- name: Configure and deploy test node 2
  hosts: build-test-node-2
  roles:
    - build-machine-test-2


environments/test/groups
[win_test_1_nodes]
win_build_machine_1

[mac_test_1_nodes]
mac_build_machine_1

[win_test_2_nodes]
win_build_machine_2

[mac_test_2_nodes]
mac_build_machine_2


environments/test/hosts
win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......
mac_build_machine_2   ansible_host=......


environments/test/meta
[win_test_1_nodes]
[mac_test_1_nodes]
[win_test_2_nodes]
[mac_test_2_nodes]

[win:children]
win_test_1_nodes
win_test_2_nodes

[mac:children]
mac_test_1_nodes
mac_test_2_nodes

[build-machine-test-1:children]
win_test_1_nodes
mac_test_1_nodes

[build-machine-test-2:children]
win_test_2_nodes
mac_test_2_nodes


This solution allows more test nodes to be generated with separate roles added to them, and can just be added to the correct group for the machine to be provisioned.


---

### Answer by username_1 on 2017-09-15. Score: 1

https://github.com/username_1/ansible-certbot
main.yml
- include: Debian.yml
when: ansible_os_family == 'Debian'

put everything that is required on debian like apt in Debian.yml and yum in Centos.yml.
In summary, it is possible to let all OS types use the same role if the OS specific things are put in separate files.
This is a more comprehensive example https://github.com/geerlingguy/ansible-role-mysql/blob/master/tasks/main.yml

> **Comments:**
> **username_0**: I have already done this at a lower level, as each of the `build-machine-test-1` roles has their own separate builds based off `os_family`. My question is for a higher level where the build machines themselves need to be separated out before the roles are applied...  

---

