2

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:

---
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?

4

According to this best practices post, your file will look like this

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]
lamp

You can analyse your file by using ansible-inventory:

ansible-inventory --inventory-file=./test --list

output:

{
    "_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"
        ]
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.