Answers: <p>According to this <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html?highlight=hosts%20yml#how-to-differentiate-staging-vs-production" rel="nofollow noreferrer">best practices post</a>, your file will look like this</p>
  
  <p>file: <code>test</code></p>
  
  <pre><code>[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
  </code></pre>
  
  <p>You can analyse your file by using <code>ansible-inventory</code>:</p>
  
  <pre><code>ansible-inventory --inventory-file=./test --list
  </code></pre>
  
  <p><strong>output:</strong></p>
  
  <pre><code>{
      "_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></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-08-23T13:50:43.643Z
Answers: 2018-08-23T13:50:43.643Z
Answers: Michael D.
Body: <p>Situation:</p>
  
  <p>I have a group "webservers" where I put all webservers in. As well as a group "mysqlservers" for the database servers respectively.</p>
  
  <p>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"</p>
  
  <p>This is my approach:</p>
  
  <pre><code>---
  all:
    hosts:
      host1:
      host2:
    children:
      webservers:
        hosts:
          web1:
          web2:
          web3
        children:
          lamp:
      mysqlservers:
        hosts:
          db1:
          db2:
          db3:
        children:
          lamp:
      lamp:
        hosts:
          lamp1:
          lamp2:
          lamp3:
  ...
  </code></pre>
  
  <p>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?</p>
  
Tags: <ansible><ansible-inventory>
Title: Ansible YAML inventory - Groups that are allways members of other groups
UserDisplayName: devnull
ThreadText: # Ansible YAML inventory - Groups that are allways members of other groups
  
  (Asked by: username_0 on 2018-08-19)
  
  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?
  
  **Tags:** ansible, ansible-inventory
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-23. Score: 4
  
  According to this [best practices post](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html?highlight=hosts%20yml#how-to-differentiate-staging-vs-production), 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"
          ]
      }
  }
  
  ```
  
  
  ---
  
  