Ansible 常用模块

Ansible

介绍

Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功能,Ansible基本上都可以实现。

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

Ansible是基于模块工作的,它只是提供了一种运行框架,它本身没有完成任务的能力,真正执行操作的是Ansible的模块, 比如copy模块用于拷贝文件到远程主机上,service模块用于管理服务的启动、停止、重启等。

Ansible其中一个比较鲜明的特性是Agentless,即无Agent的存在,它就像普通命令一样,并非C/S软件,也只需在某个作为控制节点的主机上安装一次Ansible即可,通常它基于ssh连接来控制远程主机,远程主机上不需要安装Ansible或其它额外的服务。

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

Ansible的另一个比较鲜明的特性是它的绝大多数模块都具备幂等性(idempotence)。所谓幂等性,指的是无论执行多少次同样的运算,结果都是相同的,即一条命令,任意多次执行所产生的影响均与一次执行的影响相同。比如执行 systemctl stop xxx 命令来停止服务,当发现要停止的目标服务已经处于停止状态,它什么也不会做, 所以多次停止的结果仍然是停止,不会改变结果,它是幂等的,而 systemctl restart xxx 是非幂等的。

Ansible的很多模块在执行时都会先判断目标节点是否要执行任务,所以,可以放心大胆地让Ansible去执行任务,重复执行某个任务绝大多数时候不会产生任何副作用。

 

 

 

 

 

 

 #密钥

 

 #做免密

sshpass -p '123' ssh-copy-id -o StrictHostKeyChecking=no root@192.168.19.25

 

 #支持3387个模块

COMMAND模块

 

 

 

 

 

 

 

 

SHELL 模块

 

ansible dbservers -m shell -a 'ifconfig ens33 | awk "MR==2{print}"'

ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'

 shell支持 | 和重定向

 

CRON模块

ansible dbservers -m cron -a 'minute=30 hour=2 weekday=2 job="/bin/cp -f /var/log/messages /opt" name="backup syslog"'

 #dbservers 生成计划任务

 

ansible dbservers -m cron -a 'name="backup syslog" state=absent'

 

 

ansible dbservers -m cron -a 'name="cp syslog" job="/bin/cp -f /var/log/messages /opt/" minute=10 hour="10,22" day=10 month="*/2"'

 

 #删除

ansible dbservers -m cron -a 'name="cp syslog" state=absent'

 

USER模块

 

常用的参数:
name:用户名,必选参数
state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除
system=yes|no:是否为系统账号
uid:用户uid
group:用户基本组
groups: 用户所属附加组
shell:默认使用的shell
create_home=yse|no: 是否创建家目录
password:用户的密码,建议使用加密后的字符串
remove=yes|no:当state=absent时,是否删除用户的家目录

 

ansible dbservers -m user  -a 'name=zhangsan uid=9527 groups=wheel'

 

 

 

ansible dbservers -m user  -a 'name=nginx create_home=no shell=/sbin/nologin'

 

 

 #删除nginx用户

 

 

ansible dbservers -m user -a 'name=liqiye password=abcabc123'

 

 

 #密码显示春来不安全,加密

 

ansible dbservers -m shell -a 'echo abcabc123 | passwd --stdin liqiye'

 #密码以加密

 

 #创建的用户都有家目录,删除只能删除用户,而家目录的用户还在

 

ansible]# ansible dbservers -m user  -a 'name=liqiye state=absent remove=yes'

 

 

#liqiye用户及家目录已删除

group 模块

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

 

 

 

ansible dbservers -m group -a 'name=yyq gid=1314'

 

 

ansible dbservers -m group -a 'name=mysql gid=306 system=yes'    #创建mysql组
ansible dbservers -a 'tail /etc/group'
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql'    #将test01用户添加到mysql组中
ansible dbservers -a 'tail /etc/passwd'
ansible dbservers -a 'id test01'    

 

copy 模块

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

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

 

 

 

ansible dbservers -m copy -a 'src=/etc/passwd dest=/opt/passwd.txt'

 

 

 

 #创建个用户

 

ansible dbservers -m copy -a 'src=123.txt dest=/opt/passwd.txt mode=777 owner=liqiye group=yyq'

 

 

ansible dbservers -m copy -a 'src=/etc/yum.repos.d dest=/opt/rh'

 

 

 

 #将目录下内容跟复制下来

 #将helloworld写入/opt/hello.txt文件中

ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'

 

 

file 模块

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

 #修改文件的属主属组权限等

ansible dbservers -m file -a 'path=/opt/passwd.txt mode=600 owner=root group=root'

 

 

 #创建个tetst.txt文件

ansible dbservers -m file -a 'path=/opt/test.txt state=touch'

 

 

 #创建cxk文件夹

ansible dbservers -m file -a 'path=/opt/cxk state=directory'

 

 #删除cxk文件夹

 #软连接

ansible dbservers -m file -a 'src=/opt/hello.txt path=//root/hellowodld state=link'

 

 

hostname 模块

用于管理远程主机上的主机名

 #修改主机名

ansible dbservers -m hostname -a 'name=ansible-agent03'

 

 

PING模块

检测远程主机的连通性

 

yum 模块

在远程主机上安装与卸载软件包

 

ansible dbservers -m yum -a 'name=httpd state=present'

 

ansible webservers -m yum -a 'name=httpd state=absent'
#卸载服务

 

service/systemd 模块

用于管理远程主机上的管理服务的运行状态

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

 

 

ansible webservers -m service -a 'name=nginx state=started enabled=yes'

 

 

 

 

 

 

 #防火墙开着的

 

 

 

script 模块

实现远程批量运行本地的 shell 脚本

 

 

 

 ansible webservers -m script -a 'test.sh'

 

 

1 #!/bin/bash
2
3 MYNAME=$1
4 MYLIKE=$2
5 MYFILE=$3
6
7 echo "$MYNAME like $MYLIKE" > /opt/$MYFILE

 

ansible webservers -m script -a 'test.sh 徐导 美女 xd-girl.txt'

 

mount 模块

挂载文件系统
ansible-doc -s mount

常用的参数:
src:定义挂载设备的路径
path:定义挂载到哪个目录,必须指定
fstype:指定挂载文件的系统类型,必须指定,xfs、iso9660、nfs...
opts:定义挂载的参数,defaults、rw、ro...
state:定义挂载的状态,mounted(进行挂载,修改/etc/fstab信息)、absent(永久性卸载,并修改 /etc/fstab信息)、unmounted(临时卸载,不修改/etc/fstab信息)

 #添加块硬盘

 

alias scan='echo "- - -" > /sys/class/scsi_host/host0/scan;echo "- - -" > /sys/class/scsi_host/host1/scan;echo "- - -" > /sys/class/scsi_host/host2/scan'

 

 

ansible dbservers -m file -a 'path=/data state=directory'

 

 

ansible dbservers -m mount -a 'src=/dev/sr0 path=/mnt state=mounted fstype=iso9660'

 

 #解挂载

ansible dbservers -m mount -a 'src=/dev/sr0 path=/mnt state=unmounted'

 

ansible dbservers -m mount -a 'path=/data state=absent'   #永久解挂

 

archive 模块

打包压缩

常用的参数:
path: 必须参数,远程主机上需要被打包压缩的源文件/目录
dest: 打包压缩后的包文件路径(包文件的父目录必须存在);如果包文件已存在,则会被覆盖
format: 指定压缩类型,包括: bz2、gz(默认)、tar、xz、zip
remove=yes|no: 是否删除源文件

 

ansible dbservers -m archive -a "path=/opt/123.txt,/opt/hello.txt dest=/opt/ky33.tar.gz format=gz remove=yes"

 

 

 

ansible dbservers -m archive -a "path=/etc/yum.repos.d/ dest=/opt/repo.zip format=zip"

 unarchive 模块

解包解压缩

常用的参数:
copy:默认为 copy=yes ,拷贝的文件从 ansible 主机复制到远程主机,copy=no 表示在远程主机上寻找源文件解压
src:tar包源路径,可以是 ansible 主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需设置 copy=no
dest:解压后文件的目标绝对路径
remote_src: 和 copy 功能一样且互斥,设置 remote_src=yes 表示文件在远程主机上,设置为 remote_src=no 表示文件在 ansible 主机上

 

 

将 ansible 主机的压缩文件拷贝到到远程主机并解压,修改文件所属组和用户

ansible dbservers -m unarchive -a "src=/opt/nginx-1.24.0.tar.gz dest=/root copy=yes"

ansible dbservers -m unarchive -a "src=/opt/nginx-1.24.0.tar.gz dest=/root copy=yes"

 

 

#在远程主机解包

ansible dbservers -m unarchive -a "src=/opt/repo.tar.gz dest=/opt copy=no"

ansible dbservers -m unarchive -a "src=/opt/repo.tar.gz dest=/opt remote_src=yes"

 

replace 模块

类似于sed命令,主要也是基于正则进行匹配和替换

常用的参数:
path:必须参数,指定要修改的文件
regexp:必须参数,指定一个正则表达式
replace:替换regexp参数匹配到的字符串
backup=yes|no: 修改源文件前创建一个包含时间戳信息的备份文件
before:如果指定,则仅替换/删除此匹配之前的内容,可以和after参数结合使用
after:如果指定,则仅替换/删除此匹配之后的内容,可以和before参数结合使用
owner:修改文件用户名
group:修改文件组名
mode:修改文件权限

 #最开始的替换

 

 

 

 #所有2替换成two

ansible dbservers -m replace -a 'path=/opt/test.txt regexp='2' replace=two'

 

 

ansible dbservers -m replace -a "path=/opt/test.txt regexp='^(.*)' replace='#\1'"

 

 

 ansible dbservers -m replace -a "path=/opt/test.txt regexp='^#(1.*)' replace='\1'"

 

 

 

 

#恢复原样

 

ansible dbservers -m replace -a "path=/opt/test.txt regexp='^(a.*)' replace='#\1'"

 

 

 

 

 #中间加个4

 

 

ansible dbservers -m replace -a "path=/opt/test.txt regexp='4' replace=four after=cc before=ee"

 

 

 

ansible dbservers -m replace -a "path=/opt/test.txt regexp='4' replace=four after=cc before=ee backup=yes"

 

setup 模块

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

ansible webservers -m setup                #获取mysql组主机的facts信息
ansible dbservers -m setup -a 'filter=*ipv4'    #使用filter可以筛选指定的facts信息

 

 

 

 

ansible dbservers -m setup -a 'filter=ansible_default_ipv4'

 

 

 

 

inventory 主机清单 ---------
//Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内。

//如果是名称类似的主机,可以使用列表的方式标识各个主机。
vim /etc/ansible/hosts
[webservers]
192.168.80.11:2222        #冒号后定义远程连接端口,默认是 ssh 的 22 端口
192.168.80.1[2:5]

[dbservers]
db-[a:f].example.org    #支持匹配 a~f


//inventory 中的变量
Inventory变量名                  含义
ansible_host                  ansible连接节点时的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       提升为指定用户权限时的密码

(1)主机变量
[webservers]
192.168.80.11 ansible_port=22 ansible_user=root ansible_password=abc1234

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

[all:vars]                    #表示为所有组内的所有主机定义变量
ansible_port=223)组嵌套
[nginx]
192.168.19.24
192.168.19.25
192.168.19.30

[apache]
192.168.80.3[0:3]

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

 

posted @ 2024-02-06 17:26  citywalk  阅读(62)  评论(0)    收藏  举报