centos 6.5 ansible的安装与使用方法

实验环境:

centos 6.5 x64

安装epel 源:

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

安装ansible 服务端:

yum install ansible -y

在配置文件中添加主机

vim /etc/ansible/hosts

[testhost]
192.168.28.70

测试所有连接的客户端:

[root@localhost ~]# ansible all -a 'who'
192.168.28.70 | SUCCESS | rc=0 >>
root pts/0 2017-07-11 04:13 (192.168.28.186)
root pts/2 2017-07-11 04:18 (192.168.28.186)
root pts/1 2017-07-11 04:45 (192.168.28.186)

测试一个简单的 ping:

[root@localhost ~]# ansible all -m ping
192.168.28.70 | SUCCESS => {
 "changed": false, 
 "ping": "pong"

远程执行命令

[root@localhost ~]# ansible testhost -m command -a 'w'

未分类

注意:”-m” 指定模块名,”-a” 指定相应命令,这样就可以批量执行命令。这里的 testhost 为之前自定义的主机组名。当然我们也可以直接写一个 ip,针对某一台机器来执行命令。如下:

未分类

远程执行shell脚本

1、创建一个shell脚本

[root@localhost ~]# cat /tmp/test.sh 
#!/bin/bash
 echo `date` > /tmp/ansible_shell.log

2、把脚本分发到远程主机

[root@localhost ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
192.168.28.70 | SUCCESS => {
 "changed": true, 
 "checksum": "032e736ac2c71a85c09cbef25190e404aa7eb7e8", 
 "dest": "/tmp/test.sh", 
 "gid": 0, 
 "group": "root", 
 "md5sum": "874449f8733ff8aaece2a5859b0d4446", 
 "mode": "0755", 
 "owner": "root", 
 "secontext": "unconfined_u:object_r:admin_home_t:s0", 
 "size": 53, 
 "src": "/root/.ansible/tmp/ansible-tmp-1500430217.04-122496835634479/source", 
 "state": "file", 
 "uid": 0
}

3、执行脚本

[root@localhost ~]# ansible testhost -m shell -a "/tmp/test.sh"
192.168.28.70 | SUCCESS | rc=0 >>

拷贝文件

[root@localhost ~]# ansible testhost -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansibletet.txt owner=root group=root mode=0644" 
192.168.28.70 | SUCCESS => {
 "changed": true, 
 "checksum": "bd6fddebe99a0a92d02e9e198d34c406186edc87", 
 "dest": "/tmp/ansibletet.txt", 
 "gid": 0, 
 "group": "root", 
 "md5sum": "80f6c7c933dd1ca1c626ebffa3ddb8ed", 
 "mode": "0644", 
 "owner": "root", 
 "secontext": "unconfined_u:object_r:admin_home_t:s0", 
 "size": 18066, 
 "src": "/root/.ansible/tmp/ansible-tmp-1500430393.64-228538002701013/source", 
 "state": "file", 
 "uid": 0
}

拷贝目录

[root@localhost ~]# ansible testhost -m copy -a "src=/etc/ansible/ dest=/tmp/ansibletest owner=root group=root mode=0644"
192.168.28.70 | SUCCESS => {
 "changed": true, 
 "dest": "/tmp/ansibletest/", 
 "src": "/etc/ansible"
}

添加计划任务

[root@localhost ~]# ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/test.txt' hour='1,5,10' weekday=1"
192.168.28.70 | SUCCESS => {
 "changed": true, 
 "envs": [], 
 "jobs": [
 "test_cron"
 ]
}

去远程主机上查看

[root@bogon ~]# crontab -l
#Ansible: test_cron
* 1,5,10 * * 1 /bin/touch /tmp/test.txt

删除任务计划

若要删除该cron ,只需要加一个字段 state=absent

[root@localhost etc]# ansible testhost -m cron -a "name='test_cron' state=absent"
192.168.28.70 | SUCCESS => {
 "changed": true, 
 "envs": [], 
 "jobs": []
}

Yum 安装

我们来安装一个httpd服务

[root@localhost etc]# ansible testhost -m yum -a "name=httpd"

服务管理

开启 httpd服务,并关闭开机启动

[root@localhost etc]# ansible testhost -m service -a "name=httpd state=started enabled=no"

列出ansible 所有模块

[root@localhost etc]# ansible-doc -l

查看指定模块的用法

[root@localhost etc]# ansible-doc cron

Ansible安装配置zabbix-agent

Ansible Role: zabbix-agent

安装zabbix客户端

介绍

zabbix(音同 z?bix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。zabbix agent可以运行在Linux,Solaris,HP-UX,AIX,Free BSD,Open BSD, OS X, Tru64/OSF1, Windows NT4.0, Windows (2000/2003/XP/Vista)等系统之上。

官方地址: https://www.zabbix.com
官方文档地址:https://www.zabbix.com/documentation/3.2/manual

要求

此角色仅在RHEL及其衍生产品上运行。

测试环境

ansible 2.3.0.0
os Centos 6.7 X64
python 2.6.6

角色变量

software_files_path: "/opt/software"
software_install_path: "/usr/local"

zabbix_agent_version: "3.2.6"

zabbix_agent_file: "zabbix-{{ zabbix_agent_version }}.tar.gz"
zabbix_agent_file_path: "{{ software_files_path }}/{{ zabbix_agent_file }}"
zabbix_agent_file_url: "https://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/{{ zabbix_agent_version }}/{{ zabbix_agent_file }}"

zabbix_agent_repo_url: "http://repo.zabbix.com/zabbix/3.2/rhel/6/x86_64/zabbix-release-3.2-1.el6.noarch.rpm"
zabbix_agent_packages:
  - "zabbix-agent-{{ zabbix_agent_version }}-1.el6"

zabbix_agent_user: zabbix
zabbix_agent_group: zabbix

zabbix_agent_install_from_source: false

zabbix_agent_source_dir: "/tmp/{{ zabbix_agent_file | replace('.tar.gz','') }}"
zabbix_agent_source_configure_command: >
  ./configure
  --prefix={{ software_install_path }}/zabbix-{{ zabbix_agent_version }}
  --enable-agent

zabbix_agent_conf_path: "/etc/zabbix" 
zabbix_agent_logs_path: "/var/log/zabbix"

zabbix_agent_hostname: "{{ ansible_hostname | d() }}"
zabbix_agent_server_host: "127.0.0.1"

依赖

None

github地址

https://github.com/kuailemy123/Ansible-roles/tree/master/zabbix-agent

Example Playbook

---
# 源码安装
- hosts: node2
  roles:
   - { role: zabbix-agent, zabbix_agent_install_from_source: true, zabbix_agent_server_host: "192.168.77.130" }

# rpm包安装
- hosts: node3
  roles:
   - { role: zabbix-agent, zabbix_agent_server_host: "192.168.77.130" }

使用

~]# /etc/init.d/zabbix-agent 
Usage: /etc/init.d/zabbix-agent {start|stop|status|restart|help}

    start        - start zabbix_agentd
    stop        - stop zabbix_agentd
    status        - show current status of zabbix_agentd
    restart        - restart zabbix_agentd if running by sending a SIGHUP or start if not running
    help        - this screen