def _get_playbook_cmd(self, inventory_path): flag = '' # verbose levels: 1=not verbose, 2=more verbose if self.verbose_level > 1: flag = '-' for x in range(1, self.verbose_level): flag += 'v' ansible_cmd = get_ansible_command(playbook=True) cmd = '%s %s' % (ansible_cmd, flag) cmd += ' -i %s' % inventory_path if self.include_passwords: cmd += ' %s' % self._get_password_path() cmd += ' %s' % self.playbook_path if self.extra_vars or self.serial: extra_vars = '' if self.extra_vars: extra_vars = self.extra_vars if self.serial: extra_vars += ' ' if self.serial: extra_vars += 'serial_var=1' cmd += ' --extra-vars \"%s\"' % extra_vars if self.services: service_string = '' first = True for service in self.services: if not first: service_string += ',' else: first = False service_string = service_string + service cmd += ' --tags %s' % service_string if self.hosts: host_string = '' first = True for host in self.hosts: if not first: host_string += ',' else: first = False host_string = host_string + host cmd += ' --limit %s' % host_string if self.flush_cache: cmd += ' --flush-cache' if self.become_user: cmd += ' --become-user %s' % self.become_user return cmd
def run_ansible_command(self, ansible_command, hostname): output = None command_string = '%s -vvv' % \ get_ansible_command() gen_file_path = self.create_json_gen_file() cmd = '%s %s -i %s %s' % (command_string, hostname, gen_file_path, ansible_command) try: err_msg, output = run_cmd(cmd, False) except Exception as e: err_msg = str(e) finally: self.remove_json_gen_file(gen_file_path) return err_msg, output