def run(self, resume=1): """Execute ansible-playbook using information gathered from config. Args: resume (int): Used as list index - 1 from which to resume workflow. """ # list index to start working on (for support of --resume) try: i = int(resume) - 1 except ValueError: # generally if passed a non-int i = 0 cmds = self._config.playbook_cmds kwargs = { '_out': self._print_stdout, '_err': self._print_stderr, '_env': self._config.env } for counter, cmd in enumerate(cmds): # skip execution until we reach our --resume index # using a list slice doesn't work here since we need to be aware of # the full list to produce a resume index on failure if counter < i: continue try: sh.ansible_playbook(*cmd, **kwargs) except (sh.ErrorReturnCode, sh.ErrorReturnCode_1): msg = ('An error was encountered during playbook execution. ' 'Please resolve manually and then use the following ' 'command to resume execution of this script:\n\n') cmd = self._construct_resume_cli(counter + 1) print(colorama.Fore.RED + msg + cmd) sys.exit(1)
def run_playbook(*args, **kwargs): playbook = "_propelc/playbook.yml" with sh.pushd(CWD): sh.ansible_playbook(playbook, "-i", "_propelc/hosts", _out=sh_verbose, *args, **kwargs)
def low_ttl_varnish(request): """ Lowers the Varnish object TTL so that we can make assertions about expired cache objects. """ def finished(): ansible_playbook("deploy-blog.yml", tags="varnish") ansible_playbook("deploy-blog.yml", tags="varnish", extra_vars="wordpress_varnish_ttl='3s'") request.addfinalizer(finished)
def run_playbook(self, playbook_file_name, tag_list=None, local=None): """Run ansible playbook""" if not tag_list: tag_list = [] log_info( "Running ansible playbook {0!r}...".format(playbook_file_name)) # Prepare env cwd = os.getcwd() os.chdir(self.ansible_dir) os.environ['ANSIBLE_HOST_KEY_CHECKING'] = 'False' os.environ['ANSIBLE_RETRY_FILES_ENABLED'] = 'False' os.environ['ANSIBLE_SSH_RETRIES'] = '5' ansible_parameters = [ "-i", "localhost,", "-c", "local", "-u", os.getenv('USER', ''), "--extra-vars", "env_fabric_name=" + self.fabric, "--extra-vars", "env_customer_company_name=" + self.customer_company_name, "--extra-vars", "env_hosted_zone" + self.hosted_zone, playbook_file_name ] # Check if not local execution if local is None: ansible_parameters = [ "-i", "ansible_inventory.sh", playbook_file_name ] # Add tags if provided if tag_list: tags = "" ansible_parameters.append("--tags") for tag in tag_list: tags += tag + "," ansible_parameters.append(tags) # Run ansible playbook try: cmd = sh.ansible_playbook(ansible_parameters, _out=self.ansible_process_output, _bg=True) cmd.wait() except sh.ErrorReturnCode as err: log_info(err.full_cmd) log_info('Command output:' + err.stdout.decode('UTF-8').rstrip()) log_error(err.stderr.decode('UTF-8').rstrip(), nl=False) log_error( "Unexpected ansible playbook error (status code {0!r})".format( err.exit_code)) if os.path.exists(cwd): os.chdir(cwd) return False, err.exit_code if os.path.exists(cwd): os.chdir(cwd) return True, 0
def converge(self, idempotent=False, create_instances=True, create_inventory=True): """ Provisions all instances using ansible-playbook. :param idempotent: Optionally provision servers quietly so output can be parsed for idempotence :param create_inventory: Toggle inventory creation :param create_instances: Toggle instance creation :return: Provisioning output if idempotent=True, otherwise return code of underlying call to ansible-playbook """ # support fast converging if self.molecule._args['--fast']: create_instances = False create_inventory = False if create_instances and not idempotent: self.create() if create_inventory: self.molecule._create_inventory_file() playbook, args, kwargs = self.molecule._create_playbook_args() if idempotent: kwargs.pop('_out', None) kwargs.pop('_err', None) kwargs['_env']['ANSIBLE_NOCOLOR'] = 'true' kwargs['_env']['ANSIBLE_FORCE_COLOR'] = 'false' try: output = sh.ansible_playbook(playbook, *args, **kwargs) return output except sh.ErrorReturnCode as e: print('ERROR: {}'.format(e)) sys.exit(e.exit_code) try: output = sh.ansible_playbook(playbook, *args, **kwargs) return output.exit_code except sh.ErrorReturnCode as e: print('ERROR: {}'.format(e)) sys.exit(e.exit_code)
def converge(self, idempotent=False): if not idempotent: self.create() self._create_inventory_file() playbook, args, kwargs = self._create_playbook_args() if idempotent: kwargs.pop('_out', None) kwargs.pop('_err', None) kwargs['_env']['ANSIBLE_NOCOLOR'] = 'true' kwargs['_env']['ANSIBLE_FORCE_COLOR'] = 'false' try: output = sh.ansible_playbook(playbook, *args, **kwargs) return output except sh.ErrorReturnCode as e: print("ERROR: {0}".format(e)) sys.exit(e.exit_code) try: output = sh.ansible_playbook(playbook, *args, **kwargs) return output.exit_code except sh.ErrorReturnCode as e: print("ERROR: {0}".format(e)) sys.exit(e.exit_code)
def finished(): ansible_playbook("deploy-blog.yml", tags="varnish")
help='node to run playbook on') parser.add_argument('playbook_path', metavar='f', type=str, nargs='?', help='path for the ansible playbook') (args, unknown) = parser.parse_known_args() print(args.node, args.playbook_path) # for now if not multimachine, ignore node parameter v = vagrant.Vagrant() if len(v.status()) > 1: multimachine = True private_key = v.keyfile(args.node) user = v.user(args.node) else: multimachine = False private_key = v.keyfile() user = v.user() params = [ "=".join(p) for p in (("--private-key", private_key), ("--user", user), ("--inventory-file", ".vagrant/provisioners/ansible/inventory")) ] + unknown + [args.playbook_path] for line in sh.ansible_playbook(*params, _cwd=".", _iter=True): print(line, end="")