25-7-28.note

一、配置Ansible:
1、生成密钥对,仅允许密钥对认证:

(1)在ansible本地创建脚本:
ssh-keygen
vim ssh_key.sh
(2)编写剧本:
---
- name: Copy SSH Key
hosts: all
gather_facts: false
tasks:
- name: copy ssh key
shell: /etc/ansible/ssh_key.sh
delegate_to: localhost
changed_when: false
- name: Install Basic Software Packet
raw: apt install -y python3 ssh dnsutils curl wget dbus
register: apt
changed_when: "'will be installed' in apt.stdout"
- name: Enable dbus
systemd:
name: dbus
- name: Set hostname
hostname:
name: "{{ inventory_hostname }}"
- name: Set TimeZone
timezone:
name: Asia/Shanghai
register: reg1
- name: Set X11 KeyMap
raw: localectl set-x11-keymap us
when: reg1.changed
- name: FQDN
lineinfile:
path: /etc/hosts
search_string: "127.0.1.1"
line: "127.0.1.1 {{ inventory_hostname }}.example.com {{ inventory_hostname }} debian"
- name: Disable ssh password Login
lineinfile:
path: /etc/ssh/sshd_config
search_string: "PasswordAuthentication"
line: "PasswordAuthentication no"
register: reg2
- name: Restart SSH Server
systemd:
name: sshd
state: restarted
when: reg2.changed
- name: Configure NFS Server
hosts: deb-node-3
gather_facts: false
tasks:
- name: Install Nfs Server
apt:
name: nfs-kernel-server
update_cache: false
- name: Create Folder
file:
path: /share
state: directory
- name: Configure Nfs Server
lineinfile:
path: /etc/exports
line: "/share *(rw,sync,no_subtree_check,no_root_squash)"
register: reg3
- name: Export nfs
shell: /usr/sbin/exportfs -av
changed_when: reg3.changed
- name: Configure Apache2 Server
hosts: deb-node-1,deb-node-2
gather_facts: false
tasks:
- name: Install Apache2 Server
apt:
name:
- apache2
- nfs-client
update_cache: false
- name: Create Folder
file:
path: /srv/www/html
state: directory
- name: Mount nfs directory
lineinfile:
path: /etc/fstab
line: "192.168.10.22:/share /srv/www/html nfs4 defaults 0 0"
register: reg4
- name: Mount
raw: |
systemctl daemon-reload
mount -a
when: reg4.changed
- name: Configure Apache2
lineinfile:
path: /etc/apache2/apache2.conf
search_string: "<Directory /var/www/>"
line: "<Directory /srv/www/html>"
register: reg5
- name: Configure Apache2
lineinfile:
path: /etc/apache2/sites-enabled/000-default.conf
search_string: "DocumentRoot /var/www/html"
line: "DocumentRoot /srv/www/html"
- name: Configure Apache2 index page
raw: |
echo "This is {{ inventory_hostname }} Web page" > /srv/www/html/index.html
when: reg5.changed
- name: Restart Apache2 Server
systemd:
name: apache2
state: restarted
when: reg5.changed
- name: data collection
hosts: all
gather_facts: false
tasks:
- name: create directory
file:
path: /data/hosts
state: directory
when: "inventory_hostname == 'deb-node-3'"
- name: data collection
fetch:
src: /etc/hosts
dest: "/data/hosts/{{ inventory_hostname }}.hosts"
flat: true
- name: copy to deb-node-3
copy:
src: "/data/hosts/{{ inventory_hostname }}.hosts"
dest: "/data/hosts/"
delegate_to: deb-node-3
- name: ping
ping:
delegate_to: deb-node-3
- name: configure dns
hosts: all
gather_facts: false
tasks:
- name: Install DNS Server
apt:
name: bind9
update_cache: false
register: reg6
- name: Configure DNS Server
copy:
src: /etc/ansible/data/named.conf
dest: /etc/bind
changed_when: reg6.changed
- name: Get the host
shell: |
cat /etc/ansible/hosts | grep ansible_host | awk '{print $2}' | head -n 1
register: hosts
delegate_to: localhost
changed_when: reg6.changed
- name: Configure Slave DNS
copy:
src: /etc/ansible/data/named.slave
dest: /etc/bind/named.conf
changed_when: reg6.changed
- name: Configure Slave DNS
replace:
path: /etc/bind/named.conf
regexp: "masters.*$"
replace: "masters { {{ hosts.stdout }}; };"
changed_when: reg6.changed
- name: Configure DNS
copy:
src: /etc/ansible/data/db.example.com
dest: /etc/bind/
changed_when: reg6.changed
- name: Configure DNS
copy:
src: /etc/ansible/data/db.192
dest: /etc/bind/
changed_when: reg6.changed
- name: Configure Master DNS
copy:
src: /etc/ansible/data/named.conf
dest: /etc/bind/
delegate_to: "{{ hosts.stdout }}"
changed_when: reg6.changed
- name: Configure DNS
raw: |
echo "nameserver {{ hosts.stdout }}" > /etc/resolv.conf
changed_when: reg6.changed
- name: Restart DNS
systemd:
name: bind9
state: restarted
changed_when: reg6.changed