|
16 | 16 | import json
|
17 | 17 | import os
|
18 | 18 | import tempfile
|
19 |
| -from typing import Dict, List, Optional, Union |
| 19 | +from typing import Any, Dict, List, Optional, Union |
20 | 20 |
|
21 | 21 | import testinfra
|
22 | 22 | from testinfra.utils import cached_property
|
@@ -78,18 +78,64 @@ def get_ansible_host(config, inventory, host, ssh_config=None, ssh_identity_file
|
78 | 78 | "paramiko_ssh": "paramiko",
|
79 | 79 | "smart": "ssh",
|
80 | 80 | }.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 | + |
89 | 135 | kwargs: Dict[str, Union[str, bool]] = {}
|
90 |
| - if hostvars.get("ansible_become", False): |
| 136 | + if get_config("ansible_become", False): |
91 | 137 | kwargs["sudo"] = True
|
92 |
| - kwargs["sudo_user"] = hostvars.get("ansible_become_user") |
| 138 | + kwargs["sudo_user"] = get_config("ansible_become_user") |
93 | 139 | if ssh_config is not None:
|
94 | 140 | kwargs["ssh_config"] = ssh_config
|
95 | 141 | if ssh_identity_file is not None:
|
|
0 commit comments