·— 只需要SSh和Python即可使用
·— 无客户端
·— Ansible功能强大,模块丰富
·— 基于Python开发,做二次开发容易
·— 使用公司较多,社区活跃
·Ansible实现批量操作系统配置、批量部署、批量运行命令等功能
1)实验用机为5台,一台为管理主机,其余4台位被管理主机
2)在管理主机上配置本地解析
[root@ansible ~]# vim /etc/hosts
192.168.0.25 web1
192.168.0.26 web2
192.168.0.27 web3
192.168.0.28 web4
3)在管理主机生成公私钥
[root@ansible ~]# ssh-keygen
4)将公钥传递到被管理节点
[root@ansible~]# for i in web1 web2 web3 web4
> do
> ssh-copy-id $i
> done
5)测试免密登录
[root@ansible ~]# ssh web1
[root@ansible ~]# ssh web2
[root@ansible ~]# ssh web3
[root@ansible ~]# ssh web4
[root@ansible~]#rm-rf /etc/yum.repos.d/* //清空YUM仓库路径
[root@ansible~]#vim /etc/yum.repos.d/centso.repo//创键仓库文件并指定仓库地址
[centos_file]
name=centos7
baseurl=file:///root/centos7
enabled=1
gpgcheck=0
[epel]
name=ailiyun
baseurl=http://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
[centos]
name=cobbler
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0
2)安装Ansible
[root@ansible ~]# yum -y install ansible
3)查看Ansible版本
[root@ansible ~]# ansible --version
[root@ansible ~]# vim /etc/ansible/ansible.cfg

[root@ansible ~]# vim /etc/ansible/hosts
.. ..
[webserver]//定义组名
192.168.0.25//组内主机
192.168.0.26
192.168.0.27
192.168.0.28
3)查看组内主机
[root@ansible ~]# ansible webserver --list-host
4)测试管理主机与被管理主机联通性
[root@ansible ~]# ansible webserver -m ping //-m 调用模块,ping为模块名
-M 指定模块路径
-m 使用模块,默认为command模块
-a or --args模块参数
-i inventory 文件路径,或脚本文件
-k 使用交互式登录密码
-e 定义变量
-v 详细信息
Ansible模块
-ansible-doc-l //模块手册,相当于shell的man
-ping //测试ssh联通性,ping模块没有参数
-command //默认模块,远程执行命令
-shell //命令模块,和command模块类似,区别在于shell模块通过/bin/bash执行命令
-raw //命令模块,和shell模块类似,区别在于raw没有chdir、create、remove参数
-script //执行脚本模块
-copy //复制模块,将本地文件复制到远程主机
-src //复制远程主机文件到本地
-dest //必须选,远程主机的绝对路径,如果源文件是目录,那改路径必须是目录
-yum //软件包管理模块
[root@ansible ~]# ansible webserver -m shell -a 'systemctl stop firewalld'
2)设置防火墙开机不自起
[root@ansible ~]# ansible webserver -m shell -a 'systemctl disable firewalld'
3)关闭SELinux
[root@ansible ~]# ansible webserver -m shell -a 'setenforce 0'
4)安装httpd服务
[root@ansible ~]# ansible webserver -m shell -a 'yum -y install httpd'
5)启动服务
[root@ansible ~]# ansible webserver -m shell -a 'systemctl start httpd'
6)在本地编写页面执行批量拷贝
[root@ansible ~]# echo "<marquee><font color=red><h1>新盟教育<marquee>" > index.html
7)使用copy模块远程拷贝
[root@ansible ~]# ansible webserver -m copy -a 'src=/root/index.html dest=/var/www/html/'
8)浏览器访问测试
192.168.0.25
- 赞助本站
- 微信扫一扫
-
- 加入Q群
- QQ扫一扫
-
评论