ansible入门

来源:互联网 发布:ctp交易接口源码 编辑:程序博客网 时间:2024/06/07 18:50

ansible是新出现的自动化运维工具 , 基于Python研发的,还有很重要的是和ssh协议结合起来使用,所以基本上,不用对客户机做什么操作(保证sshd可用就可以)。ansible 比起salt和puppet等,就是非常容易上手,配置方便,命令也很简单。

管理机:fedora23 ip 192.168.1.99 客户机:centos7 ip
客户机:centos7 ip 101.200.160.167 #客户机我这里是实验,只用一台就可以啦,实际上ansible的强大就是批量管理操作

setp1:ssh设置

在客户机上,保证sshd的服务可以用,我直接关闭firewalld和selinux

[root@www ~]# hostnamewww.yeting.com          #主机名[root@www ~]# setenforce 0[root@www ~]# systemctl stop firewalld[root@www ~]# systemctl status sshd            #sshd服务正常运行,默认就是开启的● sshd.service - OpenSSH server daemon   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)   Active: active (running) since 五 2016-04-08 15:35:53 CST; 1 weeks 2 days ago Main PID: 886 (sshd)   CGroup: /system.slice/sshd.service           └─886 /usr/sbin/sshd -D

setp2:在管理机上,制作ssh密钥

[root@yeting ~]# ssh-keygen        #制作ssh密钥[root@yeting ~]# ssh-copy-id  -i /root/.ssh/id_rsa.pub  root@101.200.160.167       #把公钥发送给客户机[root@yeting ~]# ssh root@101.200.160.167      #测试ssh是不是成功,成功后exit退出

setp3:管理机安装ansible

[root@yeting ~]# echo 101.200.160.167 www.yeting.com >>/etc/hosts      #添加客户机域名ip映射[root@yeting ~]# dnf install ansible   #如果没有ansible,可以自己添加ansible的相关源,或者下载源码包安装[root@yeting ~]# vim  /etc/ansible/hosts       #把这里面的内容全部注释掉,或者删除掉,并添加下面的内容[test]      #定义test组,成员为www.yeting.com,可以多个成员,一个一行www.yeting.com  

可以啦,配置完成了,其他的配置文件我们不需要修改

setp4:测试ansible的模块

ansible的管理依赖于模块,一个模块代表ansible的一类功能,下面简单介绍下一些模块的使用

[root@yeting ~]# ansible-doc -l        #查看全部模块[root@yeting ~]# ansible-doc -s ping   #查看指定模块的使用方式

ping模块

[root@yeting ~]# ansible test  -m ping     #先指定对象为test组,-m指定使用的模块The authenticity of host 'www.yeting.com (101.200.160.167)' can't be established.ECDSA key fingerprint is SHA256:85054g6bioCqMapw4VolyWZ21sgVTPph8EfgchftyFM.ECDSA key fingerprint is MD5:94:be:3b:80:5e:a8:17:55:15:f8:5b:0e:ec:fe:01:73.Are you sure you want to continue connecting (yes/no)? yes  #第一次会提示yes/nowww.yeting.com | success >> {               #success,说明ping通啦    "changed": false,     "ping": "pong"}[root@yeting ~]# ansible all  -m ping      #all代表对象为全部客户机www.yeting.com | success >> {    "changed": false,     "ping": "pong"}

command 模块 #-a 后加系统命令

[root@yeting ~]# ansible test -m command -a "touch /tmp/tmp.test"www.yeting.com | success | rc=0 >>#这样就成功在客户机上创建了/tmp/tmp.test[root@www ~]# ls /tmp/tmp.test     #在客户机上检查,确实生成了/tmp/tmp.test/tmp/tmp.test

yum 模块

[root@yeting ~]# ansible test -m yum -a "name=vsftpd"www.yeting.com | success >> {    "changed": false,     "msg": "",     "rc": 0,     "results": [        "vsftpd-3.0.2-11.el7_2.x86_64 providing vsftpd is already installed"    ]}

service 模块

[root@yeting ~]# ansible test -m service -a "name=vsftpd state=started enabled=enable"www.yeting.com | FAILED >> {        #失败    "failed": true,     "msg": "Boolean enable not in either boolean list"}[root@yeting ~]# ansible test -m service -a "name=vsftpd state=started enabled=1"www.yeting.com | success >> {   #执行成功,开启ftp服务,并且开启开机自启    "changed": true,     "enabled": true,     "name": "vsftpd",     "state": "started"}[root@yeting ~]# ansible test -m service -a "name=vsftpd state=stoped enabled=0"www.yeting.com | FAILED >> {        #失败    "failed": true,     "msg": "value of state must be one of: running,started,stopped,restarted,reloaded, got: stoped"}[root@yeting ~]# ansible test -m service -a "name=vsftpd state=stopped enabled=0"www.yeting.com | success >> {       #执行成功,关闭ftp服务,并且取消开启自启    "changed": true,     "enabled": false,     "name": "vsftpd",     "state": "stopped"}#有时候我们执行失败的时候,可以ansible-doc -s 模块名 看出该模块的帮助

还有copy,file,script等模块都很常用,大家可以自己去了解下。

0 0
原创粉丝点击