---
- name: "create server id"
shell: perl -e 'print (time()+int(rand(20)))'
register: server_id
with_items:
- "{{ groups['bind-dns'] }}"
when: inventory_hostname in groups['bind-dns']
- set_fact:
server_id: "{{ server_id['results'][0]['stdout']}}"
when: inventory_hostname in groups['bind-dns']
- name: define paramiters for dns servers
set_fact:
server_id: "{{ server_id }}"
prins: "ns1.{{ ns_domain }}"
secns: "{{ secns|default([]) + ['ns' + (ansible_loop.index+1)|string + '.' + ns_domain] }}"
old_defip: "{{ inventory_hostname }}"
with_items:
- "{{ groups['bind-dns'] }}"
loop_control:
extended: yes
when: inventory_hostname in groups['bind-dns']
- name: "add hosts into /etc/hosts for bind servers"
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}.*$'
line: "{{ hostvars[item].inventory_hostname }} {{ hostvars[item].ansible_fqdn }} ns{{ ansible_loop.index }}.{{ ns_domain }}"
state: present
with_items:
- "{{ groups['bind-dns'] }}"
loop_control:
extended: yes
when: inventory_hostname in groups['bind-dns']
- name: "login webmin to create a cookie"
uri:
url: http://127.0.0.1:20000/session_login.cgi
method: POST
body_format: form-urlencoded
status_code: [301,302]
body:
user: admin
pass: admin123
enter: Sign in
return_content: yes
headers:
Cookie: "testing=1"
register: login
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
- name: config virtual-server
template:
src: virtual-server-config.j2
dest: /etc/webmin/virtual-server/config
mode: '0711'
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
- name: check and reload Virtualmin
uri:
url: http://127.0.0.1:20000/virtual-server/check.cgi?
validate_certs: no
method: GET
#return_content: yes
headers:
Cookie: "{{ login.set_cookie }}"
status_code: [200]
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
# - name: "config virtual-server step by step"
# shell:
# cmd: |
# url='http://127.0.0.1:20000/virtual-server/wizard.cgi'
# curl -H "Cookie: {{ login.set_cookie }}" $url?step=1&preload=0&parse=Next
# curl -H "Cookie: {{ login.set_cookie }}" $url?step=2&mysql=0&postgres=0&parse=Next
# curl -H "Cookie: {{ login.set_cookie }}" $url?step=3&prins={{ prins }}.&secns={{ secns }}.&prins_skip=1&parse=Next
# curl -H "Cookie: {{ login.set_cookie }}" $url?step=4&hashpass=0&parse=Next
# curl -H "Cookie: {{ login.set_cookie }}" $url?step=5&parse=Next
# run_once: true
# delegate_to: "{{ groups['bind-dns'][0] }}"
# register: curl
# - name: config wizard
# uri:
# url: http://127.0.0.1:20000/virtual-server/wizard.cgi
# validate_certs: no
# method: POST
# #return_content: yes
# headers:
# Cookie: "{{ login.set_cookie }}"
# status_code: [200,301,302]
# body_format: form-urlencoded
# body:
# mysql: 0
# postgres: 0
# preload: 0
# lookup: 0
# prins: "{{ prins }}"
# prins_skip: 1
# secns: "{{ secns }}"
# hashpass: 0
# run_once: true
# delegate_to: "{{ groups['bind-dns'][0] }}"
# - name: modify virtual server plugin
# uri:
# url: http://127.0.0.1:20000/virtual-server/save_newfeatures.cgi
# validate_certs: no
# method: POST
# #return_content: yes
# headers:
# Cookie: "{{ login.set_cookie }}"
# status_code: [200,301,302]
# body_format: form-urlencoded
# body:
# fmods: dns
# factive: dns
# save: '%E4%BF%9D%E5%AD%98'
# run_once: true
# delegate_to: "{{ groups['bind-dns'][0] }}"
# create server index
- name: "add servers to webmin cluster"
shell:
cmd: |
cat > /etc/webmin/servers/{{ hostvars[item].server_id }}.serv << EOF
pass=admin123
ssl=0
checkssl=
port=20000
host={{ item }}
group=
desc=
user=admin
id={{ hostvars[item].server_id }}
type=centos
fast=1
EOF
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
with_items:
- "{{ groups['bind-dns'][1:] }}"
- name: add dns slave servers to dns master server
uri:
url: http://127.0.0.1:20000/bind8/slave_add.cgi
validate_certs: no
method: POST
#return_content: yes
headers:
Cookie: "{{ login.set_cookie }}"
status_code: [200,301,302]
body_format: form-urlencoded
body:
server: '{{ hostvars[item].server_id }}'
view_def: 1
view: ''
sec: 1
sync: 1
name_def: 0
name: "{{ secns[ansible_loop.index0] }}"
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
with_items:
- "{{ groups['bind-dns'][1:] }}"
loop_control:
extended: yes
- name: "Setting the Master IP Address"
lineinfile:
dest: /etc/webmin/bind8/config
regexp: '^this_ip.*$'
line: "this_ip={{ groups['bind-dns'][0] }}"
state: present
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
- name: Basic Setup of DNS server
uri:
url: http://127.0.0.1:20000/bind8/save_zonedef.cgi
validate_certs: no
method: POST
#return_content: yes
headers:
Cookie: "{{ login.set_cookie }}"
status_code: [200,301,302]
body_format: form-urlencoded
body:
refresh: 10800
refunit: ''
retry: 3600
retunit: ''
expiry: 604800
expunit:
minimum: 38400
minunit:
name_0:
type_0: A
value_0_def: 1
name_1:
type_1: A
value_1_def: 1
include_def: 1
email: 'iuap_admin@yonyou.com'
prins_def: 0
prins: '{{ prins }}'
dnssec: 0
alg: RSASHA1
size_def: 1
size:
single: 0
allow-transfer_def: 0
allow-transfer: acl1
allow-query_def: 0
allow-query: any
also-notify_def: 1
also-notify:
master: ignore
slave:
response:
notify:
run_once: true
delegate_to: "{{ groups['bind-dns'][0] }}"
- name: "add new user bind for manage dns"
lineinfile:
dest: /etc/webmin/miniserv.users
line: "bind:$1$73641827$.K742ybaJY33hg1vQQlQL/::::::::0::::"
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"
- name: "add user bind acl for manage dns"
lineinfile:
dest: /etc/webmin/webmin.acl
line: "bind: bind8"
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"
- name: "create bind.acl file"
file:
path: "/etc/webmin/bind.acl"
state: touch
mode: '0611'
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"
- name: "add bind acl for user"
shell:
cmd: |
cat > /etc/webmin/bind.acl << 'EOF'
rpc=2
nodot=0
webminsearch=1
uedit_mode=0
gedit_mode=0
feedback=2
otherdirs=
readonly=0
fileunix=root
uedit=
negative=0
root=/
uedit2=
gedit=
gedit2=
EOF
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"
- name: "set language to zh_CN for user bind"
lineinfile:
dest: /etc/webmin/config
line: 'lang_bind=zh_CN.UTF-8'
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"
# add cron job for backup configure file
- name: define parameter for backup.pl
set_fact:
script_dir: "backup-config"
script: "backup.pl"
- name: create backup script
template:
src: run_perl.j2
dest: /etc/webmin/{{ script_dir }}/{{ script }}
mode: '0755'
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"
- name: "create cron jobs"
shell:
cmd: |
cron_job_id=$(perl -e 'print time().$$')
job_dir=/etc/webmin/{{ script_dir }}/backups
backup_dir=/data/backup/bind
mkdir -p $job_dir $backup_dir
cat > $job_dir/${cron_job_id}.backup << EOF
configfile=
dest=/data/backup/bind/bind_config
others=
post=conf="$backup_dir/bind_config"; conf_time=\${conf}_\$(date +%F-%H-%M); [[ -f "\$conf" ]] && echo "move \$conf to \$conf_time" && mv \$conf \$conf_time
pre=
email=
mins=0
mods=bind8
sched=1
id=$cron_job_id
hours=0
days=*
nofiles=
emode=0
weekdays=*
months=*
EOF
grep -q "${cron_job_id}" /var/spool/cron/root || echo "0 0 * * * /etc/webmin/backup-config/backup.pl ${cron_job_id}" >> /var/spool/cron/root
run_once: true
delegate_to: "{{item}}"
loop: "{{ groups['bind-dns'] }}"