Move services.json into ansible
This commit is contained in:
		
							parent
							
								
									6548019090
								
							
						
					
					
						commit
						3fed27b42f
					
				
					 11 changed files with 188 additions and 153 deletions
				
			
		
							
								
								
									
										11
									
								
								playbooks/create_services_for_dnscontrol.yml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								playbooks/create_services_for_dnscontrol.yml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
---
 | 
			
		||||
- name: Create services for dnscontrol
 | 
			
		||||
  hosts: localhost
 | 
			
		||||
  tasks:
 | 
			
		||||
    - name: Create the services json file
 | 
			
		||||
      ansible.builtin.template:
 | 
			
		||||
        src: "json.j2"
 | 
			
		||||
        dest: "{{ services_json_file }}"
 | 
			
		||||
        mode: "0644"
 | 
			
		||||
      vars:
 | 
			
		||||
        json: "{{ all_services | services_to_dnscontrol() }}"
 | 
			
		||||
| 
						 | 
				
			
			@ -1,17 +0,0 @@
 | 
			
		|||
class FilterModule(object):
 | 
			
		||||
    def filters(self):
 | 
			
		||||
        return {
 | 
			
		||||
            'my_service_attributes': self.my_service_attributes,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def my_service_attributes(self, services, my_services, attribute="name"):
 | 
			
		||||
        result = []
 | 
			
		||||
        for service in services:
 | 
			
		||||
            if service["name"] in my_services:
 | 
			
		||||
                if attribute in service:
 | 
			
		||||
                    if type(service[attribute]) == list:
 | 
			
		||||
                        result.extend(service[attribute])
 | 
			
		||||
                    else:
 | 
			
		||||
                        result.append(service[attribute])
 | 
			
		||||
 | 
			
		||||
        return result
 | 
			
		||||
							
								
								
									
										38
									
								
								playbooks/filter_plugins/service_filters.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								playbooks/filter_plugins/service_filters.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,38 @@
 | 
			
		|||
class FilterModule(object):
 | 
			
		||||
    def filters(self):
 | 
			
		||||
        return {
 | 
			
		||||
            'my_service_attributes': self.my_service_attributes,
 | 
			
		||||
            'services_to_dnscontrol': self.services_to_dnscontrol,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def my_service_attributes(self, services, host, attribute="name"):
 | 
			
		||||
        result = []
 | 
			
		||||
        for service in services:
 | 
			
		||||
            if service["host"] != host:
 | 
			
		||||
                continue
 | 
			
		||||
            if not attribute in service:
 | 
			
		||||
                continue
 | 
			
		||||
 | 
			
		||||
            if type(service[attribute]) == list:
 | 
			
		||||
                result.extend(service[attribute])
 | 
			
		||||
            else:
 | 
			
		||||
                result.append(service[attribute])
 | 
			
		||||
 | 
			
		||||
        return result
 | 
			
		||||
 | 
			
		||||
    def services_to_dnscontrol(self, services):
 | 
			
		||||
        result = {}
 | 
			
		||||
        for service in services:
 | 
			
		||||
            for dns in service.get("dns", []):
 | 
			
		||||
                name = dns.get("name", service["name"])
 | 
			
		||||
                result[name] = {
 | 
			
		||||
                    "target": dns["target"],
 | 
			
		||||
                    "domain": dns["domain"],
 | 
			
		||||
                }
 | 
			
		||||
               
 | 
			
		||||
                if "alias" in dns:
 | 
			
		||||
                    result[name]["alias"] = dns["alias"]
 | 
			
		||||
                else:
 | 
			
		||||
                    result[name]["host"] = service["host"]
 | 
			
		||||
 | 
			
		||||
        return result
 | 
			
		||||
| 
						 | 
				
			
			@ -23,6 +23,6 @@
 | 
			
		|||
        apply:
 | 
			
		||||
          tags: "{{ services_item }}"
 | 
			
		||||
      tags: always
 | 
			
		||||
      loop: "{{ all_services | my_service_attributes(host_services) }}"
 | 
			
		||||
      loop: "{{ all_services | my_service_attributes(inventory_hostname) }}"
 | 
			
		||||
      loop_control:
 | 
			
		||||
        loop_var: services_item
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue