平台Centos 6.5 x86_64html
ansible是一款用Python开发的自动化运维部署工具,ansible没有采用C/S的架构,而是基于ssh协议、去中心化的管理方式,非常不错。^_^
1,安装
yum install -y epel-releasemysql
yum update -ylinux
yum install -y ansiblesql
2,设置管理机ssh免密码登陆节点机
管理机上执行以下命令,随后连敲两次空格键默认在/root/.ssh目录下生成密钥文件id_rsa和公钥文件id_rsa.pub,确保/root/.ssh/authorized_keys的权限为600
ssh-keygen -t rsa shell
在管理机上将本身的公钥拷贝到集群节点机上
ssh-copy-id -i .ssh/id_rsa.pub root@10.11.2.7bash
3.ansible命令格式
ansible中的临时命令的执行是经过Ad-Hoc来完成,可以快速执行,并且不须要保存执行的命令。
#命令格式:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
#例如:
ansible all -m copy -a 'src=/etc/my.cnf dest=/etc/'
#几个重要参数的含义:
-i #指定inventory文件(主机定义文件)
all #表示在host文件中定义的全部的主机,能够替换成响应的组名或IP地址
#针对于主机可使用匹配规则(全部的匹配都基于配置文件中的主机)
IP地址: ansible 192.168.239.132
IP匹配: ansible 192.168.239.*
IP匹配: ansible *
组匹配: ansible 组名:&hostname <表示这个组跟其余组的一个主机>
组匹配: ansible 组名:!hostname <表示这个组可是出除去这个组的这个主机>
#相似的匹配还不少,几乎能想到的匹配都能支持,具体参照http://docs.ansible.com/intro_patterns.html
-m #指定使用哪一个模块,默认采用command模块
-a #指定模块的参数,每一个模块都有相应的模块参数
-u #指定远端机器的用户架构
4.ansible模块帮助运维
ansible-doc l #查看模块列表
ansible-doc copy #查看copy模块的详细信息
ansible-doc script #查看script模块的详细信息ssh
经常使用的模块有ping,copy,shell,command等等。工具
command <执行linux命令的>
ansible all -m command -a 'ifconfig'
copy <实现文件复制>
ansible all -m copy -a 'src=/etc/my.cnf dest=/etc/my.cnf owner=mysql group=mysql' owner group 能够根据选择自定义的决定要不要指定
file <实现目录文件权限的修改/建立与删除>
ansible all -m file -a 'dest=/etc/rsync.d/secrets mode=600 owner=root group=root'
ansible all -m file -a 'dest=/data/install mode=755 owner=root grou=root state=directory' #建立这个不存在的目录
ansible all -m file -a 'dest=/data/tmp state=absent' #删除这个目录
yum <使用yum进行软件包的管理>
ansible all -m yum -a 'name=httpd state=installed'
service <管理linux系统服务>
ansible all -m service -a 'name=httpd state=start'
state选项主要有:start,restart,stop
cront <管理linux计划任务>
ansible all -m cron -a 'name="you autoupdate" weekday="2" minute=0 hour=12 user="root" job="/usr/sbin/yum-autoupdate" cron_file=ansible_yum-autoupdate
5,测试并开始管理节点机
ansible all -m ping
ansible all -m copy -a "src=/root/log.sh dest=/root owner=root group=root mode=755"
ansible all -m shell -a "/root/log.sh"
ansible all -m shell -a 'netstat -anl | grep 3306'
用ansible playbook简单安装zabbix客户端
1,创建playbook用到的目录
mkdir -p /etc/ansible/roles/zabbix-agent/{defaults,files,handlers,meta,tasks,templates,vars}
cd /etc/ansible
├── ansible.cfg
├── hosts
├── roles
│ └── zabbix-agent
│ ├── defaults
│ ├── files
│ │ └── install_zabcli.sh
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
└── zabbix-agent.yml
2,vi zabbix-agent.yml
- hosts: client
remote_user: root
roles:
- zabbix-agent
3,vi main.yml
- name: copy install_shell to client
copy: src=install_zabcli.sh dest=/root/install_zabcli.sh owner=root group=root
- name: install zabbix-agent
shell: /bin/bash /root/install_zabcli.sh
4,vi install_zabcli.sh
#!/bin/bash
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-agent-3.2.7-1.el7.x86_64.rpm
IP=`ip addr | grep "global eth0"| awk '{print $2}'| awk -F "/" '{print $1}'`
sed -i 's@Server=127.0.0.1@Server=10.46.209.103@' /etc/zabbix/zabbix_agentd.conf
sed -i "s@# ListenIP=0.0.0.0@ListenIP=$IP@" /etc/zabbix/zabbix_agentd.conf
sed -i 's@erverActive=127.0.0.1@ServerActive=10.46.209.103@' /etc/zabbix/zabbix_agentd.conf
sed -i 's@# UnsafeUserParameters=0@UnsafeUserParameters=1@' /etc/zabbix/zabbix_agentd.conf
echo "UserParameter=netstat[*], ss -nat | grep -c $1" >> /etc/zabbix/zabbix_agentd.conf
service zabbix-agent start
5,cd /etc/ansible
ansible-playbook zabbix-agent.yml 执行远程安装
PLAY [client] ****************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [10.25.101.187]
TASK [zabbix-agent : copy install_shell to client] ***************************************************************************************************
changed: [10.25.101.187]
TASK [zabbix-agent : install zabbix-agent] ***********************************************************************************************************
changed: [10.25.101.187]
PLAY RECAP *******************************************************************************************************************************************
10.25.111.187 : ok=3 changed=2 unreachable=0 failed=0
6,验证客户端安装成功以下: [root@i ~]# egrep -v "^#|^$" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=10.46.209.103 ListenIP=$IP SServerActive=10.46.209.103 Hostname=Zabbix server Include=/etc/zabbix/zabbix_agentd.d/*.conf UnsafeUserParameters=1 UserParameter=netstat[*], ss -nat | grep -c