一、联网下载、定时任务和防火墙
1.联网下载软件包
get_url:
sersync: 联网下载软件包
url: 具体的软件在互联网上的路径
dest: 下载到具体的目标位置
[root@manager ansible_adhoc]# ansible webservers -m get_url -a "url=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm dest=/tmp"
2.定时任务模块
corn:定时任务模块
name: 定时任务描述
job: 任务具体是什么
minute:分钟
hour:小时
day:天
month:月
weekday:周
[root@manager ansible_adhoc]# ansible webservers -m cron -a "name='backup' hour=5 minute=0 job='/server/script/client_data_server.sh &>/dev/null'"
重新启用
[root@manager ansible_adhoc]# ansible webservers -m cron -a "name='backup2' hour=5 minute=0 month=2 weekday=7 job='/server/script/client_data_server.sh &>/dev/null' disabled=no"
关闭该cron任务
[root@manager ansible_adhoc]# ansible webservers -m cron -a "name='backup2' hour=5 minute=0 month=2 weekday=7 job='/server/script/client_data_server.sh &>/dev/null' disabled=yes"
3.防火墙
selinux: 关闭selinux防火墙
- name: Disable SELinux
selinux:
state: disabled
[root@manager ansible_adhoc]# ansible webservers -m selinux -a “state=disabled”
firewalld: 防火墙
service : 放行服务
port 放行端口
permanent 永久生效
immediate 临时生效
state 启用或禁用 ( enable disabled)
zone 区域
rich_rule 使用复规则
[root@manager ansible_adhoc]# ansible webservers -m systemd -a "name=firewalld state=started"
service
[root@manager ansible_adhoc]# ansible webservers -m firewalld -a "service=https state=enabled zone=public immediate=yes"
port
[root@manager ansible_adhoc]# ansible webservers -m firewalld -a "port=8080/tcp state=enabled zone=public immediate=yes"
停止防火墙
[root@manager ansible_adhoc]# ansible webservers -m systemd -a "name=firewalld state=stopped"
二、Ansible Playbook
1.什么是playbook
playbook是由一个或多个play组成,而一个play可以包含多个task任务。 安装一个nfs ( yum copy systemd )
使用不同的模块来共同完成一件事情。
2.playbook与Ad-Hoc的关系
playbook是对ad-hoc的一种编排方式。 ( yum copy systemd )
playbook可以持久化下来,而ad-hoc只能临时运行。
playbook可以做复杂的任务,ad-hoc只能做简单的任务。
3.Playbook的三板斧
yml语法抒写,结构清晰,可读性强,掌握yml,才能学好playbook
缩进:使用两个空格来表示层级关系 (不能使用tables键)
冒号:以冒号结尾的除外,其他所有的冒号后面都必须要有空格。
短横线:表示列表项,使用一个短横线加一个空格。
使用ansible playbook创建一个文件
[root@manager ansible_playbook]# cat ansible_1.yml
- hosts: webservers
tasks:
- name: create new file
file:
path: /tmp/test.txt
state: touch
owner: root
group: root
mode: 777
playbook编写NFS服务
1.安装 yum
2.配置 copy
3.初始化 group user
4.启动 systemd
在这里插入代码片
[root@manager ansible_playbook]# cat exports.j2
/ansible_data 172.16.1.0/24(rw,all_squash,anonuid=888,anongid=888)
[root@manager ansible_playbook]# cat plabybook_nfs.yml
- hosts: webservers
tasks:
#1.安装
- name: Installed NFS Server
yum:
name: nfs-utils
state: present
#2.配置
- name: Configure NFS Server
copy:
src: ./exports.j2
dest: /etc/exports
#3.初始化
- name: Init NFS Group
group:
name: ansible-nfs
gid: 888
- name: Init NFS User
user:
name: ansible-nfs
uid: 888
group: ansible-nfs
create_home: no
shell: /sbin/nologin
- name: Init NFS Create Share Directory
file:
path: /ansible_data
state: directory
owner: ansible-nfs
group: ansible-nfs
mode: 755
recurse: yes
#4.启动服务
- name: Started NFS Server
systemd:
name: nfs
state: restarted
enabled: yes
playbook编写Rsync服务
1.安装rsync
2.配置rsync
3.根据rsync的配置进行初始化操作
4.启动rsync
[root@manager ansible_playbook]# cat rsyncd.j2
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
################################
[backup]
path = /backup
[root@manager ansible_playbook]# cat rsync.passwd.j2
rsync_backup:ansible
[root@manager ansible_playbook]# cat plabybook_rsync.yml
- hosts: webservers
tasks:
#1.安装
- name: Installed Rsync Server
yum:
name: rsync
state: present
#2.配置
- name: Configure Rsync Server
copy:
src: ./rsyncd.j2
dest: /etc/rsyncd.conf
#3.初始化
- name: Init Rsync Server Group
group:
name: www
gid: 666
- name: Init Rsync Server User
user:
name: www
uid: 666
group: www
create_home: no
shell: /sbin/nologin
- name: Init Rsync Server Virtual User Passwd File
copy:
src: ./rsync.passwd.j2
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
- name: Create Rsync Server Data Directory
file:
path: /backup
state: directory
owner: www
group: www
mode: 0755
recurse: yes
#4.启动rsync服务
- name: Started Rsync Server
systemd:
name: rsyncd
state: started
enabled: yes
playbook编写LAMP架构
使用AnsiblePlaybook方式构建LAMP架构,具体操作步骤如下:
1.使用yum安装 httpd、php、php-mysql、firewalld等
2.启动httpd、firewalld、等服务
3.添加防火墙规则,放行http的流量,并永久生效
4.使用get_url下载http://fj.xuliangwei.com/public/index.php文件
[root@manager ansible_playbook]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51
[root@manager ansible_playbook]# vim hosts
[db]
172.16.1.51
注意:关闭51服务器的nginx
[root@db01 ~]# systemctl stop nginx
[root@db01 ~]# systemctl disable nginx
1.将已有的配置拷贝到控制节点,由控制节点来统一管理
[root@manager ansible_playbook]# cat plabybook_httpd_php.yml
- hosts: db
tasks:
- name: Installed Httpd PHP
yum:
name: httpd
state: present
- name: Installed Httpd PHP
yum:
name: php
state: present
- name: Configure Httpd
copy:
src: ./httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
#notify主要是监控该任务是否发生changed状态,如果没有则不作任何处理
#如果 发生了changed状态,则通知 Restart Httpd Server 这个名称
- name: Started Httpd Server
systemd:
name: httpd
state: started
- name: Get URL Index
get_url:
url: http://fj.xuliangwei.com/public/index.php
dest: /var/www/html/
# notify只能通知给handlers,这是一个特殊的tasks
handlers:
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
playbook编写LNP架构——>部署kodcloud
0.要准备好 yum仓库
1.安装 nginx php7.1
2.配置nginx (虚拟主机)
3.创建站点目录
4.上传代码
5.启动服务
1
2
3
4
5
6
[root@manager ansible_playbook]# cat nginx.conf.j2
user www;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@manager ansible_playbook]# cat kdc.oldxu.com.conf.j2
server {
listen 80;
server_name kdc.oldxu.com;
root /ansible_code;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
root@manager ansible_playbook]# cat plabybook_cloud.yml
- hosts: webservers
tasks:
#1.安装Nginx php
- name: Installed Nginx PHP7.1
yum:
name: "{{ packages }}"
state: present
vars:
packages:
- nginx
- php71w
- php71w-cli
- php71w-common
- php71w-devel
- php71w-embedded
- php71w-gd
- php71w-mcrypt
- php71w-mbstring
- php71w-pdo
- php71w-xml
- php71w-fpm
- php71w-mysqlnd
- php71w-opcache
- php71w-pecl-memcached
- php71w-pecl-redis
- php71w-pecl-mongodb
#2.管理nginx主配置文件
- name: Configure Nginx.conf
copy:
src: ./nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
- name: Create Group WWW
group:
name: www
gid: 666
- name: Create User WWW
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: no
#2.配置nginx虚拟主机
- name: Configure Nginx Virtual Host
copy:
src: ./kdc.oldxu.com.conf.j2
dest: /etc/nginx/conf.d/kdc.oldxu.com.conf
notify: Restart Nginx Server
#3.根据配置文件初始化
- name: Init Nginx Virtual Host
file:
path: /ansible_code
state: directory
owner: www
group: www
recurse: yes
#4.拷贝代码
- name: Copy Code
unarchive:
src: ./kodbox.1.09.zip
dest: /ansible_code/
owner: www
group: www
#5.启动服务
- name: Started Nginx Server
systemd:
name: nginx
state: started
enabled: yes
#6.只要配置变更则触发重启
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
plybook编写NginxProxy+Nginx+PHP+Redis架构
主机清单的定义:
10.0.0.5 负载 均衡
10.0.0.7 web
10.0.0.8 web
10.0.0.51 redis
根据 不同的组做不同的操作
[root@manager ansible_cluster]# cat hosts
[webservers]
172.16.1.7
172.16.1.8
[db]
172.16.1.51
[lb]
172.16.1.5
安装部署的顺序
redis:
1.安装
2.配置
3.启动
web:
1.安装
2.配置nginx
3.配置nginx虚拟主机
4.配置php.ini
5.配置php-fpm
6.启动服务
7.配置 特殊的handler
lb:
1.安装nginx
2.配置nginx.conf
3.配置负载均衡的虚拟主机
4.启动nginx
5.重载服务