patternMinor
Ansible YAML inventory - Groups that are allways members of other groups
Viewed 0 times
groupsareinventorythatmembersotheryamlansibleallways
Problem
Situation:
I have a group "webservers" where I put all webservers in. As well as a group "mysqlservers" for the database servers respectively.
Now I do also manage some LAMP servers and I want them to be always part of the webservers and mysqlservers group, as well as manage them in a group "lamp"
This is my approach:
Is that the correct approach? If I followed the ansible docs examples, I would have defined the "lamp" hosts as children of webservers and mysqlservers, but I really want to reduce redundancy. So is it possible to create the group as a child of "all" and just reference it as children of the specified groups without repeating the hosts?
I have a group "webservers" where I put all webservers in. As well as a group "mysqlservers" for the database servers respectively.
Now I do also manage some LAMP servers and I want them to be always part of the webservers and mysqlservers group, as well as manage them in a group "lamp"
This is my approach:
---
all:
hosts:
host1:
host2:
children:
webservers:
hosts:
web1:
web2:
web3
children:
lamp:
mysqlservers:
hosts:
db1:
db2:
db3:
children:
lamp:
lamp:
hosts:
lamp1:
lamp2:
lamp3:
...Is that the correct approach? If I followed the ansible docs examples, I would have defined the "lamp" hosts as children of webservers and mysqlservers, but I really want to reduce redundancy. So is it possible to create the group as a child of "all" and just reference it as children of the specified groups without repeating the hosts?
Solution
According to this best practices post, your file will look like this
file:
You can analyse your file by using
output:
file:
test[webservers]
web1
web2
web3
[mysqlservers]
db1
db2
db3
[lamp]
lamp1
lamp2
lamp3
[misc]
host1
host2
[all:children]
misc
webservers
mysqlservers
lamp
[webservers:children]
lamp
[mysqlservers:children]
lampYou can analyse your file by using
ansible-inventory:ansible-inventory --inventory-file=./test --listoutput:
{
"_meta": {
"hostvars": {
"db1": {},
"db2": {},
"db3": {},
"host1": {},
"host2": {},
"lamp1": {},
"lamp2": {},
"lamp3": {},
"web1": {},
"web2": {},
"web3": {}
}
},
"all": {
"children": [
"lamp",
"misc",
"mysqlservers",
"ungrouped",
"webservers"
]
},
"lamp": {
"hosts": [
"lamp1",
"lamp2",
"lamp3"
]
},
"misc": {
"hosts": [
"host1",
"host2"
]
},
"mysqlservers": {
"children": [
"lamp"
],
"hosts": [
"db1",
"db2",
"db3"
]
},
"ungrouped": {},
"webservers": {
"children": [
"lamp"
],
"hosts": [
"web1",
"web2",
"web3"
]
}
}Code Snippets
[webservers]
web1
web2
web3
[mysqlservers]
db1
db2
db3
[lamp]
lamp1
lamp2
lamp3
[misc]
host1
host2
[all:children]
misc
webservers
mysqlservers
lamp
[webservers:children]
lamp
[mysqlservers:children]
lampansible-inventory --inventory-file=./test --list{
"_meta": {
"hostvars": {
"db1": {},
"db2": {},
"db3": {},
"host1": {},
"host2": {},
"lamp1": {},
"lamp2": {},
"lamp3": {},
"web1": {},
"web2": {},
"web3": {}
}
},
"all": {
"children": [
"lamp",
"misc",
"mysqlservers",
"ungrouped",
"webservers"
]
},
"lamp": {
"hosts": [
"lamp1",
"lamp2",
"lamp3"
]
},
"misc": {
"hosts": [
"host1",
"host2"
]
},
"mysqlservers": {
"children": [
"lamp"
],
"hosts": [
"db1",
"db2",
"db3"
]
},
"ungrouped": {},
"webservers": {
"children": [
"lamp"
],
"hosts": [
"web1",
"web2",
"web3"
]
}
}Context
StackExchange DevOps Q#4784, answer score: 4
Revisions (0)
No revisions yet.