centos 6.5上ansible初探

平台Centos 6.5 x86_64

ansible是一款用Python开发的自动化运维部署工具,ansible没有采用C/S的架构,而是基于ssh协议、去中心化的管理方式,很是不错。^_^
1,安装
yum install -y epel-release

yum update -y

yum install -y ansible

2,设置管理机ssh免密码登录节点机 
管理机上执行如下命令,随后连敲两次空格键默认在/root/.ssh目录下生成密钥文件id_rsa和公钥文件id_rsa.pub,确保/root/.ssh/authorized_keys的权限为600
ssh-keygen -t rsa 

在管理机上将自己的公钥拷贝到集群节点机上
ssh-copy-id -i .ssh/id_rsa.pub root@10.11.2.7


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模块的详细信息

常用的模块有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 

转载于:https://my.oschina.net/u/2404183/blog/740627

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值