Ansible 概述与模块基本操作

一、Ansible 概述

  • Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具

  • 它用Python写成,类似于saltstack、Puppet、CHef,但是有一个不同和优点是我们不需要在节点中安装任何客户端

  • 它使用SSH来和节点进行通信。Ansible基于 Pthon paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作

  • Ansible官方网站:https://www.ansible.com/,红帽公司于2015年10月收购了ansible,而ansible成立于2013年

  • Ansible能批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作,使用Ansible 只需在固定的一台Ansible控制节点上去完成所有主机的操作

  • 使用者在使用时,在服务器终端输入命令或者playbooks,会通过预定好的规则将playbook拆解为play,再组织成ansible可以识别的任务,调用模块和插件,根据主机清单通过SSH将临时文件发给远程的客户端执行并返回结果,执行结束后自动删除

1.Ansible 的特点

1.部署简单,没有客户端,只需在主控端部署Ansible环境,被控端无需做任何操作

2.模块化:调用特定的模块,完成特定任务

3.默认使用SSH协议对设备进行管理

4.主从集中化管理

5.配置简单、功能强大、扩展性强

6.支持API及自定义模块,可通过Python轻松扩展

7.通过Playbooks来定制强大的配置、状态管理

8.对云计算平台、大数据都有很好的支持

9.具有幂等性:一个操作在一个主机上执行一遍和执行N遍的结果是一样的

2.Ansible 工作机制

Ansible 在管理节点将 Ansible 模块通过 SSH 协议推送到被管理端执行,执行完之后自动删除,可以使用 SVN 等来管理自定义模块及编排

在这里插入图片描述

ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)连接插件connection plugins:负责和被监控端实现通信
(2)host inventory:指定操作的主机,是一个配置文件里面定义监控的主机
(3)各种模块核心模块、command模块、自定义模块
(4)借助于插件完成记录日志邮件等功能
(5)playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务

由图可以看出Ansible的组成由以下模块组成:

Ansible: ansible的核心模块
Host Inventory:主机清单,也就是被管理的主机列表
Playbooks:ansible的剧本,可想象为将多个任务放置在一起,一块执行
Core Modules:ansible的核心模块
Custom Modules:自定义模块
Connection Plugins:连接插件,用于与被管控主机之间基于SSH建立连接关系
Plugins:其他插件,包括记录日志等

二、Ansible 环境安装及部署

服务器名IP安装的服务
管理端192.168.113.125Ansible
被管理端192.168.113.128
被管理端192.168.113.129
#每台机器关闭防火墙核心防护
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0

1.管理端操作(192.168.113.125)

#1.安装ansible
#先安装epel源
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y ansible

#2.查看anisble树形结构
[root@localhost ~]# yum install -y tree
[root@localhost ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg			#ansible的配置文件,一般 无需修
├── hosts				#ansible的主机清单,用于存储需要管理的远程主机的相关信息
└── roles				#公共角色目录

#3.配置主机清单,最后一行添加
[root@localhost ~]# vim /etc/ansible/hosts 
[webservers]       #配置组名
192.168.113.128    #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)

[dbservers]
192.168.113.129

#4.配置密钥对验证
[root@localhost ~]# ssh-keygen -t rsa    #一路回车,使用免密登录

#得先shh登陆下主机在使用下面命令,不然会报错
[root@localhost ~]# ssh 192.168.113.129
[root@localhost ~]# ssh 192.168.113.128

[root@localhost ~]# sshpass -p '123123' ssh-copy-id root@192.168.113.129
[root@localhost ~]# sshpass -p '123123' ssh-copy-id root@192.168.113.128
#sshpass -p是一种免交互方式

#测试免交互
[root@localhost ~]# ssh 192.168.113.129
[root@localhost ~]# ssh 192.168.113.128

三、Ansible 命令行模块

命令格式: ansible <组名> -m <模块> -a <参数列表>
ansible-doc -l      #列出所有已安装的模块,按q退出

1.command 模块

#在远程主机执行命令,不支持管道,重定向等shell的特性
[root@localhost ~]# ansible-doc -s command   #-s 列出指定模块的描述信息和操作动作,按q退出

#指定ip执行date
[root@localhost ~]# ansible 192.168.113.128 -m command -a 'date'

#指定组执行date
[root@localhost ~]# ansible webservers -m command -a 'date'
[root@localhost ~]# ansible dbservers -m command -a 'date'

#all代表所有hosts 主机
[root@localhost ~]# ansible all -m command -a 'date'

#如省略-m 模块,则默认运行command 模块,查看所有主机的目录
[root@localhost ~]# ansible all -a 'ls /'


[root@localhost ~]# ansible all -m command -a "chdir=/home ls ./"
#常用的参数:
chdir: 在远程主机上运行命令前,提前进入目录
creates: 在命令运行时创建一个文件,如果文件存在,则不会创建任务
removes:在命令运行时移除一个文件,如果文件不存在,则不会执行移除任务
executeble:指明运行命令的shell程序

#指定ip执行date

image-20220917164008498

#指定组执行date,all代表所有hosts 主机

image-20220917164200360

image-20220917164220193

image-20220917164408173

2.shell 模块

#在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)
#列出指定模块的描述信息和操作动作,按q退出
ansible-doc -s shell

#指定dbservers组执行shell模块打印出ifconfig ens33第二行第二列,以空格分割截取第二列
[root@localhost ~]# ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'

[root@localhost ~]# ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'

image-20220917171041384

3.cron 模块

#在远程主机定义任务计划。其中有两种状态(state) : present表示添加 (可以省略) ,absent表示移除
ansible-doc -s cron 

#常用参数:
minute/hour/day/month/weekday:分/时/日/月/周
job: 任务计划要执行的命令
name: 任务计划的名称

#指定那个组调用cron模块每一分钟执行一次helloworld并确定目录名字
[root@localhost ~]# ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'

#查看计划任务
[root@localhost ~]# ansible webservers -a 'crontab -l'

#移除计划任务,假如该计划任务没有取名字,name=None即可,再次查看计划任务就没了
[root@localhost ~]# ansible webservers -m cron -a 'name="test crontab" state=absent'

eg:
##10号到20号,每两个小时执行一次hello
ansible webservers -m cron -a 'hour="*/2" day="10-20" job="/bin/echo hello" name="cron hello"'
ansible webservers -a 'crontab -l'

#移除相关效果
ansible webservers -m cron -a 'name="cron helli" state=absent'

image-20220917174449413

image-20220917174823813

image-20220917175010028

4.user 模块

#用户管理的模块
ansible-doc -s user

#常用参数:
#name:用户名,必选参数
#state-present | absent:创建账号或者删除账号,present表示创建,absent表示删除
#system=yes|no:是否为系统账号
#uid:用户uid
#group:用户基本组
#shell:默认使用的shell
#move_home=yes|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动
#password:用户的密码,建议使用加密后的字符串
#comment:用户的注释信息
#remove=yes|no:当state=absent时, 是否删除用户的家目录


#例:指定dbservers组创建用户q1
 [root@localhost ~]# ansible dbservers -m user -a 'name="q1"'
 
#查看是否创建
[root@localhost ~]# ansible dbservers -m command -a 'tail /etc/passwd'

#指定dbservers组删除用户q1
[root@localhost ~]# ansible dbservers -m user -a 'name="q1" state=absent

image-20220917175623727

image-20220917175721953

image-20220917175827539

5.group 模块

#用户组管理的模块
ansible-doc -s group

#创建qjm组
[root@localhost ~]# ansible dbservers -m group -a 'name=qjm gid=1007 system=yes' 

#查看
[root@localhost ~]# ansible dbservers -a 'tail -3 /etc/group'

#新建一个用户添加到qjm组
[root@localhost ~]# ansible dbservers -m user -a 'name=q1 uid=1007 system=yes group=qjm'

#查看是否添加有两种方法
[root@localhost ~]# ansible dbservers -a 'tail -3 /etc/passwd'
[root@localhost ~]# ansible dbservers -a 'id q1'

image-20220917182107820

image-20220917182422444

6.copy 模块

#用于复制指定主机文件到远程主机的
ansible-doc -s copy

常用参数:
#dest:指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
#src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是日录
#mode:指出复制时,目标文件的权限
#owner:指出复制时,目标文件的属主
#group:指出复制时,目标文件的属组
#content:指出复制到目标主机上的内容,不能与src一起使用

#例1:
[root@localhost ~]# ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
[root@localhost ~]# ansible dbservers -a 'ls -l /opt'  #查看权限
[root@localhost ~]# absible dbservers -a 'cat /opt/fstab.bak'  #查看内容

#例2:
[root@localhost ~]# ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'  #将helloworld写入/opt/he1lo.txt文件中
[root@localhost ~]# ansible dbservers -a 'cat /opt/hello.txt'

例1:

image-20220917183716667

image-20220917183731062

image-20220917183742322

例2:
image-20220917183827015

7.file 模块

#设置文件属性
ansible-doc -s file

#常用参数
path:指定路径

例:
#修改文件的属主属组权限
[root@localhost ~]# ansible dbservers -m file -a 'owner=q1 group=qjm mode=644 path=/opt/fstab.bak'

#设置/opt/fstab.link为/opt/fstab.bak的链接文件
[root@localhost ~]# ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'

#查看是否创建
[root@localhost ~]# ansible dbservers -a 'ls -l /opt'

#创建一个文件
[root@localhost ~]# ansible dbservers -m file -a "path=/opt/abc.txt state=touch"

#删除一个文件
[root@localhost ~]# ansible dbservers -m file -a "path=/opt/abc.txt state=absent"

image-20220917185909691

image-20220917185935739

image-20220917190018359

image-20220917190117294

8.hostname 模块

#用于管理远程主机上的主机名
[root@localhost ~]# ansible dbservers -m hostname -a "name=q2"

9.ping 模块

#检测远程主机的连通性
[root@localhost ~]# ansible all -m ping

image-20220917190640145

10.yum 模块

#在远程主机.上安装与卸载软件包
ansible-doc -s yum

#安装服务
[root@localhost ~]# ansible webservers -m yum -a 'name=httpd'

#卸载服务
[root@localhost ~]# ansible webservers -m yum -a 'name=httpd state=absent'

image-20220917191045425

image-20220917191100330

11.service/systemctl 模块

#用于管理远程主机上的管理服务的运行状态
ansible-doc -s service

常用的参数:
#name:被管理的服务名称
#state=started|stopped|restarted:动作包含启动关闭或者重启
#enabled=yes|no:表示是否设置该服务开机自启
#runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动

#查看web服务器httpd运行状态,先安装下前面删掉了
[root@localhost ~]# ansible webservers -m yum -a 'name=httpd'
[root@localhost ~]# ansible webservers -a 'systemctl status httpd'

#启动httpd服务
[root@localhost ~]# ansible webservers -m service -a 'enabled=true name=httpd state=started'

image-20220917191801725

image-20220917191818659

image-20220917191829857

image-20220917191842285

12.script 模块

#实现远程批量运行本地的shell脚本
ansible-doc -S script

[root@localhost ~]# vim test.sh
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt
[root@localhost ~]# chmod +x test.sh 

[root@localhost ~]# ansible webservers -m script -a 'test.sh'
[root@localhost ~]# ansible webservers -a 'cat /opt/script.txt'

image-20220917192324301

13.setup 模块

#facts 组件是用来收集被管理节点信息的,使用setup 模块可以获取这些信息
ansible-doc -s setup

例:
#获取qjm组主机的facts信息
[root@localhost ~]# ansible webservers -m setup 

#使用filter可以筛选指定的facts信息
[root@localhost ~]# ansible dbservers -m setup -a 'filter=*ipv4'

image-20220917192652485

image-20220917192704939

四、inventory 主机清单

  • inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内

#如果是名称类似的主机,可以使用列表的方式标识各个主机
[root@server2 opt]# vim /etc/ssh/sshd_config   #去被管理者机器,17行取消注释,随意更改端口号但要和管理者机器配置的端口号一致(192.168.113.128)
#重启服务
[root@server2 opt]# systemctl restart sshd
[root@server2 opt]# netstat -ntlp|grep ssh  #查看端口是否更改

#管理者机器操作(192.168.113.125)
[root@localhost ~]# vim /etc/ansible/hosts 
[webservers]
192.168.113.128:1007   #冒号后定义远程连接端口,默认是ssh的22端口

------------------------------------------------------------------
192.168.113.12[1:9]    #可访问192.168.113.121-129的机器,只是例子免密交互没做和机器没有那么多
[dbservers]
db-[a:f].example.org    #支持匹配a~f,一样是例子
------------------------------------------------------------------

#测试
[root@localhost ~]# ansible webservers -a 'ls /'  #端口不一样的情况下是查看不了的

image-20220917193803929

image-20220917194458552

#端口号不一致是查看

image-20220917194943613

#一致时查看

image-20220917194638810

image-20220917194858316

1.inventory 中的变量

Inventory变量名含义
ansible_hostansible连接节点时的IP地址
ansible_port连接对方的端口号,ssh连接时默认为22
ansible_user连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户
ansible_password连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效
ansible_ssh_ private_key_file指定密钥认证ssh连接时的私钥文件
ansible_ssh_common_args提供给ssh、sftp、 scp命令的额外参数
ansible become允许进行权限提升
ansible become_ method指定提升权限的方式,例如可使用sudo/ su/runas等方式
ansible become_user提升为哪个用户的权限,默认提升为root
ansible_become_password提升为指定用户权限时的密码

2.主机变量

[root@localhost ~]# vim /etc/ansible/host
[webservers]
192.168.113.128 ansile_port=22 ansible_user=root ansible_password=123123

#测试
[root@localhost ~]# ansible webservers -a 'ls /'

image-20220917200423719

image-20220917200729279

3.组变量

[webservers:vars]     #表示为webservers 组内所有主机定义变量
ansible_user=root
ansible_password=123123

[all:vars]       #表示为所有组内的所有主机定义变量
ansible_port=22

4.组嵌套

[nginx]
192.168.113.126
192.168.113.130
192.168.113.127

[apache]
192.168.113.12[0:3]

[webservers: children]     #表示为webservers 主机组中包含了nginx 组和apache 组内的所有主机
nginx
apache

root@localhost ~]# ansible webservers -a ‘ls /’


[外链图片转存中...(img-YOEFMqx8-1663757091656)]

[外链图片转存中...(img-qLcusiQK-1663757091656)]

### 3.组变量

```bash
[webservers:vars]     #表示为webservers 组内所有主机定义变量
ansible_user=root
ansible_password=123123

[all:vars]       #表示为所有组内的所有主机定义变量
ansible_port=22

4.组嵌套

[nginx]
192.168.113.126
192.168.113.130
192.168.113.127

[apache]
192.168.113.12[0:3]

[webservers: children]     #表示为webservers 主机组中包含了nginx 组和apache 组内的所有主机
nginx
apache
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值