Skip to content

Commit 4614915

Browse files
BenoitKnechtphilpep
authored andcommitted
ansible: Support environment variables
Ansible options can now consistently be specified in the `ansible.cfg` configuration file, in group or host variables or in environment variables, the latter taking precedence on the former. | Configuration File | Host Variable | Environment | | ----------------------------------------- | --------------------- | --------------------- | | `[privilege_escalation]`<br>`become` | `ansible_become` | `ANSIBLE_BECOME` | | `[privilege_escalation]`<br>`become_user` | `ansible_become_user` | `ANSIBLE_BECOME_USER` | | `[defaults]`<br>`remote_port` | `ansible_port` | `ANSIBLE_REMOTE_PORT` | | `[defaults]`<br>`remote_user` | `ansible_user` | `ANSIBLE_REMOTE_USER` | Signed-off-by: Benoît Knecht <[email protected]>
1 parent 1133428 commit 4614915

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

testinfra/utils/ansible_runner.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import json
1717
import os
1818
import tempfile
19-
from typing import Dict, List, Optional, Union
19+
from typing import Any, Dict, List, Optional, Union
2020

2121
import testinfra
2222
from testinfra.utils import cached_property
@@ -78,18 +78,64 @@ def get_ansible_host(config, inventory, host, ssh_config=None, ssh_identity_file
7878
"paramiko_ssh": "paramiko",
7979
"smart": "ssh",
8080
}.get(connection, connection)
81-
testinfra_host = hostvars.get("ansible_host", host)
82-
user = config.get("defaults", "remote_user", fallback=None)
83-
if "ansible_user" in hostvars:
84-
user = hostvars.get("ansible_user")
85-
password = hostvars.get("ansible_ssh_pass")
86-
port = config.get("defaults", "remote_port", fallback=None)
87-
if "ansible_port" in hostvars:
88-
port = hostvars.get("ansible_port")
81+
82+
options: Dict[str, Any] = {
83+
"ansible_become": {
84+
"ini": {
85+
"section": "privilege_escalation",
86+
"key": "become",
87+
},
88+
"environment": "ANSIBLE_BECOME",
89+
},
90+
"ansible_become_user": {
91+
"ini": {
92+
"section": "privilege_escalation",
93+
"key": "become_user",
94+
},
95+
"environment": "ANSIBLE_BECOME_USER",
96+
},
97+
"ansible_port": {
98+
"ini": {
99+
"section": "defaults",
100+
"key": "remote_port",
101+
},
102+
"environment": "ANSIBLE_REMOTE_PORT",
103+
},
104+
"ansible_user": {
105+
"ini": {
106+
"section": "defaults",
107+
"key": "remote_user",
108+
},
109+
"environment": "ANSIBLE_REMOTE_USER",
110+
},
111+
}
112+
113+
def get_config(name, default=None):
114+
value = default
115+
option = options.get(name, {})
116+
117+
ini = option.get("ini")
118+
if ini:
119+
value = config.get(ini["section"], ini["key"], fallback=default)
120+
121+
if name in hostvars:
122+
value = hostvars[name]
123+
124+
var = option.get("environment")
125+
if var and var in os.environ:
126+
value = os.environ[var]
127+
128+
return value
129+
130+
testinfra_host = get_config("ansible_host", host)
131+
user = get_config("ansible_user")
132+
password = get_config("ansible_ssh_pass")
133+
port = get_config("ansible_port")
134+
89135
kwargs: Dict[str, Union[str, bool]] = {}
90-
if hostvars.get("ansible_become", False):
136+
if get_config("ansible_become", False):
91137
kwargs["sudo"] = True
92-
kwargs["sudo_user"] = hostvars.get("ansible_become_user")
138+
kwargs["sudo_user"] = get_config("ansible_become_user")
93139
if ssh_config is not None:
94140
kwargs["ssh_config"] = ssh_config
95141
if ssh_identity_file is not None:

0 commit comments

Comments
 (0)