def write_role_vars(self, ansible_dir, role, document, vars_file_name='main.yml'): vars_dir = os.path.join(ansible_dir, 'roles', to_role_name(role), 'vars') if not os.path.exists(vars_dir): os.makedirs(vars_dir) vars_file_path = os.path.join(vars_dir, vars_file_name) with open(vars_file_path, 'w') as stream: dump(document, stream) if vars_file_name == 'main.yml': self.roles_with_generated_vars.append(to_role_name(role))
def get_inventory(self): inventory = [] for component_key, component_value in self.cluster_model.specification.components.items( ): if component_value.count < 1: continue ips = self.proxy.get_ips_for_feature(component_key) if len(ips) > 0: roles = self.get_roles_for_feature(component_key) for role in roles: ansible_role_name = to_role_name(role) inventory.append( AnsibleInventoryItem(to_role_name(ansible_role_name), ips)) return self.group_duplicated(inventory)
def apply(self): inventory_path = get_inventory_path( self.cluster_model.specification.name) # copy resources self.copy_resources() # create inventory inventory_creator = AnsibleInventoryCreator(self.cluster_model, self.config_docs) inventory_creator.create() time.sleep(10) # generate vars ansible_vars_generator = AnsibleVarsGenerator( inventory_creator=inventory_creator) ansible_vars_generator.generate() # pre-flight to prepare machines self.pre_flight(inventory_path) # run roles enabled_roles = inventory_creator.get_enabled_roles() for role in enabled_roles: self.ansible_command.run_playbook(inventory=inventory_path, playbook_path=self.playbook_path( to_role_name(role))) #post-flight after we are done self.post_flight(inventory_path)
def run(self): enabled_roles = self.inventory_creator.get_enabled_roles() ansible_dir = get_ansible_path(self.cluster_model.specification.name) cluster_config_file_path = os.path.join(ansible_dir, 'roles', 'common', 'vars', 'main.yml') clean_cluster_model = self.get_clean_cluster_model() self.populate_group_vars(ansible_dir) with open(cluster_config_file_path, 'w') as stream: dump(clean_cluster_model, stream) for role in enabled_roles: document = select_first( self.config_docs, lambda x: x.kind == 'configuration/' + to_feature_name(role)) if document is None: self.logger.warn('No config document for enabled role: ' + role) continue document = self.add_provider_info(document) vars_dir = os.path.join(ansible_dir, 'roles', to_role_name(role), 'vars') if not os.path.exists(vars_dir): os.makedirs(vars_dir) vars_file_name = 'main.yml' vars_file_path = os.path.join(vars_dir, vars_file_name) with open(vars_file_path, 'w') as stream: dump(document, stream)
def run(self): inventory_path = get_inventory_path( self.cluster_model.specification.name) # create inventory on every run self.inventory_creator.create() time.sleep(10) copy_files_recursively( AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH, get_ansible_path(self.cluster_model.specification.name)) # todo: install packages to run ansible on Red Hat hosts self.ansible_command.run_task_with_retries( hosts="all", inventory=inventory_path, module="raw", args="cat /etc/lsb-release | grep -i DISTRIB_ID | grep -i ubuntu && " "sudo apt-get install -y python-simplejson " "|| echo 'Cannot find information about Ubuntu distribution'", retries=5) self.ansible_vars_generator.run() common_play_result = self.ansible_command.run_playbook_with_retries( inventory=inventory_path, playbook_path=os.path.join( get_ansible_path(self.cluster_model.specification.name), "common.yml"), retries=5) if common_play_result != 0: return enabled_roles = self.inventory_creator.get_enabled_roles() for role in enabled_roles: play_result = self.ansible_command.run_playbook_with_retries( inventory=inventory_path, playbook_path=os.path.join( get_ansible_path(self.cluster_model.specification.name), to_role_name(role) + ".yml"), retries=1) if play_result != 0: break
def test_to_role_name(): actual = to_role_name("infrastructure/route-table-association") assert actual == "infrastructure/route_table_association"
def test_replaces_all_hyphens_in_variable(): actual = to_role_name("infrastructure/route-table-association") assert actual == "infrastructure/route_table_association"