Skip to content

Instantly share code, notes, and snippets.

@digitalist
Last active April 15, 2019 17:42
Show Gist options
  • Save digitalist/55ae03f581b649b82ae83d54c1aa7bfc to your computer and use it in GitHub Desktop.
Save digitalist/55ae03f581b649b82ae83d54c1aa7bfc to your computer and use it in GitHub Desktop.
dnsmasq local/server setup ansible playbook
#sudo systemctl stop dnsmasq.service
#sudo apt remove -y --purge dnsmasq
#sudo rm -rf /etc/dnsmasq.*
#sudo systemctl start systemd-resolved.service
#ansible-playbook --extra-vars @/home/user/bash/ansible-vault-test.yml tasks/software_setup/dnsmasq.yml
#sudo systemctl stop systemd-resolved.service
# sudo systemctl start dnsmasq.service
---
- hosts: "{{ rhosts | default('localhost') }}"
#@readme_ru: Установка серверного dnsmasq.
#@readme_full_ru: Установка серверного dnsmasq.
#@readme_full_ru: Если есть файл network_dns.ini - подключает сервера вместо дефолтных гугловых
#@readme_full_ru: Если есть файл network_hosts.ini - добавляет файл /etc/dnsmasq.our.hosts, как локальную базу dns
#@readme_en: Install dnsmasq server.
#@readme_full_en: Install dnsmasq server.
#@readme_full_en: If we have local network_dns.ini - they will be used instead of default google dns
#@readme_full_en: If we have local network_hosts.ini - will add /etc/dnsmasq.our.hosts, as an additional hosts database
tasks:
- name: Install a list of packages
apt:
name: "{{ packages }}"
update_cache: no
vars:
packages:
dnsmasq
- name: Check if we provide our own dns servers
local_action: "stat path={{ inventory_dir }}/../network_dns.ini"
register: private_dns_config
- name: Check if we provide additional hosts file
local_action: "stat path={{ inventory_dir }}/../network_hosts.ini"
register: additional_host_config
# - name: dbg private_dns_config
# debug:
# var: private_dns_config
#
# - name: dbg me
# debug:
# msg: "stat path={{ playbook_dir }}/network_dns.ini"
- name: "/etc/dnsmasq.resolv setup our nameservers"
blockinfile:
path: /etc/dnsmasq.resolv
block: "{{ lookup('file', private_dns_config.stat.path) }}"
create: yes
when:
- private_dns_config.stat.exists == true
- private_dns_config.stat.size > 0
#
# - name: end
# meta: end_play
- name: "/etc/dnsmasq.resolv setup default nameservers"
blockinfile:
path: /etc/dnsmasq.resolv
block: |
nameserver 127.0.0.53
nameserver 8.8.8.8
nameserver 8.8.4.4
create: yes
when:
- inventory_hostname == "localhost" or private_dns_config.stat.exists == false
- name: "/etc/dnsmasq.resolv setup our nameservers"
blockinfile:
path: /etc/dnsmasq.our.hosts.ini
block: "{{ lookup('file', additional_host_config.stat.path) }}"
create: yes
when:
- additional_host_config.stat.exists == true
- additional_host_config.stat.size > 0
- name: "listen to lo!"
blockinfile:
path: /etc/dnsmasq.conf
block: |
cache-size=15000
dns-forward-max=500
resolv-file=/etc/dnsmasq.resolv
addn-hosts=/etc/dnsmasq.our.hosts.ini
domain-needed
bogus-priv
create: yes
- name: set as local client dns when
lineinfile:
path: /etc/dnsmasq.conf
line: listen-address=127.0.0.1
when:
- inventory_hostname == "localhost"
# rhosts == localhost
- name: fix systemd (you can't fix it! ;-)
when: ansible_service_mgr == "systemd"
systemd:
state: stopped
name: systemd-resolved.service
enabled: no
- name: start dnsmasq
when: ansible_service_mgr == "systemd"
systemd:
state: started
name: dnsmasq.service
enabled: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment