ansible部署应用

1.1 Ansible是什么

Ansible 简单的说是一个配置管理系统(configuration management system)。你只需要可以使用 ssh 访问你的服务器或设备就行。它也不同于其他工具,因为它使用推送的方式,而不是像 puppet 等 那样使用拉取安装agent的方式。你可以将代码部署到任意数量的服务器上!

1.2 Ansible能做什么

ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作。
比如:同时在100台服务器上安装nginx服务,并在安装后启动它们。
比如:将某个文件一次性拷贝到100台服务器上。
比如:每当有新服务器加入工作环境时,你都要为新服务器部署某个服务,也就是说你需要经常重复的完成相同的工作。
这些场景中我们都可以使用到ansible。

1.3 Ansible能做什么

模块化:调用特定的模块,完成特定任务有PyYAML,Jinja2(模板语言)三个关键模块支持自定义模块;基于Python语言实现,部署简单,基于python和SSH(默认已安装),agent less
安全,基于OpenSSH,支持playbook编排任务,幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况;无需代理不依赖PKI(无需ssl)可使用任何编程语言写模块,YAML格式,编排任务,支持丰富的数据结构,较强大的多层解决方案。

2、Ansible实训环境

服务器清单
服务器系统 角色 IP
Centos7 x86_64 ansible 192.168.100.100
Centos7 x86_64 host1 192.168.100.101

+++++++ +++++++++++++++++++++++ 我是分割线 +++++++++++++++++++++++++++++++++++++

由于ansible主机和被控制的是已经绑定好了的,所以我们需要全部配置静态ip,改完后要,重启网卡才生效。

这里我用的NAT,然后配置的静态,不是很了解虚拟机的三大网络模式的,可以看看我的另一篇文章,

非常简单易懂点击我查看
在这里插入图片描述

另一台也是一样的,配置

在这里插入图片描述

然后是为了区分主机,给主机改名

在这里插入图片描述
在这里插入图片描述

(2)、设置SSH免密登陆

ansible主机
[root@ansible ~]# ssh-keygen -t rsa(执行后一直按回车就行)
[root@ansible ~]# ssh-copy-id ansible主机(输入yes,然后输入目标主机密码)
[root@ansible ~]# ssh-copy-id host1主机(输入yes,然后输入目标主机密码)

(3)安装ansile软件

[root@ansible ~]#yum install epel-release
[root@ansible ~]#yum -y install ansible

说明:

安装ansible软件只需在ansible控制主机上安装即可,其他主机不用安装。

注:这里我是用的阿里的yum(前提是虚拟机要能连网)

Centos的yum源更换为国内的阿里云源
1、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3、之后运行命令生成缓存
yum makecache

在这里插入图片描述
在这里插入图片描述

1) 、安装验证

安装成功后,可以使用下面命令检查Ansible的安装版本:
[root@ansible ~]# ansible --version

在这里插入图片描述

2)、配置主机

Inventory文件通常用于定义要管理的主机的认证信息,例如ssh登录用户名、密码以及key相关信息。可以同时操作一个组的多台主机,组与主机组之间的关系都是通过inventory文件配置。配置文件路径为:/etc/ansible/hosts
[root@ansible ~]# vim /etc/ansible/hosts
主机+端口+密钥
[webserver] 只是一个组名,随便取名,组名下面就放要管理的主机
192.168.223.110
192.168.223.111
在这里插入图片描述

2) 、模块介绍

Ansible配置文件分析
/etc/ansible/ansible.cfg
Ansible模块源文件
/usr/lib/python2.7/site-packages/ansible/modules/packaging/os

4、ansible基础模块应用

(1)、ping模块
#ansible all -m ping (这里我用的all,表示所有组,不同组可以存放不同作用的主机)
说明:
all 表示对所有主机执行操作
-m 模块参数
ping 模块调用
在这里插入图片描述

(2)、setup模块

获取节点详细信息,包括硬件和软件信息,如主机IP、环境变量等。
[root@ansible ~]# ansible web -m setup |more
在这里插入图片描述
(3)、copy
ansible all -m copy -a 'dest=/opt/ src=/root/test.txt ’
说明:
-m 模块
-a 参数
dest 目标地址
src 原地址文件
在这里插入图片描述
(4)file模块
新建文件
[root@ansible ~]# ansible all -m file -a “path=/root/testfile.txt state=touch”
说明:
-m 模块
-a 参数
path= 路径
state= 命令
在这里插入图片描述
新建目录
[root@ansible ~]# ansible all -m file -a “path=/root/testfile state=directory”
说明:
-m 模块
-a 参数
path= 路径
state= 命令
在这里插入图片描述
删除目录
[root@ansible ~]# ansible all -m file -a “path=/root/testfile state=absent”
在这里插入图片描述
(5)、command与shell命令使用
command模块
[root@ansible ~]# ansible web -m command -a ‘ls /opt’

这里要特别强调一下,ansible服务不能识别 ll 这种简写命令,应该写他的全称 ls -l
注意command不通过管道
在这里插入图片描述
Shell模块
[root@ansible ~]# ansible all -m shell -a “cat /etc/passwd |tail -n5”
而shell模块可以通过管道
在这里插入图片描述
[root@ansible ~]# ansible all -m shell -a “ps -ef |grep 22”
在这里插入图片描述
(6)script模块
script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。
[root@ansible ~]# ansible web -m script -a “usertest.sh”

这里需要脚本,我们就先简单的写个shell脚本
在这里插入图片描述
在这里插入图片描述
(7)、group模块
[root@ansible ~]# ansible web -m group -a ‘gid=2020 name=test’
删除
[root@ansible ~]# ansible web -m group -a ‘gid=2020 name=test state=absent’

在这里插入图片描述
验证
[root@ansible ~]# ansible web -m shell -a ‘cat /etc/group’
在这里插入图片描述
(8)user模块
[root@ansible ~]# ansible web -m user -a ‘name=test11’
在这里插入图片描述
删除
[root@ansible ~]# ansible web -m user -a ‘name=test11 state=absent’

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章