首页 > 技术文章 > ansible的介绍和一些基本模块介绍

liu1026 2017-10-09 09:45 原文

必须保证ansible工作站与各个node实现无密码ssh登入

①:192.168.1.100 - 在你本地的工作站或服务器上安装 Ansible。

 
②:文件服务器1到代理服务器3 - 使用 192.168.1.100 和 Ansible 来自动管理所有的服务器。
 
③:SSH - 在 192.168.1.100 和本地/远程的服务器之间设置 SSH 密钥。
 
 

ssh-keygen -t rsa -p " "   生成密钥

ssh-copy-id  -i  .ssh/id_rsa.pub 172.16.19.1.1 分发密钥

 

 

(一)Ansible 入门

官网:https://www.ansible.com
官方文档:http://docs.ansible.com/ansible/latest/index.html

一: ansible介绍

ansible是由 Python 编写的强大的配置管理解决方案,ansible 的特点就在于它的简洁与高效率
ansible与其他的配置管理工具不同点在于:不需要你在想要配置的每个节点上安装自己的组件,也就是说,ansible管理各个节点不需要依赖于agent端

 

自动运维工具 
(1)puppet (ruby) 各个节点上运行一个agent端的程序 
(2)saltstack (Python) 
(3) ansible (Python)

运维工作的发展历程:

写脚本 ==> 使用自动化运维工具 ==> 自动运维平台

ansible workplace 实现各个节点的管理。

zabbix: agent端,通过执行对于的操作,在节点的本地获取需要采集的数据
将数据返回给zabbix server
 
ansible是通过ssh服务连接到各个node,各个node而言,不需要有任何的agent端,只需要开启
sshd服务,ansible可以通过ssh协议,连接到各个node,基于免密码登入
 
ansible服务端:ansible、配置好ssh免密码登入

ansible:主程序

ansible-doc :模块文档接口程序
ansible-playbook:执行playbook所使用的程序
 
 
ansible连接各个主机使用ssh协议连接
 

二:ansible核心模块介绍

(一)ping (ping模块,用于确认和对象机器之间是否能够ping通,正常情况会返回pong )

ansible 172.16.19.114 -m ping
 
 

(二)command (command 模块用于运行系统命令,比如echo hello, 你安装在系统里的python,或者make 一类) 
Executes a command on a remote node

[root@7 ansible]# ansible webservers -m command -a "ifconfig"
 
 

(三)shell (这个是一个很神奇的模块,它也是ansible的核心模块之一。可以让另外一台主机执行命令)

ansible 172.16.19.249 -m shell -a "echo '123456' | passwd --stdin uplooking"
 
 

(四)copy (copy模块在ansible里的角色就是把ansible执行机器上的文件拷贝到远程节点上。 )

ansible dbservers -m copy -a "src=/root/hello dest=/root/nihao"
ansible 172.16.19.249 -m copy -a "src=/root/hello dest=/root/hello owner=uplooking group=uplooking mode=777"

 

(五)cron (用于管理任务计划,在一段时间内把所得到的数据输入到 /dev/null)

ansible all -m cron -a "minute=*/30 job='/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null'"

 

(六)fetch (从远程节点上拷贝文件到ansible执行机器上)

ansible 172.16.19.249 -m fetch -a "src=/etc/profile dest=/tmp"

 

(七)file (改变一个文件的主,组 ,权限)

ansible 172.16.19.249 -m file -a "path=/root/nihao owner=uplooking group=uplooking mode=777"
ansible 172.16.19.249 -m file -a "path=/root/ops state=directory"

 

(八)hostname (更改远程节点的用户名)

ansible 172.16.19.248 -m hostname -a "name=node3"

 

(九)pip (在远程节点上安装一个jinjia2的模块)

ansible 172.16.19.249 -m pip -a "name=jinja2"

 

(十)yum (使用yum包管理器来管理软件包)

ansible webservers -m yum -a "name=httpd state=latest" (安装)
ansible webservers -m yum -a "name=httpd state=absent" (=removed移除vb)

 

(十一)service (管理服务器 ,enables=yes 是开机自启)

ansible webservers -m service -a "name=httpd state=started enabled=yes"

 

(十二)user (user模块是请求的是useradd, userdel, usermod三个指令 createhome:创建home目录 ,system : 系统用户 , shell:不允许登录)

ansible 172.16.19.246 -m user -a "name=home1 createhome=no uid=4321 system=yes shell=/sbin/nologin"

 

(十三)group (goup模块请求的是groupadd, groupdel, groupmod 三个指令)

ansible 172.16.19.246 -m group -a "name=uplooking gid=3120"

 

(十四)setup (setup模块,主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。)

ansible 172.16.19.246 -m setup

 

(十五)script (scripts模块可以在本地写一个脚本,然后在远程服务器上执行)

ansible 172.16.19.246 -m script -a "/root/useradd.sh"

 

(十六)template (template使用了Jinjia2格式作为文件模版,进行文档内变量的替换的模块)

/mytemplates/foo.j2文件经过填写参数后,复制到远程节点的/etc/file.conf,文件权限相关略过
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644
 
跟上面一样的效果,不一样的文件权限设置方式
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode="u=rw,g=r,o=r"

 

(十七)unarchive (用于解压文件)

ansible 172.16.19.246 -m unarchive -a "src=/root/wordpress.zip dest=/tmp"

 

(十八)mount (配置挂载点)

ansible 172.16.19.246 -m mount -a "path=/var/www/html src=172.16.19.246:/data/static fstype=nfs state=mounted"
 
 
 
 
 
 
 
 

推荐阅读