主要内容**
服务
什么是ansible
ansible的用法
注意
安装ansible
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# rm -rm epel*
[root@master yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-6.repo
[root@master yum.repos.d]# yum clean all
[root@master ~]# yum install ansible -y
ansible的简单使用
# 1)在管理节点,生成密钥对
[root@master ~]# ssh-keygen -t rsa
# 2)将公钥文件发送到被管理节点
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
1)在管理节点安装ansible
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# rm -rm epel*
[root@master yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-6.repo
[root@master yum.repos.d]# yum clean all
[root@master ~]# yum install ansible -y
2)配置免密认证
# 1)在管理节点,生成密钥对
[root@master ~]# ssh-keygen -t rsa
# 2)将公钥文件发送到被管理节点
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
3)修改主机清单文件
[root@master ~]# vim /etc/ansible/hosts
[myserver]
192.168.31.64
192.168.31.65
4)检查被管理节点是否在线
[root@master ~]# ansible myserver -m ping
192.168.31.65 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.31.64 | SUCCESS => {
"changed": false,
"ping": "pong"
}
5)在被管理节点安装apache
[root@master ~]# ansible myserver -a "yum install httpd -y"
[root@master ~]# ansible myserver -a "rpm -q httpd"
192.168.31.64 | SUCCESS | rc=0 >>
httpd-2.2.15-69.el6.centos.x86_64
6)启动被管理节点上的apache
[root@master ~]# ansible myserver -a "service httpd start"
[root@master ~]# ansible myserver -a "service httpd status"
7)关闭被管理节点的防火墙和selinux
[root@master ~]# ansible myserver -a "service iptables stop"
[root@master ~]# ansible myserver -a "setenforce 0"
1.请检查在管理主机上是否安装Ansible,如没有请安装
[root@master ~]# rpm -q ansible
[root@master ~]# yum install ansible -y
2.安装成功后请测试Ansible是否安装成功(查看版本信息)
[root@master ~]# ansible --version
ansible 2.6.20
3.请在默认的Inventory文件:hosts文件中添加主机组webserver,并在组中添加那两台Centos6.x的主机
[root@master ~]# vim /etc/ansible/hosts
[webserver]
192.168.31.64
192.168.31.65
4.请为管理主机配置公钥登录被管理服务器(无密码登录)
# 1)在管理节点,生成密钥对
[root@master ~]# ssh-keygen -t rsa
# 2)将公钥文件发送到被管理节点
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
5.使用ansible的ping模块,测试webserver主机组的主机状态
[root@master ~]# ansible myserver -m ping
6.请使用ansible的yum模块在两台Centos6的服务器上安装Apache
[root@master ~]# ansible myserver -a "yum install httpd -y"
7.请使用ansible的service模块将刚才安装的Apache启动,并设置为开机自启
[root@master ~]# ansible myserver -a "service httpd start"
[root@master ~]# ansible myserver -a "chkconfig httpd on"
8.请使用ansible关闭所有主机的防火墙,并在浏览器测试Apache是否可以访问
[root@master ~]# ansible myserver -a "service iptables stop"
[root@master ~]# ansible myserver -a "setenforce 0"
ansible的命令格式
ansible 目标主机 [-f N] [-m module_name] [-a args]
案例:在每个被管理主机上执行free -m命令
[root@centos6-1 ~]# ansible myserver -m command -a "free -m"
192.168.31.65 | SUCCESS | rc=0 >>
total used free shared buffers cached
Mem: 981 210 770 0 11 62
-/+ buffers/cache: 136 844
Swap: 1983 0 1983
192.168.31.64 | SUCCESS | rc=0 >>
total used free shared buffers cached
Mem: 981 200 781 0 11 58
-/+ buffers/cache: 129 851
Swap: 1983 0 1983
补充1
[root@centos6-1 ~]# ansible myserver -a "free -m"
补充2:
[root@centos6-1 ~]# ansible all -a "free -m"
案例:在所有被管理节点上创建用户tom,然后修改tom密码为123
[root@centos6-1 ~]# ansible all -m shell -a "echo 123 | passwd --stdin tom"
192.168.31.65 | SUCCESS | rc=0 >>
Changing password for user tom.
passwd: all authentication tokens updated successfully.
192.168.31.64 | SUCCESS | rc=0 >>
Changing password for user tom.
passwd: all authentication tokens updated successfully.
案例:在所有被管理节点创建用户jerry,密码设为123
[root@centos6-1 ~]# ansible all -m user -a "name=jerry password=123"
[root@centos6-1 ~]# ansible all -m shell -a "tail -n1 /etc/passwd"
案例:在所有被管理节点删除用户jerry
[root@centos6-1 ~]# ansible all -m user -a 'name="jerry" state="absent"'
案例:将/tmp/a.txt分发到所有被管理节点的/home下,复制后,将权限改为700
[root@centos6-1 ~]# ansible all -m copy -a "src=/tmp/a.txt dest=/ mode=700"
故障案例:在使用copy模块报错:
192.168.31.64 | FAILED! => {
"changed": false,
"checksum": "d475c8d3ef70e5e2f92a4ace42e98dc6f19cf45d",
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
[root@centos6-1 ~]# ansible all -m shell -a "yum install libselinux-python -y"
案例:在每个被管理主机的tmp下创建空文件abc.txt
方法1
[root@centos6-1 ~]# ansible all -m file -a "name=/tmp/abc.txt state=touch"
方法2
[root@centos6-1 ~]# ansible all -m file -a "path=/tmp/abc.txt state=touch"
案例:在每个被管理主机的tmp下创建空文件abc.txt,创建软链接soft.txt
[root@centos6-1 ~]# ansible all -m file -a "src=/tmp/abc.txt path=/tmp/soft.txt state=link"
案例:检查被管理节点是否在线
[root@centos6-1 ~]# ansible all -m ping
案例:重启所有被管理节点
方法1:
[root@centos6-1 ~]# ansible all -m reboot
方法2:
[root@centos6-1 ~]# ansible all -m shell -a "reboot"
案例:在所有被管理节点安装zsh
[root@centos6-1 ~]# ansible all -m yum -a "name=zsh state=present
[root@centos6-1 ~]# ansible all -m shell -a "rpm -q zsh"
案例:在所有被管理主机上安装apache
[root@centos6-1 ~]# ansible all -m yum -a "name=httpd state=present"
案例:启动所有被管理主机上的httpd服务,并设置为开机自动启动
[root@centos6-1 ~]# ansible all -m service -a "name=httpd state=started enabled=on"
案例:在被管理节点的/home、/tmp、/root分别创建一个文件,文件名 时间.txt
[root@centos6-1 ~]# cat my.sh
#!/bin/bash
for dir in /home /tmp /root
do
touch $dir/`date +%F-%T`.txt
done
[root@centos6-1 ~]# chmod +x my.sh
[root@centos6-1 ~]# ansible all -m script -a "/root/my.sh"
案例:将nginx的源码包解压到被管理节点的home下
[root@centos6-1 ~]# ansible all -m unarchive -a "src=/root/nginx-1.16.1.tar.gz dest=/home/"
作用:周期完成特定的工作
格式:-m cron -a “选项”
选项
案例:每隔2分钟在/tmp/a.txt中写入一行数据
[root@centos6-1 .ssh]# ansible all -m cron -a "name=mycron state=present job='echo 123>>/tmp/a.txt' minute=*/2"
1) 准备3台虚拟机,1台为ansible管理主机,2台为nginx服务
关闭指纹信息
2) 请检查ansible主机的ansible是否安装,若未安装请安装
[root@centos6-1 ~]# rpm -q ansible
ansible-2.6.20-1.el6.noarch
3) 请合理添加服务器组,将两台nginx服务器分配在webserver组当中
[root@centos6-1 ~]# vi /etc/ansible/hosts
[webserver]
192.168.31.64
192.168.31.65
4) 配置免密码登录
[root@centos6-1 ~]# ssh-keygen -t rsa
[root@centos6-1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.31.64
[root@centos6-1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.31.65
5) 合理调用相关模块,安装nginx依赖环境
[root@centos6-1 ~]# yum install gcc gcc-c++ make pcre-devel ncurses-devel -y
6) 批量分发Nginx源码包至Nginx服务器
[root@centos6-1 ~]# ansible all -m shell -a "yum install gcc gcc-c++ make pcre-devel ncurses-devel zlib-devel -y"
7) 批量分发安装指令至Nignx服务器(提示:可分发Nginx安装脚本,执行脚本安装,但脚本不可使用提前编写好的脚本!可以现写脚本使用)
思路1:先将nginx安装包发送到被管理节点,安装远程执行解压、编译、安装
思路2:直接用unarchive解压分发,安装远程执行编译、安装
思路3:直接用script脚本完成安装过程
首先编写shell脚本
[root@centos6-1 ~]# vim /root/install_ngx.sh
#!/bin/bash
cd /home/nginx-1.16.1 && ./configure --prefix=/usr/local/nginx && make && make install
下载nginx的安装包
[root@centos6-1 ~]# cd /root/
[root@centos6-1 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
分发安装包
[root@centos6-1 ~]# ansible all -m unarchive -a "src=/root/nginx-1.16.1.tar.gz dest=/home/"
并进行安装
[root@centos6-1 ~]# ansible all -m script -a "/root/install_ngx.sh"
8) 启动所有Nginx服务器的Nginx服务
[root@centos6-1 ~]# ansible all -m shell -a "ss -tnl | grep 80"
9) 查看所有Nginx的端口监听是否正常
[root@centos6-1 ~]# ansible all -m shell -a "/usr/local/nginx/sbin/nginx"
[root@centos6-1 ~]# ansible all -m shell -a "ss -tnl | grep 80"
[root@centos6-1 ~]# ansible all -m shell -a "lsof -i :80"
10) 访问所有Nginx服务器,测试是否可以访问到测试页面
[root@centos6-1 ~]# ansible all -m shell -a "systemctl stop firewalld"
[root@centos6-1 ~]# ansible all -m shell -a "service iptables stop"
1. ansible all -m shell -a "hostname"
1. ansible all -m shell -a "mkdir /home/abc; chown root /home/abc"
2. ansible all -m file -a "state=directory path=/home/abc1 owner=root"
1. ansible all -m shell -a "mkdir /home/abc; chmod 777/home/abc"
2. ansible all -m file -a "state=directory path=/home/abc1 mode=777"
1. ansible all -m shell -a "mkdir /opt/abc"
2. ansible all -m file -a "state=directory path=/opt/abc1"
1. ansible all -m shell -a "touch /home/abc.txt"
2. ansible all -m file -a "state=touch path=/home/abc1.txt"
1. ansible all -m shell -a "chmod 422 /home/abc.txt"
2. ansible all -m file -a "name=/home/abc1.txt mode=422"
`1. ansible all -m shell -a "touch /home/abc2.txt;
2. chown root` /home/abc2.txt"
3. ansible all -m file -a "state=touch path=/home/abc2.txt owner=root"
1. ansible all -m shell -a "hostname pc2;sed -i s/HOSTNAME=.*/HOSTNAME=pc2/g /etc/sysconfig/network"
1. ansible all -m shell -a 'echo "192.168.31.63 pc2">>/etc/hosts'
第一题:ansible配置及使用
1)准备3台服务器,在其中一台配置安装ansible并查看版本号
[root@centos6-1 .ssh]# ansible --version
ansible 2.6.20
2)将被控端添加到ansible主机的主机清单中并验证是否能连接
[root@centos6-1 .ssh]# cat /etc/ansible/hosts
[myserver]
192.168.31.64
192.168.31.65
[root@centos6-1 .ssh]# ansible all -m ping
3)修改任务返回SUCCESS时的颜色为purple并验证
[root@centos6-1 .ssh]# vim /etc/ansible/ansible.cfg
ok = purple
[root@centos6-1 .ssh]# ansible all -m ping
5)配置免密登录并验证
[root@centos6-1 ~]# ssh-keygen -t rsa
[root@centos6-1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.31.64
[root@centos6-1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.31.65
6)使用ansible模块永久修改远程主机名分别为node1、node2
centos6做法
# ansible 192.168.31.65 -m replace -a "path=/etc/sysconfig/network regexp='(HOSTNAME=).*' replace=\1node1"
centos7做法
# ansible 192.168.31.65 -m shell -a "hostnamectl set-hostname node2"
7)使用ansible模块在一台远程主机下opt下创建目录apache,属组root,权限777
[root@centos6-1 ~]# ansible all -m file -a "path=/opt/apache state=directory mode=777 group=root"
8)使用模块推送apache到远程主机下
[root@centos6-1 ~]# ansible all -m copy -a "src=/root/httpd-2.2.9.tar.gz dest=/tmp"
9)使用模块在远程主机上安装apache
[root@centos6-1 ~]# vim /root/install_httpd.sh
#!/bin/bash
yum install gcc gcc-c++ -y
cd /tmp
tar xvf httpd-2.2.9.tar.gz
cd httpd-2.2.9
./configure --prefix=/opt/apache && make && make install
[root@centos6-1 ~]# ansible all -m script -a "/root/install_httpd.sh"
10)成功启动服务并访问apache首页
[root@centos6-1 ~]# ansible all -m shell -a "/opt/apache/bin/apachectl start"
[root@centos6-1 ~]# ansible all -m shell -a "service iptables stop"
第二题:shell脚本实战
1)使用模块在远程主机下创建count.txt文件,属组为root,权限为777
[root@centos6-1 ~]# ansible all -m file -a "path=/opt/count.txt state=touch mode=777 group=root"
2)在ansible主机上编写脚本,要求如下
①统计远程主机/opt目录下文件的个数
②将结果统计到指定文件count.txt中
[root@centos6-1 ~]# vi /root/check.sh
#!/bin/bash
ls /opt | wc -l >/tmp/count.txt
[root@centos6-1 ~]# ansible all -m script -a "/root/check.sh"
3)使用ansible模块将count.txt文件返回到ansible主机上并查看
[root@centos6-1 ~]# ansible all -m fetch -a "src=/tmp/count.txt dest=/tmp"
yaml文件中的元素
变量命名规则
变量类别
获取内置变量
[root@centos6-1 ~]# ansible all -m setup
执行a.yaml,同时传递变量name=tom,age=20
# ansible-playbook a.yaml --extra-vars "name=tom, age=20"
案例:安装httpd,要求设置apache的端口为808,网站跟目录/myweb
- host: all # 指定要操作的主机组
remoute_user: root # 指定在远程主机上以哪个用户身份执行tasks
vars: # 定义变量
- port: 808 # 变量名、变量值
- root: /myweb # 变量名、变量值
tasks: # 指定要执行的操作列表
- name: install httpd # 设置一个名称,用于给用户提示正在执行操作
yum: name=httpd state=latest # yum是模块名,后面是参数
- name: start httpd
service: name=httpd state=started # service是模块名,后面的参数
yaml文件中主要构成
案例:通过一个yaml文件实现如下要求:
在被管理主机上新建一个用户组,组名mygp1,组id是800
再在每个被管理主机上新建用户tom01,将tom加入到这个组中
将当前主机上/etc/inittab分发到所有被管理主机的/tmp下
# 创建yaml文件
[root@centos6-1 ~]# vim user.yaml
- hosts: all
remote_user: root
tasks:
- name: create gropu
group: name=mygp1 gid=800 state=present
- name: create user
user: name=tom group=mygp1 state=present
- name: send file
copy: src=/etc/inittab dest=/tmp
# 执行yaml文件
[root@centos6-1 ~]# ansible-playbook user.yaml
案例1:在所有被管理节点安装httpd,然后启动httpd
- hosts: all
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: start httpd
service: name=httpd state=started enabled=true
案例2:在所有被管理节点安装httpd,然后启动httpd,要求httpd启动端口是8080
- hosts: all
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: start httpd
service: name=httpd state=started enabled=true
- name: send httpd.conf
copy: src=/root/httpd.conf.template dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
handlers:
- name: restart httpd
service: name=httpd state=restarted
(1)自定义变量
- hosts: all
remote_user: root
vars:
- package_name: mysql-server # mariadb-server
- service_name: mysqld # mariadb
tasks:
- name: install mysql server
yum: name={{ package_name }} state=latest
- name: start mysql server
service: name={{ service_name }} state=started
(2)使用ansible的内置变量
案例:
在每个被管理主机上创建一个用户,用户名和主机名相同
在每个被管理主机上创建一个和主机同名的目录
# 查询内置变量
[root@centos6-1 ~]# ansible all -m setup | grep fqdn
"ansible_fqdn": "centos6-2",
[root@centos6-1 ~]# cat c.yaml
- hosts: all
tasks:
- name: create user
user: name={{ ansible_fqdn }} state=present
- name: create file
file: name=/tmp/{{ ansible_fqdn }} state=touch
(3)主机清单变量
# 定义主机变量
[webservers]
192.168.31.64 userid=1050
192.168.31.66 userid=1060
# 定义主机组变量
[webservers:vars]
username=jerry
- hosts: all
tasks:
- name: create user
user: name={{ username }} uid={{ userid }} state=present
主机规划
案例:基于yaml文件实现如下功能
1)在ansible主机,安装epel源
[root@centos6-1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2)在ansible主机,安装nginx
[root@centos6-1 ~]# yum install nginx -y
3)在ansible主机,基于nginx的配置文件生成配置文件模板
[root@centos6-1 ~]# cp /etc/nginx/nginx.conf.default /root/template.j2
[root@centos6-1 ~]# vim /root/template.j2
# 修改一行
listen {{ nginx_port }};
4)修改主机清单文件
[root@centos6-1 ~]# vim /etc/ansible/hosts
[webservers]
192.168.31.64 nginx_port=81
192.168.31.65 nginx_port=82
5)编译yaml文件
- hosts: all
tasks:
- name: delete old epel repo file
shell: rm -rf /etc/yum.repos.d/epel*
- name: create new epel repo file
shell: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
- name: install nginx
yum: name=nginx state=latest
- name: send nginx.conf
template: src=/root/template.j2 dest=/etc/nginx/nginx.conf
notify:
- restart nginx
- name: start nginx
service: name=nginx state=started enabled=true
handlers:
- name: restart nginx
service: name=nginx state=restarted
案例:用playbook实现如下功能
1:在所有被管理主机上安装zsh
2:在主机名为centos7-2的主机上创建用户tom3,其他主机不创建
- hosts: all
tasks:
- name: install zsh
yum: name=zsh state=present
- name: create user tom3
user: name=tom3 state=present
when: ansible_fqdn == "centos7-5"
格式:
注意:
案例:在所有被管理主机上创建5个用户u1 u2 u3 u4 u5
- hosts: all
tasks:
- name: create user
user: name={{ item }} state=present
with_items:
- u1
- u2
- u3
- u4
- u5
作用:给某个task设置一个表情,用于仅仅执行某个task
案例:
- hosts: all
tasks:
- name: isntall
shell: yum install httpd -y
- name: send file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
tags:
- senfile
- name: start httpd
shell: systemctl start httpd
handlers:
- name: restart httpd
shell: systemctl restart httpd
[root@centos7-1 ~]# ansible-playbook 4.yaml --tags="senfile"