Ansible的使用
介绍:
Ansible是为了更方便、快捷的进行配置管理。用Ansible能够将日常复杂的配置工做变得简单,更加标准化且更容易控制。Ansible能够实现100、1000台批量部署等。
Ansible特色:
(1)部署简单,只需在主控端部署 Ansible 环境,被控端无需作任何操做。(Ansible只须要在一台普通的服务器上运行便可,不须要在被管控的服务器上安装客户端)
(2)使用 SSH协议对设备进行管理。
(3)使用python编写的,维护更简单
操做:
Centos7安装Ansiblehtml
[root@mail ~]# yum install ansible –y rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
#镜像源,能够先按装镜像源,再安装ansible,这里是直接安装的。
python
安装完后,ansible的默认配置文件路径为
[root@mail ~]# ls /etc/ansible
ansible.cfg hosts rolesshell
[root@mail ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): yes
[root@mail ~]# cd /root/.ssh [root@mail .ssh]# ls authorized_keys id_rsa id_rsa.pub known_hosts [root@mail .ssh]# ssh-copy-id -i id_rsa.pub 111.231.144.197 #将生成的公钥拷贝到远程机器上
4.Ansible的简单使用vim
[root@mail ~]# cd /etc/ansible/ [root@mail ansible]# ls ansible.cfg hosts roles [root@mail ansible]# vim hosts
##默认hosts中能够配置分组,咱们能够定义各类ip及规则。在hosts中添加如下内容:
[manage-other]
111.231.144.197
127.0.0.1服务器
在命令行执行如下命令,查看磁盘使用状况:运维
[root@mail ansible]# ansible manage-other -m shell -a 'df -h' #查看hosts文件中自定义组中机器的磁盘使用状况 [root@mail ansible]# ansible all -m shell -a 'df -h' #查看hosts文件中全部组中机器的磁盘使用状况
命令:ansible 分组 -m 模块名 -a 模块参数
(1)在远程的机器上执行命令ssh
[root@mail ansible]# ansible manage-other -m shell -a uptime [root@mail ansible]# ansible manage-other -m command -a uptime
(2)在远程主机上执行主控端的shell脚本(array.sh在ansible所在的机器上)相似scp+shell命令。[root@mail SHELL]# ansible manage-other -m script -a array.sh
ide
(3)实现主控端向目标主机拷贝文件(ansible所在机器向所控制的远程机器拷贝文件,相似scp命令)工具
[root@mail SHELL]# ansible manage-other -m copy -a "src=array.sh dest=/root/array.sh" [root@mail SHELL]# ll /root/array.sh -rw-r--r-- 1 root root 323 Oct 25 13:01 /root/array.sh
(4)实现主控端向目标主机拷贝文件(ansible所在机器向所控制的远程机器拷贝文件,相似scp命令)而且修改文件的权限
[root@mail SHELL]# ansible manage-other -m copy -a "src=array.sh dest=/root/array.sh owner=root group=root mode=777"url
[root@mail SHELL]# ll /root/array.sh -rwxrwxrwx 1 root root 323 Oct 25 13:01 /root/array.sh #权限发生改变 注意:远程主机的用户存在和组存在,不然拷贝失败。
(5)实如今远程主机下载指定url内容到远程主机上[root@mail SHELL]# ansible manage-other -m get_url -a "url=http://www.baidu.com dest=/root/index.html"
总结:command:在远程执行权限内的shell命令.script:在远程主机执行控制端的脚本文件.shell:在控制端执行远程主机上的shell脚本文件.