Skip to content

Commit 6fca336

Browse files
committed
Use ansible_facts to reference facts
By default, Ansible injects a variable for every fact, prefixed with ansible_. This can result in a large number of variables for each host, which at scale can incur a performance penalty. Ansible provides a configuration option [0] that can be set to False to prevent this injection of facts. In this case, facts should be referenced via ansible_facts.<fact>. This change updates all references to Ansible facts from using individual fact variables to using the items in the ansible_facts dictionary. This allows users to disable fact variable injection in their Ansible configuration, which may provide some performance improvement. [0] https://docs.ansible.com/ansible/latest/reference_appendices/config.html#inject-facts-as-vars
1 parent 83d473e commit 6fca336

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

ansible.cfg

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[defaults]
2+
# Use the YAML stdout callback plugin.
3+
stdout_callback = yaml
4+
# Use the stdout_callback when running ad-hoc commands.
5+
bin_ansible_callbacks = True
6+
# Disable fact variable injection to improve performance.
7+
inject_facts_as_vars = False
8+
9+
[ssh_connection]
10+
pipelining = True

roles/vault/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ Example playbook (used with OpenStack Kayobe)
103103
roles:
104104
- role: stackhpc.hashicorp.vault
105105
consul_bind_interface: "{{ internal_net_interface }}"
106-
consul_bind_ip: "{{ internal_net_ips[ansible_hostname] }}"
107-
vault_bind_address: "{{ external_net_ips[ansible_hostname] }}"
106+
consul_bind_ip: "{{ internal_net_ips[inventory_hostname] }}"
107+
vault_bind_address: "{{ external_net_ips[inventory_hostname] }}"
108108
vault_api_addr: "https://{{ external_net_fqdn }}:8200"
109109
vault_config_dir: "/opt/kayobe/vault"
110110
```

roles/vault/defaults/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ vault_config: >
4848
}
4949
5050
consul_bind_interface: ""
51-
consul_bind_ip: "{{ hostvars[inventory_hostname]['ansible_'~consul_bind_interface].ipv4.address }}"
51+
consul_bind_ip: "{{ hostvars[inventory_hostname].ansible_facts[consul_bind_interface].ipv4.address }}"
5252

5353
# Docker options
5454
vault_container: {}

roles/vault/tasks/consul.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
CONSUL_CLIENT_INTERFACE: "{{ consul_bind_interface }}"
1515
command: >
1616
consul agent
17-
-bind "{{ hostvars[inventory_hostname]['ansible_'~consul_bind_interface].ipv4.address }}"
17+
-bind "{{ hostvars[inventory_hostname].ansible_facts[consul_bind_interface].ipv4.address }}"
1818
-data-dir /consul/data
1919
-server
2020
-bootstrap-expect "{{ ansible_play_hosts | length }}"
2121
{% for host in ansible_play_hosts %}
2222
{% if host != inventory_hostname %}
23-
-retry-join "{{ hostvars[host]['ansible_'~consul_bind_interface].ipv4.address }}"
23+
-retry-join "{{ hostvars[host].ansible_facts[consul_bind_interface].ipv4.address }}"
2424
{% endif %}
2525
{% endfor %}
2626
become: True

0 commit comments

Comments
 (0)