Linux System Environment
[root@ansible ~]# cat /etc/redhat-release #==》系统版本
CentOS Linux release 7.5.1804 (Core)
[root@ansible ~]# uname –r #==》系统内核
3.10.0-862.el7.x86_64
[root@ansible ~]# uname -m #==》系统位数
x86_64
[root@ansible ~]# echo $LANG #==》系统字符集
en_US.UTF-8
[root@ansible ~]# ssh –V #==》SSH版本
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
[root@master ~]# ansible –version #==》Ansible版本
ansible 2.4.2.0
[root@master ~]# python –version #==》Python版本
Python 2.7.5
Ansible Roles角色简介
Ansible 角色是基于Ansible Playbook剧本进行拆分成从多模块的文件结构,然后按模块执行并且还可以制作成模版共享给其它角色使用。
Ansible Roles 文件结构
标注:创建的目录里必须包含 files/handlers/tasks/templates/vars 目录,这些目录的名称是固定的,不能修改,否则对应的功能就失效。对应这些目录编写的playbook剧本名称也是固定为main.yml
[root@ansible ~]# tree /etc/ansible/roles/
/etc/ansible/roles/
└── nfs #==》创建的nfs目录名称,这个名称可以自定义
├── files #==》部署的文件(需要传输给远程主机的文件)
├── handlers #==》触发的程序
├── tasks #==》具体的任务剧本
├── templates #==》部署的模版,可供其它角色使用
└── vars #==》存放的变量
一、Ansible Roles方式部署优化系统并架设NFS(先把playbook剧本写好)
标注:Ansible Roles官网说明:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
主机规则
主机IP地址 主机名 备注
10.0.0.7 ansible ansible服务端
10.0.0.31 NFSserver NFS服务端
10.0.0.32 NFSclient NFS客户端
角色规则
1、optimize #==》系统优化
2、nfs服务端 #==》安装配置nfs服务端
3、nfs客户端 #==》安装配置nfs客户端
Ansible hosts主机配置
[root@ansible ~]# cat /etc/ansible/hosts
[optimize_host]
10.0.0.31
10.0.0.32
[nfs_server]
10.0.0.31
[nfs_client]
10.0.0.32
1、创建角色目录
[root@ansible ~]# cd /etc/ansible/roles/
[root@ansible roles]#
mkdir -p {init_system,nfs_server,nfs_client}/{files,handlers,tasks,templates,vars}
[root@ansible roles]# tree
.
├── init_system
│ ├── files
│ ├── handlers
│ ├── tasks
│ ├── templates
│ └── vars
├── nfs_client
│ ├── files
│ ├── handlers
│ ├── tasks
│ ├── templates
│ └── vars
└── nfs_server
├── files
├── handlers
├── tasks
├── templates
└── vars
18 directories, 0 files
2、系统优化Playbook剧本
[root@ansible ~]# vim /etc/ansible/roles/init_system/tasks/main.yml
- name: 01-close iptables
service:
name: firewalld
state: stopped
- name: 02-close iptables autostart
systemd:
name: firewalld
state: stopped
- name: 03-close SElinux
selinux:
policy: targeted
state: disabled
- name: 04-configure character
shell: "{{ item }}"
loop:
- cp /etc/locale.conf{,.bak}
- echo 'LANG="en_US.UTF-8"' > /etc/locale.conf
- name: 05-optimizing SSH
lineinfile:
path: /etc/ssh/sshd_config
backrefs: yes
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
loop:
- { regexp: '^#UseDNS yes',line: 'UseDNS no' }
- { regexp: '#GSSAPIAuthentication yes',line: 'GSSAPIAuthentication no' }
- name: 06-update system date and clean mail rubbish file
cron:
name: "{{ item.name }}"
minute: "{{ item.minute }}"
hour: "{{ item.hour }}"
job: "{{ item.job }}"
loop:
- { name: 'update system date', minute: '00', hour: '01', job: '/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' }
- { name: 'clean mail', minute: '00', hour: '23', job: '/usr/bin/find /var/spool/postfix/maildrop/ -type f | xargs rm -rf >/dev/null 2>&1' }
- name: 07-configure aliyun yum source
get_url:
url: "{{ item.url }}"
dest: "{{ item.dest }}"
loop:
- { url: 'http://mirrors.aliyun.com/repo/Centos-7.repo', dest: '/etc/yum.repos.d/CentOS-Base.repo' }
- { url: 'http://mirrors.aliyun.com/repo/epel-7.repo', dest: '/etc/yum.repos.d/epel.repo' }
- name: 08-yum install/update common software
yum:
name: "{{ item }}"
state: latest
loop:
- tree
- bash
- openssl
- openssh
- ntpdate
- vim
- name: 09-hidden system version information AND setup system file descriptor AND delete old yum file
shell: "{{ item }}"
loop:
- "cat /dev/null > /etc/issue"
- "cat /dev/null > /etc/issue.net"
- "echo '* - nofile 65535' >> /etc/security/limits.conf"
- find /etc/yum.repos.d/ -name "CentOS-Base.repo" | xargs rm -f
- find /etc/yum.repos.d/ -name "epel.repo" | xargs rm -f
- ntpdate ntp1.aliyun.com
3、nfs服务端playbook剧本
[root@ansible ~]# vim /etc/ansible/roles/nfs_server/tasks/main.yml
- hosts: nfs_server
vars:
show: 'showmount -e 10.0.0.31'
remote_user: root
tasks:
- name: 01-delete old yum file
shell: "{{ item }}"
loop:
- find /etc/yum.repos.d/ -name "CentOS-Base.repo" | xargs rm -f
- find /etc/yum.repos.d/ -name "epel.repo" | xargs rm -f
- name: 02-configure aliyun yum source
get_url:
url: "{{ item.url }}"
dest: "{{ item.dest }}"
loop:
- { url: 'http://mirrors.aliyun.com/repo/Centos-7.repo', dest: '/etc/yum.repos.d/CentOS-Base.repo' }
- { url: 'http://mirrors.aliyun.com/repo/epel-7.repo', dest: '/etc/yum.repos.d/epel.repo' }
- name: 03-yum install nfs
yum:
name: nfs-utils
state: latest
- name: 04-mkdir data directory
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
- name: 05-copy nfs configure /ect/exports
copy:
src: /etc/exports
dest: /etc/
notify: restart_nfs_server
handlers:
- name: restart_nfs_server
service:
name: nfs
state: restarted
- name: 06-start rpcbind nfs
service:
name: "{{ item }}"
state: started
loop:
- rpcbind
- nfs
- name: 07-enable nfs rpcbind
systemd:
name: "{{ item }}"
enabled: yes
loop:
- rpcbind
- nfs
- name: 08-check nfs
shell: "showmount -e"
register: printmount
- name: 09-showmount
debug:
msg: "{{ printmount.stdout_lines }}"
4、nfs客户端playbook剧本
[root@ansible ~]# vim /etc/ansible/roles/nfs_client/tasks/mail.yml
- hosts: nfs_client
vars:
yum_url_base: 'http://mirrors.aliyun.com/repo/Centos-7.repo'
yum_url_epel: 'http://mirrors.aliyun.com/repo/epel-7.repo'
remote_user: root
tasks:
- name: 01-delete old yum file
shell: "{{ item }}"
loop:
- find /etc/yum.repos.d/ -name "CentOS-Base.repo" | xargs rm -f
- find /etc/yum.repos.d/ -name "epel.repo" | xargs rm -f
- name: 02-configure aliyun yum source
get_url:
url: "{{ item.url }}"
dest: "{{ item.dest }}"
loop:
- { url: "{{ yum_url_base }}", dest: '/etc/yum.repos.d/CentOS-Base.repo' }
- { url: "{{ yum_url_epel }}", dest: '/etc/yum.repos.d/epel.repo' }
- name: 03-yum install rpcbind
yum:
name: nfs-utils
state: latest
- name: 04-mkdir data directory
file:
path: /data
state: directory
owner: nfsnobody
group: nfsnobody
- name: 05-start rpcbind
service:
name: rpcbind
state: started
- name: 06-enable rpcbind
systemd:
name: rpcbind
enabled: yes
- name: 07-mount /backup
mount:
path: /data
src: 10.0.0.31:/data
fstype: nfs
opts: defaults
state: mounted
二、把NFS服务端剧本改造成roles角色
1、NFS服务端roles角色目录
[root@ansible ~]# tree /etc/ansible/roles/nfs_server/
/etc/ansible/roles/nfs_server/
├── files
├── handlers
├── tasks
│ └── main.yml
├── templates
└── vars
2、files目录操作
标注:把NFS服务端的配置文件export放入到files目录,并修改copy模块src参数路径为配置文件命名即可,无须指定exports文件路径
[root@ansible ~]# cp /etc/exports /etc/ansible/roles/nfs_server/files/
[root@ansible ~]# ls -l /etc/ansible/roles/nfs_server/files/
total 4
-rw-r--r-- 1 root root 27 Aug 15 14:13 exports
[root@ansible ~]# vim /etc/ansible/roles/nfs_server/tasks/main.yml
- name: 05-copy nfs configure /ect/exports
copy:
src: exports
dest: /etc/
notify: restart_nfs_server
3、handlers目录操作
标注:把playbook剧本里包含handlers操作放入到此目录的剧本文件
[root@ansible ~]# vim /etc/ansible/roles/nfs_server/handlers/main.yml
- name: restart_nfs_server
service:
name: nfs
state: restarted
4、vars目录操作
[root@ansible ~]# vim /etc/ansible/roles/nfs_server/vars/main.yml
yum_url_base: http://mirrors.aliyun.com/repo/Centos-7.repo
yum_url_epel: http://mirrors.aliyun.com/repo/epel-7.repo
5、tasks目录操作
[root@ansible ~]# vim /etc/ansible/roles/nfs_server/tasks/main.yml
- name: 01-delete old yum file
shell: "{{ item }}"
loop:
- find /etc/yum.repos.d/ -name "CentOS-Base.repo" | xargs rm -f
- find /etc/yum.repos.d/ -name "epel.repo" | xargs rm -f
- name: 02-configure aliyun yum source
get_url:
url: "{{ item.url }}"
dest: "{{ item.dest }}"
loop:
- { url: "{{ yum_url_base }}", dest: '/etc/yum.repos.d/CentOS-Base.repo' }
- { url: "{{ yum_url_epel }}", dest: '/etc/yum.repos.d/epel.repo' }
- name: 03-yum install nfs
yum:
name: nfs-utils
state: latest
- name: 04-mkdir data directory
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
- name: 05-copy nfs configure /ect/exports
copy:
src: /etc/exports
dest: /etc/
notify: restart_nfs_server
- name: 06-start rpcbind nfs
service:
name: "{{ item }}"
state: started
loop:
- rpcbind
- nfs
- name: 07-enable nfs rpcbind
systemd:
name: "{{ item }}"
enabled: yes
loop:
- rpcbind
- nfs
- name: 08-check nfs
shell: "showmount -e"
register: printmount
- name: 09-showmount
debug:
msg: "{{ printmount.stdout_lines }}"
6、init_system系统优化配置角色
[root@ansible ~]# vim /etc/ansible/roles/init_system/tasks/main.yml
#==》将以下三行删除
- hosts: optimize_host
remote_user: root
tasks:
7、nfs服务端角色目录最终效果
[root@ansible ~]# tree /etc/ansible/roles/nfs_server/
/etc/ansible/roles/nfs_server/
├── files
│ └── exports
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
8、编写nfs服务端主任务文件
[root@ansible ~]# vim /etc/ansible/roles/nfs_server_roles.yml
- hosts: nfs_server
remote_user: root
roles:
- init_system #==》系统优化角色根目录名称
- nfs_server #==》nfs服务端角色根目录名称
网友评论