/
usr
/
lib
/
python3.6
/
site-packages
/
cloudinit
/
config
/
/usr/lib/python3.6/site-packages/cloudinit/config
mkdir
upload
Name
Size
Mode
Actions
schemas/
-
0755
rm
__pycache__/
-
0755
rm
cc_ansible.py
8894
0644
edit
dl
rm
cc_apk_configure.py
5792
0644
edit
dl
rm
cc_apt_configure.py
42995
0644
edit
dl
rm
cc_apt_pipelining.py
2771
0644
edit
dl
rm
cc_bootcmd.py
2921
0644
edit
dl
rm
cc_byobu.py
3738
0644
edit
dl
rm
cc_ca_certs.py
9347
0644
edit
dl
rm
cc_chef.py
14102
0644
edit
dl
rm
cc_disable_ec2_metadata.py
2074
0644
edit
dl
rm
cc_disk_setup.py
33136
0644
edit
dl
rm
cc_fan.py
3093
0644
edit
dl
rm
cc_final_message.py
3470
0644
edit
dl
rm
cc_growpart.py
21538
0644
edit
dl
rm
cc_grub_dpkg.py
6812
0644
edit
dl
rm
cc_install_hotplug.py
3904
0644
edit
dl
rm
cc_keyboard.py
2439
0644
edit
dl
rm
cc_keys_to_console.py
3694
0644
edit
dl
rm
cc_landscape.py
5433
0644
edit
dl
rm
cc_locale.py
1903
0644
edit
dl
rm
cc_lxd.py
18580
0644
edit
dl
rm
cc_mcollective.py
6251
0644
edit
dl
rm
cc_migrator.py
3569
0644
edit
dl
rm
cc_mounts.py
20186
0644
edit
dl
rm
cc_ntp.py
21341
0644
edit
dl
rm
cc_package_update_upgrade_install.py
4652
0644
edit
dl
rm
cc_phone_home.py
5615
0644
edit
dl
rm
cc_power_state_change.py
7591
0644
edit
dl
rm
cc_puppet.py
14435
0644
edit
dl
rm
cc_reset_rmc.py
4575
0644
edit
dl
rm
cc_resizefs.py
10983
0644
edit
dl
rm
cc_resolv_conf.py
5103
0644
edit
dl
rm
cc_rh_subscription.py
17378
0644
edit
dl
rm
cc_rightscale_userdata.py
4387
0644
edit
dl
rm
cc_rsyslog.py
13800
0644
edit
dl
rm
cc_runcmd.py
2974
0644
edit
dl
rm
cc_salt_minion.py
6022
0644
edit
dl
rm
cc_scripts_per_boot.py
1697
0644
edit
dl
rm
cc_scripts_per_instance.py
1852
0644
edit
dl
rm
cc_scripts_per_once.py
1803
0644
edit
dl
rm
cc_scripts_user.py
1893
0644
edit
dl
rm
cc_scripts_vendor.py
2347
0644
edit
dl
rm
cc_seed_random.py
4837
0644
edit
dl
rm
cc_set_hostname.py
5254
0644
edit
dl
rm
cc_set_passwords.py
11234
0644
edit
dl
rm
cc_snap.py
6448
0644
edit
dl
rm
cc_spacewalk.py
3514
0644
edit
dl
rm
cc_ssh.py
15216
0644
edit
dl
rm
cc_ssh_authkey_fingerprints.py
4318
0644
edit
dl
rm
cc_ssh_import_id.py
6264
0644
edit
dl
rm
cc_timezone.py
1492
0644
edit
dl
rm
cc_ubuntu_advantage.py
17411
0644
edit
dl
rm
cc_ubuntu_autoinstall.py
4604
0644
edit
dl
rm
cc_ubuntu_drivers.py
4668
0644
edit
dl
rm
cc_update_etc_hosts.py
5283
0644
edit
dl
rm
cc_update_hostname.py
3963
0644
edit
dl
rm
cc_users_groups.py
8777
0644
edit
dl
rm
cc_wireguard.py
9439
0644
edit
dl
rm
cc_write_files.py
6819
0644
edit
dl
rm
cc_write_files_deferred.py
1721
0644
edit
dl
rm
cc_yum_add_repo.py
7633
0644
edit
dl
rm
cc_zypper_add_repo.py
6749
0644
edit
dl
rm
modules.py
12019
0644
edit
dl
rm
schema.py
56165
0644
edit
dl
rm
__init__.py
14
0644
edit
dl
rm
Edit:
/usr/lib/python3.6/site-packages/cloudinit/config/cc_ansible.py
(8894B)
"""ansible enables running on first boot either ansible-pull""" import abc import logging import os import re import sys import sysconfig from copy import deepcopy from textwrap import dedent from typing import Optional from cloudinit.cloud import Cloud from cloudinit.config import Config from cloudinit.config.schema import MetaSchema, get_meta_doc from cloudinit.distros import ALL_DISTROS, Distro from cloudinit.settings import PER_INSTANCE from cloudinit.subp import subp, which from cloudinit.util import Version, get_cfg_by_path meta: MetaSchema = { "id": "cc_ansible", "name": "Ansible", "title": "Configure ansible for instance", "frequency": PER_INSTANCE, "distros": [ALL_DISTROS], "activate_by_schema_keys": ["ansible"], "description": dedent( """\ This module provides ``ansible`` integration for augmenting cloud-init's configuration of the local node. This module installs ansible during boot and then uses ``ansible-pull`` to run the playbook repository at the remote URL. """ ), "examples": [ dedent( """\ ansible: package_name: ansible-core install_method: distro pull: url: "https://github.com/holmanb/vmboot.git" playbook_name: ubuntu.yml """ ), dedent( """\ ansible: package_name: ansible-core install_method: pip pull: url: "https://github.com/holmanb/vmboot.git" playbook_name: ubuntu.yml """ ), ], } __doc__ = get_meta_doc(meta) LOG = logging.getLogger(__name__) CFG_OVERRIDE = "ansible_config" class AnsiblePull(abc.ABC): def __init__(self, distro: Distro): self.cmd_pull = ["ansible-pull"] self.cmd_version = ["ansible-pull", "--version"] self.distro = distro self.env = os.environ self.run_user: Optional[str] = None # some ansible modules directly reference os.environ["HOME"] # and cloud-init might not have that set, default: /root self.env["HOME"] = self.env.get("HOME", "/root") def get_version(self) -> Optional[Version]: stdout, _ = self.do_as(self.cmd_version) first_line = stdout.splitlines().pop(0) matches = re.search(r"([\d\.]+)", first_line) if matches: version = matches.group(0) return Version.from_str(version) return None def pull(self, *args) -> str: stdout, _ = self.do_as([*self.cmd_pull, *args]) return stdout def check_deps(self): if not self.is_installed(): raise ValueError("command: ansible is not installed") def do_as(self, command: list, **kwargs): if not self.run_user: return self.subp(command, **kwargs) return self.distro.do_as(command, self.run_user, **kwargs) def subp(self, command, **kwargs): return subp(command, env=self.env, **kwargs) @abc.abstractmethod def is_installed(self): pass @abc.abstractmethod def install(self, pkg_name: str): pass class AnsiblePullPip(AnsiblePull): def __init__(self, distro: Distro, user: Optional[str]): super().__init__(distro) self.run_user = user # Add pip install site to PATH user_base, _ = self.do_as( [sys.executable, "-c", "'import site; print(site.getuserbase())'"] ) ansible_path = f"{user_base}/bin/" old_path = self.env.get("PATH") if old_path: self.env["PATH"] = ":".join([old_path, ansible_path]) else: self.env["PATH"] = ansible_path def install(self, pkg_name: str): """should cloud-init grow an interface for non-distro package managers? this seems reusable """ if not self.is_installed(): # bootstrap pip if required try: import pip # noqa: F401 except ImportError: self.distro.install_packages([self.distro.pip_package_name]) cmd = [ sys.executable, "-m", "pip", "install", ] if os.path.exists( os.path.join( sysconfig.get_path("stdlib"), "EXTERNALLY-MANAGED" ) ): cmd.append("--break-system-packages") if self.run_user: cmd.append("--user") self.do_as([*cmd, "--upgrade", "pip"]) self.do_as([*cmd, pkg_name]) def is_installed(self) -> bool: stdout, _ = self.do_as([sys.executable, "-m", "pip", "list"]) return "ansible" in stdout class AnsiblePullDistro(AnsiblePull): def install(self, pkg_name: str): if not self.is_installed(): self.distro.install_packages([pkg_name]) def is_installed(self) -> bool: return bool(which("ansible")) def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: ansible_cfg: dict = cfg.get("ansible", {}) ansible_user = ansible_cfg.get("run_user") install_method = ansible_cfg.get("install_method") setup_controller = ansible_cfg.get("setup_controller") galaxy_cfg = ansible_cfg.get("galaxy") pull_cfg = ansible_cfg.get("pull") package_name = ansible_cfg.get("package_name", "") if ansible_cfg: ansible: AnsiblePull validate_config(ansible_cfg) distro: Distro = cloud.distro if install_method == "pip": ansible = AnsiblePullPip(distro, ansible_user) else: ansible = AnsiblePullDistro(distro) ansible.install(package_name) ansible.check_deps() ansible_config = ansible_cfg.get("ansible_config", "") if ansible_config: ansible.env[CFG_OVERRIDE] = ansible_config if galaxy_cfg: ansible_galaxy(galaxy_cfg, ansible) if pull_cfg: run_ansible_pull(ansible, deepcopy(pull_cfg)) if setup_controller: ansible_controller(setup_controller, ansible) def validate_config(cfg: dict): required_keys = ( "install_method", "package_name", ) for key in required_keys: if not get_cfg_by_path(cfg, key): raise ValueError(f"Missing required key '{key}' from {cfg}") if cfg.get("pull"): for key in "pull/url", "pull/playbook_name": if not get_cfg_by_path(cfg, key): raise ValueError(f"Missing required key '{key}' from {cfg}") controller_cfg = cfg.get("setup_controller") if controller_cfg: if not any( [ controller_cfg.get("repositories"), controller_cfg.get("run_ansible"), ] ): raise ValueError(f"Missing required key from {controller_cfg}") install = cfg["install_method"] if install not in ("pip", "distro"): raise ValueError("Invalid install method {install}") def filter_args(cfg: dict) -> dict: """remove boolean false values""" return { key.replace("_", "-"): value for (key, value) in cfg.items() if value is not False } def run_ansible_pull(pull: AnsiblePull, cfg: dict): playbook_name: str = cfg.pop("playbook_name") v = pull.get_version() if not v: LOG.warning("Cannot parse ansible version") elif v < Version(2, 7, 0): # diff was added in commit edaa0b52450ade9b86b5f63097ce18ebb147f46f if cfg.get("diff"): raise ValueError( f"Ansible version {v.major}.{v.minor}.{v.patch}" "doesn't support --diff flag, exiting." ) stdout = pull.pull( *[ f"--{key}={value}" if value is not True else f"--{key}" for key, value in filter_args(cfg).items() ], playbook_name, ) if stdout: sys.stdout.write(f"{stdout}") def ansible_galaxy(cfg: dict, ansible: AnsiblePull): actions = cfg.get("actions", []) if not actions: LOG.warning("Invalid config: %s", cfg) for command in actions: ansible.do_as(command) def ansible_controller(cfg: dict, ansible: AnsiblePull): for repository in cfg.get("repositories", []): ansible.do_as( ["git", "clone", repository["source"], repository["path"]] ) for args in cfg.get("run_ansible", []): playbook_dir = args.pop("playbook_dir") playbook_name = args.pop("playbook_name") command = [ "ansible-playbook", playbook_name, *[f"--{key}={value}" for key, value in filter_args(args).items()], ] ansible.do_as(command, cwd=playbook_dir)
Save
cmd:
run