ansible简介
Ansible是一种自动化的运维工具, 基于Python开发,能够实现批量操作. 但其实Ansible是基于模块工作的, 本身没有批量部署的能力. 真正具有批量部署的是ansible所运行的模块, Ansible只是提供一种框架.
ansible安装
# host: Control Machine
# 准备好清华base源和epel源
[root@control_machine ~]# ls /etc/yum.repos.d
centos-base.repo epel.repo
# 使用yum安装ansible
[root@control_machine ~]# yum -y install ansible
- 使用ansible前之主机认证
# 生成公钥私钥用于认证主机
[root@control_machine ~]# ssh-keygen -t rsa -P '' -f '/root/.ssh/id_rsa'
# 认证主机(分别执行)
[root@control_machine ~]# ssh-copy-id root@192.168.161.11
[root@control_machine ~]# ssh-copy-id root@192.168.161.12
[root@control_machine ~]# ssh-copy-id root@192.168.161.13
- 使用ansible前之配置文件及主机列表
[root@control_machine ~]# cp /etc/ansible/{ansible.cfg,ansible.cfg.bak}
[root@control_machine ~]# vim /etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts # 定义主机列表位置
#library = /usr/share/my_modules/ # 定义模块位置文件
#forks = 5 # 设定并发数
#log_path = /var/log/ansible.log # ansible日志记录位置
#gathering = implicit # fact信息收集开关
#fact_caching = memory # fact缓存方式
#fact_caching_connection =/tmp # fact缓存路径
#ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
>>> ansible.cfg配置文件位置: ~/.ansible.cfg -->> 使用该位置文件可以让Linux中每个用户都有独立的配置文件
[root@control_machine ~]# cat .ansible.cfg
[defaults]
inventory = ~/.ansible/hosts
library = /usr/share/my_modules/
forks = 5
#log_path = /var/log/ansible.log
#fact_caching = memory
#fact_caching_connection = /tmp
#vault_password_file = /path/to/vault_password_file
>>> hosts主机列表: 使用ini格式定义主机列表, 可对主机进行分组操作
[root@control_machine ~]# cat .ansible/hosts
[webserver]
192.168.161.11
192.168.161.12
192.168.161.13
ansible Ad-Hoc
[root@control_machine ~]# ansible webserver -m ping -o
192.168.161.11 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.161.12 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.161.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@control_machine ~]# ansible webserver -m shell -a 'hostname' -o
192.168.161.13 | CHANGED | rc=0 | (stdout) jx04
192.168.161.11 | CHANGED | rc=0 | (stdout) jx02
192.168.161.12 | CHANGED | rc=0 | (stdout) jx03
[root@control_machine ~]# ansible webserver -m command -a 'hostname' -o
192.168.161.13 | CHANGED | rc=0 | (stdout) jx04
192.168.161.12 | CHANGED | rc=0 | (stdout) jx03
192.168.161.11 | CHANGED | rc=0 | (stdout) jx02
- cron模块
[root@control_machine ~]# ansible-doc -s cron # 获取cron模块的设置信息
[root@control_machine ~]# ansible webserver -m cron -a 'minute="*/5" hour="15" day="01" month="04" weekday="*" job="touch /opt/file001 &" name="create file"'
[root@control_machine ~]# ansible webserver -m shell -a 'crontab -l' --one-line
192.168.161.13 | CHANGED | rc=0 | (stdout) #Ansible: create file\n*/5 15 01 04 * touch /opt/file001 &
192.168.161.12 | CHANGED | rc=0 | (stdout) #Ansible: create file\n*/5 15 01 04 * touch /opt/file001 &
192.168.161.11 | CHANGED | rc=0 | (stdout) #Ansible: create file\n*/5 15 01 04 * touch /opt/file001 &
present(安装/增加/启动)/absent(删除/卸载/停止)
[root@control_machine ~]# ansible webserver -m cron -a 'name="create file" state=absent'
- copy模块
# 复制功能并更改文件的属主属组及权限并备份
[root@control_machine ~]# ansible webserver -m copy -a 'src=/opt/ansible.txt dest=/tmp/ansible.txt owner=root group=root mode=0600' --one-line
# 向远程服务器中的文件添加内容
[root@control_machine ~]# ansible webserver -m copy -a 'content="ansible test content of copy module" dest="/tmp/ansible-content.txt"'
- file模块
# 更改拥有者和所属组以及权限
[root@control_machine ~]# ansible webserver -m file -a 'owner="root" group="root" mode=0644 dest="/tmp/ansible.txt"' --one-line
# 在远程服务器上创建文件
[root@control_machine ~]# ansible webserver -m file -a 'path=/tmp/ansible_file.txt state=touch' --one-line
# 在远程服务器上删除文件
[root@control_machine ~]# ansible webserver -m file -a 'path=/tmp/ansible_file.txt state=absent' --one-line
# 更改拥有者和所属组以及权限
[root@control_machine ~]# ansible webserver -m file -a 'owner="root" group="root" mode=0644 dest="/tmp/ansible.txt"' --one-line
# 在远程服务器上创建文件
[root@control_machine ~]# ansible webserver -m file -a 'path=/tmp/ansible_file.txt state=touch' --one-line
# 在远程服务器上删除文件
[root@control_machine ~]# ansible webserver -m file -a 'path=/tmp/ansible_file.txt state=absent' --one-line
- get_url模块
# 下载资源到远程指定的目录中
ansible webserver -m get_url -a 'url=https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm dest=/tmp'
- yum模块
# 安装软件
[root@control_machine ~]# ansible webserver -m yum -a 'name=httpd state=latest' --one-line
# 卸载软件
[root@control_machine ~]# ansible webserver -m yum -a 'name=httpd state=absent' --one-line
# 安装软件
[root@control_machine ~]# ansible webserver -m yum -a 'name=httpd state=latest' --one-line
# 卸载软件
[root@control_machine ~]# ansible webserver -m yum -a 'name=httpd state=absent' --one-line
- service模块
# 启动服务
[root@control_machine ~]# ansible webserver -m service -a 'name=httpd state=started enabled=true'
# 重启服务
[root@control_machine ~]# ansible webserver -m service -a 'name=httpd state=restarted'
# 关闭服务
[root@control_machine ~]# ansible webserver -m service -a 'name=httpd state=stopped'
- script模块: 把管理机上的脚本复制到受控机中执行
[root@control_machine ~]# cat .ansible/scripts/cpuMonitor.sh
#!/usr/bin/env bash
#
# author: bavdu
# date: 2019/07/27
# usage: monitor cpu status
DATE=$(date +'%Y-%m-%d %H:%M:%S')
IPADDR=$(ifconfig | grep inet | awk 'BEGIN{ FS=" " }NR==1{ print $2 }')
MAIL="bavduer@163.com"
# 检测vmstat命令是否存在
if ! which vmstat &>/dev/null; then
yum -y install procps-ng &>/dev/null
if [ $? -eq 0 ];then
echo "vmstat already installed"
fi
fi
US=$(vmstat | awk 'BEGIN{ FS=" " }NR==3{ print $13 }')
SY=$(vmstat | awk 'BEGIN{ FS=" " }NR==3{ print $14 }')
ID=$(vmstat | awk 'NR==3{ print $15 }')
WA=$(vmstat | awk 'NR==3{ print $16 }')
ST=$(vmstat | awk 'NR==3{ print $17 }')
useTotal=$((${US}+${SY}))
if [[ ${useTotal} -ge 70 ]];then
echo "
Date: ${DATE}
Host: ${HOSTNAME}: ${IPADDR}
Problem: CPU using rate: ${useTotal}%
" | mail -s "CPU Monitor Warnning" ${MAIL}
fi
[root@control_machine ~]# ansible webserver -m script -a '.ansible/scripts/cpuMonitor.sh'
[root@control_machine ~]# cat .ansible/scripts/cpuMonitor.sh
#!/usr/bin/env bash
#
# author: bavdu
# date: 2019/07/27
# usage: monitor cpu status
DATE=$(date +'%Y-%m-%d %H:%M:%S')
IPADDR=$(ifconfig | grep inet | awk 'BEGIN{ FS=" " }NR==1{ print $2 }')
MAIL="bavduer@163.com"
# 检测vmstat命令是否存在
if ! which vmstat &>/dev/null; then
yum -y install procps-ng &>/dev/null
if [ $? -eq 0 ];then
echo "vmstat already installed"
fi
fi
US=$(vmstat | awk 'BEGIN{ FS=" " }NR==3{ print $13 }')
SY=$(vmstat | awk 'BEGIN{ FS=" " }NR==3{ print $14 }')
ID=$(vmstat | awk 'NR==3{ print $15 }')
WA=$(vmstat | awk 'NR==3{ print $16 }')
ST=$(vmstat | awk 'NR==3{ print $17 }')
useTotal=$((${US}+${SY}))
if [[ ${useTotal} -ge 70 ]];then
echo "
Date: ${DATE}
Host: ${HOSTNAME}: ${IPADDR}
Problem: CPU using rate: ${useTotal}%
" | mail -s "CPU Monitor Warnning" ${MAIL}
fi
[root@control_machine ~]# ansible webserver -m script -a '.ansible/scripts/cpuMonitor.sh'
facts信息
主要用于获取主机信息, 在playbooks里经常会用到的一个参数gather_facts就与该模块相关; 当ansible执行任务时, 受控机会把信息返回给中控机;
# filter可以过滤某些特定的条件
[root@control_machine ~]# ansible webserver -m setup -a 'filter=ansible_hostname'
ansible执行速度优化
/ 优化一: 开启SSH长连接 /
Ansible模式是使用SSH和远程主机进行通信, 所以Ansible对SSH的依赖性非常强, 在OpenSSH 5.6版本以后SSH就支持了Multiplexing, 关于这个特性我们可以参考 Multiplexing手册. 所以如果Ansible中控机的SSH -V版本高于5.6时, 我们可以直接在ansible.cfg文件中设置SSH长连接, 设置参数如下:
ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d
(s/m/h/d/M/Y)
ConrolPersist=5d, 这个参数是设置整个长连接保持时间为5天.
开启此参数后, 通过SSH连接过的设备都会在当前ansible/cp/目录下生成一个socket文件. 也可以通过netstat命令查看, 会发现有一个ESTABLISHED状态的连接一直与远程设备进行着TCP连接
netstat -anptu
/ 优化二: 设置facts缓存 /
如果细心的话, 就会发现执行playbook的时候, 默认第一个task都是GATHERING FACTS, 这个过程就是Ansible在收集每台主机的facts信息. 方便我们在playbook中直接引用facts里的信息. 当然如果你的playbook中不需要facts信息, 可以在playbook中设置gather_facts: False来提高playbook效率.
但是如果我们既想在每次执行playbook的时候都能收集facts, 又想加速这个收集过程, 那么就需要配置facts缓存了. 目前Ansible支持使用json文件存储facts信息. 如下设置示例:/etc/ansible/ansible.cfg // .ansible.cfg
gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile//memory/redis
fact_caching_connection = /dev/shm/ansible_fact
网友评论