import os, sys, subprocess, textwrap, stat
BASE = "/content material/ansible_lab" if os.path.isdir("/content material") else os.path.expanduser("~/ansible_lab")
os.makedirs(BASE, exist_ok=True)
ENV = os.environ.copy()
ENV["ANSIBLE_CONFIG"] = os.path.be part of(BASE, "ansible.cfg")
ENV["ANSIBLE_FORCE_COLOR"] = "1"
ENV["PY_COLORS"] = "0"
def banner(title):
print("n" + "=" * 78 + f"n {title}n" + "=" * 78)
def write(relpath, content material):
"""Write a dedented file underneath BASE, creating father or mother dirs."""
path = os.path.be part of(BASE, relpath)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(textwrap.dedent(content material).lstrip("n"))
return path
def sh(cmd, title=None):
"""Run a shell command from BASE, stream stdout, by no means elevate."""
if title:
banner(title)
print(f"$ {cmd}n")
p = subprocess.run(cmd, shell=True, cwd=BASE, env=ENV,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, textual content=True)
print(p.stdout)
return p.returncode
banner("STEP 1 — Putting in ansible-core")
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "ansible-core"], test=True)
sh("ansible --version")
write("ansible.cfg", """
[defaults]
stock = ./stock.ini
roles_path = ./roles
library = ./library
filter_plugins = ./filter_plugins
vault_password_file = ./vault_pass.txt
host_key_checking = False
retry_files_enabled = False
interpreter_python = auto_silent
callback_result_format = yaml
deprecation_warnings = False
localhost_warning = False
nocows = 1
[privilege_escalation]
grow to be = False
""")
write("stock.ini", """
[webservers]
web1 ansible_connection=native
web2 ansible_connection=native
[dbservers]
db1 ansible_connection=native
[datacenter:children]
webservers
dbservers
""")
Source link