ansible1

大型架构及配置技术

一.环境准备 (六台机器)

 

1.ansible 简介

 ansible是2013年推出的一款IT自动化和DevOps软件,基于python开发,糅合了很多老运维工具的特点,可以实现批量操作系统配置,批量程序部署,批量运行命令等功能

2.ansible可以实现:

自动化部署app

自动化管理配置项

自动化持续交付

自动化(AWS)云服务管理

3.为什么选择ansible

活跃度(社区) ----看用的人多不多

学习成本---简单易学

使用成本----节约大量时间和经历

编码语言------内置Python

性能

使用是否广泛

ansible优点

只需要SSH和Python即可使用

无客户端

ansible功能强大,模块丰富

上手容易,门槛低

基于Python 开发,做二次开发更容易

使用公司较多,社区活跃

ansible 特性

模块化设计,调用特定的模块完成特定任务

基于Python语言实现

PyYAML(半结构化语言)

Jinja2

其模块支持JSON(基于文本的轻量级数据交换格式)等标准输出格式,可以采用任何编程语言重写

 

 软件依赖关系

1.对管理主机

要求python2.6 python2.74

ansible 使用以下模块,都需要安装

paramiko PyYAML JInja2 httplib2 six

2.对于被托管主机

ansible默认通过ssh协议管理机器

被管理主机要开启ssh服务,允许ansible主机登录

在托管节点上也需要安装python2.5或以上版本

如果托管节点上开启了SELINUX,需要安装libselinux-python

4.安装ansible

真机上 mkdier /var/ftp/ansible

cd /linux-soft/04/ansible

cp * /var/ftp/ansible

createrepo    /var/ftp/ansible #生成repodata的目录

ansible虚拟机:

vim /etc/yum.repo.d/local.repo

[local]
name=local
baseurl="ftp://192.168.1.254/ansible"
enabled=1
gpgcheck=0
]#yum -y install ansible

]#ansible --version

主机定义与分组

1.首先检测环境变量ANSIBE_CONFIG变量定义的配置文件

其次检查当前目录下 ~/ansible.cfg 文件

再次检查当前 目录下 ~ansible.cfg 文件

最后检查/etc/ansible/ansible.cfg 文件

/etc/ansible/ansible.cfg是ansible的默认配置文件路径

 ansible 配置文件

inventory 定义托管主机地址配置文件路径名

inventory 制定的配置文件,写入远程主机的地址

 ssh 主机key 验证配置参数

host_key_check=False

如果为False,不需要输入 yes

如果为True,等待输入 yes

1.改ansible配置文件

vim /etc/ansible/ansible.cnf

14 host_key_checking = False

61  host_key_checking = False

97  host_key_checking = False

2.修改主机集合配置文件

vim /etc/ansible/hosts

在最下面添加

[web]
web1
web2
[db]
db1
db2
[other]
cache

如果主机较多的话

db-[001:100] 批量添加

显示添加的主机

ansible web --list-hosts

  hosts (2):
    web1
    web2
ansible all --list-hosts

列出所有主机

批量部署认证文件

ssh-keygen -t rsa -b 2048 -N '' -f key

ssh-copy-id -i key.pub web1   #依次传给所有机器

ssh -i key web1    #依次尝试登录,无需密码

inventory 扩展参数

 rm -rf /root/.ansible/cp/*  清除缓存

ansible all ping -m    #列出ping通的主机

[all:vars]  #变量定义
ansible_ssh_private_key_file="/root/.ssh/key"

[app:children] # 给子组定义   

web

db

[all:vars]
ansible_ssh_private_key_file="/root/.ssh/key"

单独定义 (如果改了web1的ssh端口为222)

ansible all  -m ping   只有web1 不通

[web]

web1         ansible_ssh_port=222   #单独定义
web2

配置文件加载顺序

ansibl  可以自定义配置文件

mkdir myansbile   文件夹

创建配置文件 ansible.cfg  #必须在文件夹里执行

查看 ansible帮助文档

ansible-doc -l 列出所有ansible参数

ansible-doc user       先看 =号参数  必写项   - 号可选参数

1.ping 模块  测试连通性

2.command 模块   --默认模块,远程主机执行命令 (没有bash,所有shell和ssh命令都不能用,不能解析变量)

ansible all -m command -a 'date +"%F %T"'

ansible web -m command -a 'uptime'

command模块注意事项

注:ansible all -m command -a 'ps aux | grep ssh'   #会失败

3.shell模块  执行任意命令

ansible all -m shell -a 'uptime'    #查看所有机器的负载

 4.script 模块

 在本地写脚本,然后用script模块批量执行

 ansible web -m script -a '/root/1.sh'

5.yum 模块

使用 yum包 管理器来管理软件包

name:要进行操作的软件包名字

state:动作(installd,removed)

ansible db -m yum -a 'state=installed name=mariadb-server'

 6.service模块

name:必须项

enabled:是否开机启动

sleep:执行restarted,会在stop和start之间沉睡几秒

state:对当前服务执行启动,停止,重启,重新加载等操作(started,stopped,restarted,reloaded)

ansible db -m service -a 'name=mariadb state=started enabled=yes'  #开启mariadb并设置开机自启

 7.copy模块

复制文件到远程主机

src:复制本地文件到远程主机,绝对路径和相对路径都可,路径为目录是会递归复制.若路径以"/"结尾,只复制包含目录在内的内容

 backup覆盖前先备份文件,备份文件包含时间信息.有两个选项:yes| no

force:若目标主机包含该文件

批量修改配置文件:

ansible db -m shell -a 'sed -i "4alog_bin=mysql-bin" /etc/my.cnf'   #在db主机的/etc/resolv.conf 第四行添加 log_bin=mysql-bin

 ansible db -m shell -a 'sed -i "5abinlog-format=mixed" /etc/my.cnf'

8.lineinfile模块

1.path 目标文件

2.regexp正则表达式,要修改的行

3.line 最终修改的结果

andible db -m lineinfile -a 'path="/etc/my.cnf" regexp=" mixed$ line="binlog-format=row" '

9.replace模块

类似于sed的一种行编辑替换模块

path 目的文件

regexp 正则表达式

replace 替换后的结果

替换制定字符 row

ansible db -m replace -a 'path="/etc/my.cnf" regexp="row" replace="mixed"'

10.setup模块

主要用于获取主机信息,playbooks里面经常会用到的另一个参数gather_facts与该模块相关,setup模块下经常用

到的setup模块下经常用到的是filter参数

ansible cache -m setup -a 'filter=ansible_distribution'

 

posted @ 2019-10-12 12:06  Jeff.zhao0302  阅读(176)  评论(0)    收藏  举报