架构——16——Ansible安装及配置(模块)

Ansible简介

Ansible概述

是一个配置管理系统(configuration management system),当下最流行的批量自动化运维工具之一

常用的运维工具:

ssh
puppet(ruby)
ansible(无客户端,中小规模)(python)
saltstack(master-minion) (python)大规模

1000台以下推荐ansible,1000台以上推荐saltstack

Ansible的作用

批量部署,服务安装,日常备份

Ansible官方文档

https://docs.ansible.com/ansible/latest/index.html

Ansible的特性

无客户端软件,通过ssh远程管理
安装后不需要启动服务
依赖大量的Python模块扩展功能
配置文件:/etc/ansible/ansible.cfg

Ansible基础架构

连接插件(connecter plugins):用来连接主机,连接被管理端
核心模块(core modules):连接主机,实现操作,依赖于具体模块来执行
自定义模块:用户自己开发的功能模块
剧本(playbook):将多个任务组合成一个剧本,由ansible自动批量执行
主机清单(host inventory):定义ansible管理的客户端主机范围

Ansible的命令格式:ansible 主机清单名 -m 调用的模块 -a 动作命令

———————————分隔线————————————

实验所需环境:

ansible192.168.1.128
web192.168.1.129
nfs192.168.1.134
rsync192.168.1.135

修改主机名:

hostnamectl set-hostname ansible
bash
hostnamectl set-hostname web
bash
hostnamectl set-hostname nfs
bash
hostnamectl set-hostname rsync
bash

安装ansible

1)先配epel源:

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo	#epel源(扩展包)
[root@ansible ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo	#linux镜像源(组包)

2)安装ansible

[root@ansible ~]# yum -y install ansible
[root@ansible ~]# ansible --version		#查看版本

Ansible的配置:

1)配置清单

[root@ansible ~]# vim /etc/ansible/hosts
[web]
192.168.1.129
[nfs]
192.168.1.134
[rsync]
192.168.1.135

[hao:children]
web
nfs
rsync

2)在ansible上配置ssh秘钥对访问

[root@ansible ~]# ssh-keygen -t rsa			#全部回车
[root@ansible ~]# ssh-copy-id root@192.168.1.129
[root@ansible ~]# ssh-copy-id root@192.168.1.134
[root@ansible ~]# ssh-copy-id root@192.168.1.135
[root@ansible ~]# vim /etc/hosts
192.168.1.129	web
192.168.1.134	nfs
192.168.1.135	rsync

调用常用模块
安装软件
修改配置
创建程序用户和组
创建目录,修改归属和权限
启动服务
挂载
测试

调用模块颜色显示:

橘黄色更改成功
帽绿色没有更改
深红色错误
亮紫色警告

列出所有模块

[root@ansible ~]# ansible-doc --list

1、command

仅支持简单语法命令,但语句中不能包含管道符等复杂元素

[root@ansible ~]# ansible web -m command -a "hostname"
[root@ansible ~]# ansible web -m command -a "useradd zhangsan"
[root@ansible ~]# ansible web -m command -a "echo 123 | passwd --stdin zhangsan"		#错误示例—command仅支持简单语法命令,但语句中不能包含管道符等复杂元素所以,改不了密码

2、shell

command升级版,支持复杂语句,但不支持别名

[root@ansible ~]# ansible web -m shell -a "echo 123 | passwd --stdin zhangsan"

3、yum

[root@ansible ~]# ansible web -m yum -a "name=httpd state=installed"

注释:name 安装的软件包名,多个软件","分开
state 服务状态:

installed,present安装软件包
removed,absent卸载软件包
latest安装最新软件包

4、copy

[root@ansible ~]# ansible hao -m copy -a "src=/etc/hosts  dest=/etc/hosts backup=yes"

注释:

src源文件路径
dest目标文件路径
backup覆盖到目标文件前,是否提前备份
content添加文件内容
group指定属组
owner指定属主
mode指定权限

案例:在ansible上远程配置rsync服务

1)修改rsync配置文件,并传到rsync服务器
[root@ansible ~]# mkdir /etc/ansible/conf
[root@ansible ~]# cd /etc/ansible/conf
[root@ansible conf]# cp /etc/rsyncd.conf ./
[root@ansible conf]# vim rsyncd.conf
uid = nobody
gid = nobody
port 873
address = 192.168.1.135
hosts allow = 192.168.1.0/24
max connections = 4
pid file = /var/run/rsyncd.pid
timeout = 900
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[backup]
        path = /backup
        read only = no
        auth users = rsync_backup
        secrets file = /etc/rsync.password
[root@ansible ~]# ansible rsync -m copy -a "src=rsyncd.conf  dest=/etc/rsyncd.conf backup=yes"
2)启动rsync服务
[root@ansible ~]# ansible rsync -m shell -a "rsync --daemon"
3)创建目录,并赋权,更改属主属组
[root@ansible ~]# ansible rsync -m file -a "path=/backup owner=root group=root recurse=yes mode=777"
4)配置rsync服务器的密码文件:
[root@ansible ~]# ansible rsync -m copy -a "content='rsync_backup:1' dest=/etc/rsync.password owner=root group=root mode=600"
[root@ansible ~]# ansible rsync -m shell -a "ls -l /etc/rsync.password"
[root@ansible ~]# ansible rsync -m shell -a "cat /etc/rsync.password"
5)配置所有服务器的rsync连接密码文件
[root@ansible ~]# ansible hao -m copy -a "content='1' dest=/etc/server.pass owner=root group=root mode=600"
6)测试:备份WEB的httpd.conf配置文件
[root@ansible ~]# ansible web -m shell -a "rsync -avz --password-file=/etc/server.pass /etc/httpd/conf/httpd.conf rsync_backup@rsync::backup"

5、service

[root@ansible ~]# ansible web -m service -a "name=httpd state=stopped enabled=yes"

注释:

name指定服务名
state指定服务运行状态
started开启服务
stopped关闭服务
reloaded重载服务
restarted重启服务
enabled是否开机自启

6、group

在所有清单主机上创建组www,gid 666

[root@ansible ~]# ansible all -m group -a "name=www gid=666"

在所有清单主机删除组www

[root@ansible ~]# ansible all -m group -a "name=www gid=666 state=absent"

7、user

[root@ansible ~]# ansible all -m user -a "name=www"
[root@ansible ~]# ansible web -m shell -a "echo 123 |passwd --stdin www"

8、file

创建目录,并赋权,更改属主属组

[root@ansible ~]# ansible rsync -m file -a "path=/backup owner=root group=root recurse=yes mode=777"

创建文件

[root@ansible ~]# ansible rsync -m file -a "path=/test.txt owner=root group=root state=touch  mode=777"

9、mount

[root@ansible ~]# ansible nfs -m file -a "path=/nfs owner=root group=root recurse=yes mode=777"
[root@ansible ~]# vim exports
/nfs 192.168.1.0/24(rw,sync,no_root_squash)
	
[root@ansible ~]# ansible nfs -m copy -a "src=exports dest=/etc/exports"
[root@ansible ~]# ansible nfs -m service -a "name=nfs state=restarted"
[root@ansible ~]# ansible nfs -m service -a "name=rpcbind state=restarted"

挂载nfs目录到web下的/var/www/html

[root@ansible ~]# ansible web -m mount -a "src=192.168.1.134:/nfs path=/var/www/html fstype=nfs state=mounted"

注释:

state挂载状态
mounted挂载
unmounted卸载

10、script

在ansible上编写任意测试脚本:

[root@ansible ~]# vim test.sh
#!/bin/bash
ifconfig ens33
[root@ansible ~]# chmod +x test.sh 
[root@ansible ~]# ansible web -m script -a "/root/test.sh"
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值