HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Deploying VMs using roles and hosts in Ansible based on operating system

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
hostsdeployingrolessystemvmsoperatingusingbasedandansible

Problem

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?

Solution

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.

Code Snippets

- 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
[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
win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......
mac_build_machine_2   ansible_host=......
[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

Context

StackExchange DevOps Q#1981, answer score: 6

Revisions (0)

No revisions yet.