uprava popisu, a zjednodušení Inventory

This commit is contained in:
2024-11-22 01:05:46 +01:00
parent 67441569ae
commit 4169e31d2d
3 changed files with 21 additions and 23 deletions

View File

@@ -1,40 +1,40 @@
- name: Detekce zařízení v síti - name: Detekce zarizeni v siti
hosts: localhost hosts: localhost
vars: vars:
subnets: ["10.11.99", "10.22.99"] subnets: ["10.11.99", "10.22.99"]
gather_facts: no gather_facts: no
tasks: tasks:
- name: Získání seznamu zařízení v rozsahu DHCP serveru - name: Ziskani seznamu zarizeni v rozsahu DHCP serveru
ansible.builtin.shell: | ansible.builtin.shell: |
for i in $(seq 100 254); do for i in $(seq 100 254); do
ping -W .1 -c 1 {{ item }}.$i > /dev/null 2>&1 && echo {{ item }}.$i; ping -W .1 -c 1 {{ item }}.$i > /dev/null 2>&1 && echo {{ item }}.$i;
done || true done || true
register: new_ips register: new_ips
loop: "{{ subnets }}" loop: "{{ subnets }}"
- name: Přidat aktivní IP do dynamického inventáře - name: Pridani aktivni IP do dynamickeho inventare
ansible.builtin.add_host: ansible.builtin.add_host:
name: "{{ item }}" name: "{{ item }}"
groups: found_devices groups: found_devices
loop: "{{ new_ips.results | map(attribute='stdout_lines') | flatten }}" loop: "{{ new_ips.results | map(attribute='stdout_lines') | flatten }}"
- name: Přidat skupinu found_devices do metaskupiny openwrt - name: Pridani skupiny found_devices do metaskupiny openwrt
ansible.builtin.add_host: ansible.builtin.add_host:
groups: openwrt groups: openwrt
name: "{{ item }}" name: "{{ item }}"
loop: "{{ groups['found_devices'] }}" loop: "{{ groups['found_devices'] }}"
- name: Získání seznamu volných adres mimo DHCP server - name: Ziskáni seznamu volnych adres mimo DHCP server
ansible.builtin.shell: | ansible.builtin.shell: |
for i in $(seq 1 99); do for i in $(seq 1 99); do
ping -W .1 -c 1 {{ item }}.$i > /dev/null 2>&1 || echo {{ item }}.$i; ping -W .1 -c 1 {{ item }}.$i > /dev/null 2>&1 || echo {{ item }}.$i;
done done
register: free_ips register: free_ips
loop: "{{ subnets }}" loop: "{{ subnets }}"
- name: Vytvoření seznamu volných IP adres podle subnetu - name: Vytvoreni seznamu volných IP adres podle subnetu
set_fact: set_fact:
free_subnet_ips: "{{ free_subnet_ips | default({}) | combine({item.item: item.stdout_lines}) }}" free_subnet_ips: "{{ free_subnet_ips | default({}) | combine({item.item: item.stdout_lines}) }}"
loop: "{{ free_ips.results }}" loop: "{{ free_ips.results }}"
- name: Získání hostů a generování hostnamů - name: Ziskani hostu a generovani hostnamu
hosts: found_devices hosts: found_devices
roles: roles:
- gekmihesg.openwrt - gekmihesg.openwrt
@@ -43,42 +43,42 @@
ansible_scp_extra_args: "-O" ansible_scp_extra_args: "-O"
openwrt_install_recommended_packages: false openwrt_install_recommended_packages: false
tasks: tasks:
- name: Získání unikátního názvu zařízení podle MAC adresy - name: Ziskani unikatniho nazvu zarizeni podle MAC adresy
set_fact: set_fact:
hostname: "ap_{{ ansible_facts.openwrt_devices[ansible_facts.openwrt_interfaces.mgmnt.l3_device].macaddr.replace(':', '') }}" hostname: "ap_{{ ansible_facts.openwrt_devices[ansible_facts.openwrt_interfaces.mgmnt.l3_device].macaddr.replace(':', '') }}"
- name: Uložení ip adresy hosta a hosnamu do seznamu hostů - name: Ulozeni ip adresy hosta a hosnamu do seznamu hostu
set_fact: set_fact:
host_info: { "ip": "{{ ansible_host }}", "hostname": "{{ hostname }}" } host_info: { "ip": "{{ ansible_host }}", "hostname": "{{ hostname }}" }
- name: Shromáždění informací o všech hostech a předání na localhost - name: Shromazdeni informaci o vsech hostech a predani na localhost
delegate_to: localhost delegate_to: localhost
delegate_facts: true delegate_facts: true
run_once: true run_once: true
set_fact: set_fact:
found_hosts_info: "{{ ansible_play_hosts | map('extract', hostvars, 'host_info') | list }}" found_hosts_info: "{{ ansible_play_hosts | map('extract', hostvars, 'host_info') | list }}"
- name: Přidání nového access poitu do správy - name: Pridani nového access poitu do spravy
hosts: localhost hosts: localhost
gather_facts: no gather_facts: no
tasks: tasks:
- name: Přidělení statické IP adresy a vytvoření souboru v host_vars - name: Prideleni staticke IP adresy a vytvoreni souboru v host_vars
include_tasks: include/createHostVars.yml include_tasks: include/createHostVars.yml
loop: "{{ found_hosts_info }}" loop: "{{ found_hosts_info }}"
- meta: refresh_inventory - meta: refresh_inventory
- name: Přidat nalezených hostů do dynamického inventáře - name: Pridani nalezenych hostu do dynamickeho inventare
ansible.builtin.add_host: ansible.builtin.add_host:
name: '{{ item.hostname }}' name: '{{ item.hostname }}'
ansible_host: '{{ item.ip }}' ansible_host: '{{ item.ip }}'
groups: new_devices groups: new_devices
loop: "{{ found_hosts_info }}" loop: "{{ found_hosts_info }}"
- name: Přidat skupinu new_devices do metaskupiny openwrt - name: Pridani skupiny new_devices do metaskupiny openwrt
ansible.builtin.add_host: ansible.builtin.add_host:
groups: openwrt groups: openwrt
name: "{{ item }}" name: "{{ item }}"
loop: "{{ groups['new_devices'] }}" loop: "{{ groups['new_devices'] }}"
- name: Nastavení statické ip na mgmnt interface - name: Nastaveni staticke ip na mgmnt interface
hosts: new_devices hosts: new_devices
roles: roles:
- gekmihesg.openwrt - gekmihesg.openwrt

View File

@@ -9,15 +9,15 @@
stat: stat:
path: host_vars/{{item.hostname}}.yml path: host_vars/{{item.hostname}}.yml
register: st register: st
- name: Vytvoření souboru v host_vars - name: Vytvoreni souboru v host_vars
template: template:
src: templates/host_vars.j2 src: templates/host_vars.j2
dest: host_vars/{{item.hostname}}.yml dest: host_vars/{{item.hostname}}.yml
when: not st.stat.exists when: not st.stat.exists
- name: Aktualizovat seznam volných IP adres po přidělení - name: Aktualizovat seznam volnych IP adres po prideleni
set_fact: set_fact:
free_subnet_ips: "{{ free_subnet_ips | combine({ip_subnet: free_subnet_ips[ip_subnet][1:]}) }}" free_subnet_ips: "{{ free_subnet_ips | combine({ip_subnet: free_subnet_ips[ip_subnet][1:]}) }}"
- name: Přidat blok s novým přístupovým bodem na konec souboru - name: Pridat blok s novym pristupovym bodem na konec souboru
lineinfile: lineinfile:
path: inventory.yml # Cesta k souboru path: inventory.yml # Cesta k souboru
line: |2 line: |2

View File

@@ -10,13 +10,11 @@ accessPoints:
hosts: hosts:
ap_107c61992bd8: ap_107c61992bd8:
ansible_host: "10.11.99.2" ansible_host: "10.11.99.2"
ap_c47154393f26:
ansible_host: "10.22.99.2"
swconfig: swconfig:
hosts: hosts:
ap_b04e26bbc7e3: ap_b04e26bbc7e3:
ansible_host: "10.11.99.3" ansible_host: "10.11.99.3"
ap_0c806307e88a: ap_0c806307e88a:
ansible_host: "10.11.99.4" ansible_host: "10.11.99.4"
ap_c47154393f26:
ansible_host: "10.11.99.5"
ap_c47154391634:
ansible_host: "10.11.99.6"