예제 #1
0
파일: api.py 프로젝트: dloup/enoslib
def _generate_inventory(roles):
    """Generates an inventory files from roles

    :param roles: dict of roles (roles -> list of Host)
    """
    inventory = EnosInventory(roles=roles)
    return inventory.to_ini_string()
예제 #2
0
 def test_address_gateway_same_user(self):
     h = Host("1.2.3.4", user="******", extra={'gateway': '4.3.2.1'})
     enos_inventory = EnosInventory(roles={"r1": [h]})
     ini = enos_inventory.to_ini_string()
     line = _find_host_line(ini, "r1")
     self.assertEqual(
         "1.2.3.4 ansible_host=1.2.3.4 ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh -W %h:%p -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l foo 4.3.2.1\"' ansible_ssh_user='******'",
         line)
예제 #3
0
 def test_port(self):
     h = Host("1.2.3.4", port=2222)
     enos_inventory = EnosInventory(roles={"r1": [h]})
     ini = enos_inventory.to_ini_string()
     line = _find_host_line(ini, "r1")
     self.assertEqual(
         "1.2.3.4 ansible_host=1.2.3.4 ansible_port='2222' ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'",
         line)
예제 #4
0
 def test_address_user(self):
     h = Host("1.2.3.4", user="******")
     enos_inventory = EnosInventory(roles={"r1": [h]})
     ini = enos_inventory.to_ini_string()
     line = _find_host_line(ini, "r1")
     self.assertEqual(
         "1.2.3.4 ansible_host=1.2.3.4 ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' ansible_ssh_user='******'",
         line,
     )
예제 #5
0
파일: api.py 프로젝트: dloup/enoslib
def _load_defaults(inventory_path=None,
                   roles=None,
                   extra_vars=None,
                   tags=None,
                   basedir=False):
    """Load common defaults data structures.

    For factorization purpose."""

    extra_vars = extra_vars or {}
    tags = tags or []
    loader = DataLoader()
    if basedir:
        loader.set_basedir(basedir)

    inventory = EnosInventory(loader=loader,
                              sources=inventory_path,
                              roles=roles)

    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # seems mandatory to load group_vars variable
    if basedir:
        variable_manager.safe_basedir = True

    if extra_vars:
        variable_manager.extra_vars = extra_vars

    # NOTE(msimonin): The ansible api is "low level" in the
    # sense that we are redefining here all the default values
    # that are usually enforce by ansible called from the cli
    Options = namedtuple("Options", [
        "listtags", "listtasks", "listhosts", "syntax", "connection",
        "module_path", "forks", "private_key_file", "ssh_common_args",
        "ssh_extra_args", "sftp_extra_args", "scp_extra_args", "become",
        "become_method", "become_user", "remote_user", "verbosity", "check",
        "tags", "diff", "basedir"
    ])

    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection="ssh",
                      module_path=None,
                      forks=100,
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=None,
                      become_method="sudo",
                      become_user="******",
                      remote_user=None,
                      verbosity=2,
                      check=False,
                      tags=tags,
                      diff=None,
                      basedir=basedir)

    return inventory, variable_manager, loader, options