Proper way to define default variables for all hosts in Ansible
Asked Answered
B

3

16

There is a definition of [all:vars] in my ansible inventory file as follows:

[all:vars]
ansible_shell_type=bash
ansible_user=certain_user
ansible_ssh_common_args="-o ConnectionAttempts=10"

I plan to move such variables into ansible.cfg to set defaults for all hosts. Would it simply work in a similar way or is there any circumstances to consider? What else alternatives available to remove [all:vars] from the inventory file?

Barogram answered 13/9, 2017 at 13:9 Comment(0)
T
15

ansible.cfg is a configuration file and not inventory, you can't put variables in there.

[all:vars] is a syntax to define group variables for a group (special group all in this case) inside inventory file.

If you want to split out variable definition from main inventory file (which is preferred practice, by the way!), you may want to read this chapter: Splitting Out Host and Group Specific Data.

Excerpt:

In addition to storing variables directly in the inventory file, host and group variables can be stored in individual files relative to the inventory file

Assuming the inventory file path is:

/etc/ansible/hosts

If the host is named ‘foosball’, and in groups ‘raleigh’ and ‘webservers’, variables in YAML files at the following locations will be made available to the host:

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
Touter answered 13/9, 2017 at 15:48 Comment(1)
That's the very reason why there may be many different answers to the same question. Who knows what's OP's main goal was? I see question about general group vars, you see question about ansible-configuration vars. Upvoting your answer.Touter
R
15

Consider moving the variables to /group_vars/all to achieve the same in a supported way.

Ricoricochet answered 13/9, 2017 at 13:46 Comment(1)
just fyi group_vars will produce default variables for all hosts, but if you want to override them per hostname specific inside the inventory - you can't as it takes precedence over everything else.Brookbrooke
T
15

ansible.cfg is a configuration file and not inventory, you can't put variables in there.

[all:vars] is a syntax to define group variables for a group (special group all in this case) inside inventory file.

If you want to split out variable definition from main inventory file (which is preferred practice, by the way!), you may want to read this chapter: Splitting Out Host and Group Specific Data.

Excerpt:

In addition to storing variables directly in the inventory file, host and group variables can be stored in individual files relative to the inventory file

Assuming the inventory file path is:

/etc/ansible/hosts

If the host is named ‘foosball’, and in groups ‘raleigh’ and ‘webservers’, variables in YAML files at the following locations will be made available to the host:

/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
Touter answered 13/9, 2017 at 15:48 Comment(1)
That's the very reason why there may be many different answers to the same question. Who knows what's OP's main goal was? I see question about general group vars, you see question about ansible-configuration vars. Upvoting your answer.Touter
G
3

Of course you can define these values in ansible.cfg, they just have different names, so refer to the manual beforehand:

The values set in the configuration file will apply to all hosts unless overridden by specific settings.

Geer answered 13/9, 2017 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.