Esempio n. 1
0
 def __init__(self, **kwargs):
     # Get the specified AT instance
     logger.debug(f"AnsibleTower instantiated with {kwargs=}")
     instance_name = kwargs.pop("AnsibleTower", None)
     # Validate the AnsibleTower-specific settings
     self._validate_settings(instance_name)
     # get our instance settings
     self.url = settings.ANSIBLETOWER.base_url
     self.uname = settings.ANSIBLETOWER.get("username")
     self.pword = settings.ANSIBLETOWER.get("password")
     self.token = settings.ANSIBLETOWER.get("token")
     self._inventory = (
         kwargs.get("tower_inventory") or settings.ANSIBLETOWER.inventory
     )
     # Init the class itself
     self._construct_params = []
     config = kwargs.get("config", awxkit.config)
     config.base_url = self.url
     root = kwargs.get("root")
     if root is None:
         root = awxkit.api.Api()  # support mock stub for unit tests
     # Prefer token if its set, otherwise use username/password
     # auth paths for the API taken from:
     # https://github.com/ansible/awx/blob/ddb6c5d0cce60779be279b702a15a2fddfcd0724/awxkit/awxkit/cli/client.py#L85-L94
     # unit test mock structure means the root API instance can't be loaded on the same line
     if self.token:
         helpers.emit(auth_type="token")
         logger.info("Using token authentication")
         config.token = self.token
         try:
             root.connection.login(
                 username=None, password=None, token=self.token, auth_type="Bearer"
             )
         except awxkit.exceptions.Unauthorized as err:
             raise exceptions.AuthenticationError(err.args[0])
         versions = root.get().available_versions
         try:
             # lookup the user that authenticated with the token
             # If a username was specified in config, use that instead
             my_username = (
                 self.uname or versions.v2.get().me.get().results[0].username
             )
         except (IndexError, AttributeError):
             # lookup failed for whatever reason
             raise exceptions.ProviderError(
                 provider="AnsibleTower",
                 message="Failed to lookup a username for the given token, please check credentials",
             )
     else:  # dynaconf validators should have checked that either token or password was provided
         helpers.emit(auth_type="password")
         logger.info("Using username and password authentication")
         config.credentials = {
             "default": {"username": self.uname, "password": self.pword}
         }
         config.use_sessions = True
         root.load_session().get()
         versions = root.available_versions
         my_username = self.uname
     self.v2 = versions.v2.get()
     self.username = my_username
Esempio n. 2
0
 :return: dictionary containing all information about executed workflow/job template
 """
 if (name := kwargs.get("workflow")):
     subject = "workflow"
     get_path = self.v2.workflow_job_templates
 elif (name := kwargs.get("job_template")):
     subject = "job_template"
     get_path = self.v2.job_templates
 else:
     raise exceptions.ProviderError(
         provider="AnsibleTower",
         message="No workflow or job template specified")
 try:
     candidates = get_path.get(name=name).results
 except awxkit.exceptions.Unauthorized as err:
     raise exceptions.AuthenticationError(err.args[0])
 if candidates:
     target = candidates.pop()
 else:
     raise exceptions.ProviderError(
         provider="AnsibleTower",
         message=f"{subject.capitalize()} not found by name: {name}",
     )
 payload = {"extra_vars": str(kwargs)}
 if self.inventory:
     payload["inventory"] = self.inventory
 else:
     logger.info(
         "No inventory specified, Ansible Tower will use a default.")
 logger.debug(
     f"Launching {subject}: {url_parser.urljoin(self.url, str(target.url))}\n"