Search code examples
ansibleyamlansible-inventory

Ansible separating hosts, group_vars and host_vars


I need to change some files in YAML format from one "big" yml to several smaller files. Unfortunately, I do not know which part belongs where. For example, I have the following .yml:

all:
  hosts:
    station01:
      dns_name: localhost
      ansible_host: localhost
    work01:
      dns_name: X.X.X.X
      ansible_host: Y.Y.Y.Y
  vars:
    yum_proxy: false
    zabbix_proxy_ip: A.A.A.A
    yum_proxy_ip:
    dns_servers:
      - B.B.B.B
      - C.C.C.C
    timezone: XXX
    ntp_servers:
      - D.D.D.D
    disable_firewall_tasks: true

  children:
    deploy:
      hosts:
        station01:
    linux:
      hosts:
        work01:
    work_users:
      hosts:
        work01:
      vars:
        users:
          - john: user
          - mike: user

I believe that hosts file should looke like this:

all:
  hosts:
    station01
    work01

  children:
    deploy:
      hosts:
        station01:
    linux:
      hosts:
        work01:
    work_users:
      hosts:
        work01:

The group_vars "all.yml" looks like this, I guess:

---
all:
  yum_proxy: false
  zabbix_proxy_ip: A.A.A.A
  yum_proxy_ip:
  dns_servers:
    - B.B.B.B
    - C.C.C.C
  timezone: XXX
  ntp_servers:
    - D.D.D.D
  disable_firewall_tasks: true

And this is where the problem begins. I do not know where to put the "users:". Should it be in host_vars "work01.yml" assigned to one host, like this:

---
work01:
  dns_name: X.X.X.X
  ansible_host: Y.Y.Y.Y
  users:
    - john: user
    - mike: user

Or should it be contained in the group_vars file named "work_users.yml"?

---
users:
  - john: user
  - mike: user

What the files host_vars and group_vars should look like? Any kind of help would be appreciated.


Solution

  • Q: "What the files host_vars and group_vars should look like?"

    A: There are more options of how to name and organize the directories and files. See Organizing host and group variables.

    Files named after the corresponding groups and hostnames

    Below is an example of putting the variables into the files named after the corresponding groups and hostnames

    shell> cat group_vars/all.yml
    ---
    yum_proxy: false
    zabbix_proxy_ip: A.A.A.A
    yum_proxy_ip:
    dns_servers:
      - B.B.B.B
      - C.C.C.C
    timezone: XXX
    ntp_servers:
      - D.D.D.D
    disable_firewall_tasks: true
    
    shell> cat group_vars/work_users.yml
    ---
    users:
      - john: user
      - mike: user
    
    shell> cat host_vars/work01.yml
    ---
    dns_name: X.X.X.X
    ansible_host: Y.Y.Y.Y
    
    shell> cat host_vars/station01.yml
    ---
    dns_name: localhost
    ansible_host: localhost
    

    Files inside the directories named after the corresponding groups and hostnames

    The next option is putting the variables into the files inside the directories named after the corresponding groups and hostnames

    shell> cat group_vars/all/network.yml
    ---
    yum_proxy: false
    zabbix_proxy_ip: A.A.A.A
    yum_proxy_ip:
    dns_servers:
      - B.B.B.B
      - C.C.C.C
    timezone: XXX
    ntp_servers:
      - D.D.D.D
    disable_firewall_tasks: true
    
    shell> cat group_vars/work_users/users.yml
    ---
    users:
      - john: user
      - mike: user
    
    shell> cat host_vars/work01/network.yml
    ---
    dns_name: X.X.X.X
    ansible_host: Y.Y.Y.Y
    
    shell> cat host_vars/station01/network.yml
    ---
    dns_name: localhost
    ansible_host: localhost