文章目录
一、Ansiable的管理方式
1. Ansible的管理方式
- Ansible的命令讲解
ansible --viersion #显示ansible的版本信息
ansible -m #指定模块
ansible -v #显示过程进行过程的显示
ansible --list-host #列出清单列表
ansible -i #指定清单文件
ansible -k #输入ssh密码进行登陆默认使用key进行登陆
ansible -b #切换用户身份进行登陆可以进行免密登陆被控主机
ansible -become-user=root #指定sudo的用户为谁
ansible -K #输入sudo的密码进行sudo的提权设定
ansible-doc #显示ansible的模块帮助信息
ansible-doc -v #显示ansible的详细信息多一个v显示会更详细,最对三个-vvv
ansible-doc -s #显示帮助的简要信息
- 命令显示的区别
黄色:已经成功执行,同时也以经更被控主机
绿色:已经成功执行,同但是未能更改被控主机
红色:命令执行失败
二、Ansible常用模块
1.command模块
- 功能讲解
此模块为默认模块,前提时用户级配置文件未能被更改过则会进行个更改
此模块作用为:远程主机执行命令但是很多通配符在模块中时不进行支持的 - 命令参数以及用法
参数
chdir=/etc #表示执行命令之前进入/etc这个目录
creats=/etc/passwd #表示这个文件如果存在则命令不再执行
removes=/etc/passwd #表示这个文件如果存在则这命令正常执行
cmd #表示运行指定命令,但是现在已经可以被替代掉直接写命令即可
ansible -m command -a 'chdir=/etc removes=passwd tail -n 3 passwd' -b
#chdir执行命令之前首先进入某个目录,如果这个文件存在则执行命令,已经进入此目录所以使用相对路径即可
ansible -m command -a 'chdir=/etc creats=passwd tail -n 3 passwd' -u root -k
#creats表示如过文件存在则命令不会执行被控主机中
2.shell模块
-
功能讲解
功能和command模块类似但是支持通配符,可以在被控主机中进行远程的命令调用 -
命令以及参数用法
chdir #执行命令前先进入到指定目录
cmd #运行命令指定
creates #如果文件存在将不运行
removes #如果文件存在在将运行
free_form #在远程主机中执行的命令,此参数不需要加
executable #指定执行环境,默认为sh,可以在ad-hoc调用时直接指定bash或是其他环境
ansible all -m shell -a "executable=bash ps ax | grep $$" -k #注意必须使用弱引用在包含特殊符号的命令中,否则特殊符号会被转译
3.script模块
- 功能讲解
在被控主机中执行本地已经写好的脚本,但是同时将被控的主机
- 命令用法
ansible all -m script -a "~/Desktip/user.sh" -k #脚本在本机的那里就写那里,就可以执行在被控主机志中
4.copy模块
-
功能
将本地的文件远程复制到多台主机上进行执行使得文件可以在多台主机进行执行 -
命令讲解
src=1.sh #源文件
dest=/mnt/westos #目的地文件
owner=westos #文件所有人
group=westos #文件所有组
mode=755 #文件权限
backup=yes #备份源文件,no为不备份,原文内容一致则不会备份
content= #指定文本内容在直接在受控主机中生成文件\n表示换行符
示例
ansible all -m copy -a "src=/etc/passwd dest=/root/Desktop/root_passwd mode=777 owner=root group=ftp" -k
ansible all -m copy -a "execonet='linux is good\n' dest=/mnt/linux mode=755 owner=root group=westos backup=yes"
5.fetch模块
- 参数
把受控主机的文件复制到本机中进行存储的模块
flat=yes #时定位置为文件而不是目录
dest= #设定目标的保存目录
src= #被控主机的原文件位置
- 示例
ansible all -m fetch -a "src=/mnt/linux1 dest=/mnt mode=777 owner=westos group=ftp"
ansible all -m fetch -a 'src=/etc/hostname dest=/mnt/hahaha flat=yes mode=777 owner=root group=ftp'
6.file模块
- 参数
用于文件的创建和文件的属性设定
path= #设定文件的的路径
state= #设定文件的状态
state=touch #建立文件
state=directory #第归建立目录即杂当前目录下建立一个子目录
state=adsent #删除文件或是目录
state=link #建立软连接
state=hard #建立硬连接
recures=yes #表示递归更改,即更改这个目录和目录下的所有子文件
dest= #表示目标文件
mode= #表示目标文件的权限
owner= #目标文件或是目录的所有人
group= #目标文件或是目录的所有组
- 命令
ansible all -m file -a 'path=/mnt/westos state=touch'
ansible all -m file -a 'path=/mnt/westos state=adsent'
ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m file -a 'path=/mnt/westos state=directory recures=yes mode=775 owner=westos group=westos'
ansible all -m file -a 'path=/mnt/westos state=directory mode=775 recurse=yes'
ansible all -m file -a 'path=/mnt/westos state=link'
ansible all -m file -a 'src=/mnt/hostname dest=/mnt state=hard'
7.unarchive模块
- 参数
解压文件
copy #默认为yes 从ansible主机复制文件到受控主机
#设定为no从受控主机中寻找src源文件
remote_src #从受控主机复制文件到被控主机
src #包路径,可以是本地主机也可以使受控主机
dest #受控主机目录
mode #加压后文件权限copy=yes时才可生效
- 命令
ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt copy=no'
#直接将压缩包在被控主机中进行
ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt remote_src=yes'
#直接将压缩包在被控主机中进行
ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt copy=yes'
#将本机的压缩宝解压后发送到被控主机中
ansiblee all -m unachive -a 'src=/mnt/etc.tar.gz dest=/mnt remote_src=no'
#将本机中的压缩包解压后发送到被控主机中
8.archive模块
1.参数
压缩目录文件
path #打包名称
dest #打包目标位置
format #打包文件使用的格式
owner #文件打包后所有人
mode #文件打包后权限
- 命令
ansible all -m archive -a 'path=/etc dest=/mnt/etc.tar.bzip format=bzip owner=root mode=755'
#可以将文件打包归档到指定位置同时将位置指定
9.hostname模块
- 参数
name #指定主机名称
- 命令
ansible 172.25.254.91 -m hostname -a "name=westos.org"
10.cron模块
- 参数
job= #需要执行的命令
name= #crontab的任务名称
disabled=yes #表示禁用cron模块
disabled=no #表示可以使用cron模块
mintue= #表示设定执行的时间的分钟数
hory= #时
day= #天
moth= #月
weekday= #周
state=absent #表示删任务以及命令
- 命令
ansible all -m cron -a "job='date' name=westos state=absent"
#删除定时任务
ansible all -m cron -a "job='date' name=westos hory=*/2 disabled=no"
#建立crontab任务
11.yum_repoitpory模块
- 参数
主要用在配置远程主机的软件仓库
baseurl= #软件仓库的位置
name= #软件仓库的名字
gpgcheck= #是否经过gpgcheck的验证
enabled= #开启软件仓库
state= #默认为present
state=absent #为删除该软件仓库
- 命令
ansible all -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.90/westos/BaseOS description='is from to ansible BaseOS' gpgcheck=1 file=westos"
#搭建软件仓库的内容
ansible all -m yum_repository -a "file=westos name=Baseos state=absent"
#删除软件仓库方式
12.dnf模块
- 参数
下载软件到受控主机中进行控制
disable_gpg_check=yes #忽略gpgcheck检测的影响
name= #软件包的名称
stent=pretsent #下载
stent=latest #更新软件,如果软件为下载则直接下载
stent=absent #卸载软件
disable_gpg_check=yes #禁用gpgchek
disablerepo= #禁用某个指定的源
- 命令
ansible all -m dnf -a 'name=httpd state=latest disable_gpg_check=yes' #更新软件同时忽略了gpgcheck的问题
ansible all -m dnf -a 'name=httpd state=prestent' #安装软件
ansible all -m dnf -a 'name=http://172.25.254.90/westos/EULA state=present' #网络下载软件
ansible all -m dnf -a 'name="*" state=latest' #根新系统中的所有软件
13.service模块
- 参数
name=httpd #指定服务
state=started #开启服务
state=reloaded #重读配置文件
state=restarted #重启配置文件
state=stoped #停止服务
enabled=yes #开机自起该服务no开机不启动
- 命令
ansible all -m service -a "name=httpd state=started enabled=yes"
ansible all -m service -a "name=httpd state=reloaded enabled=no"
14.firewalld模块
- 参数
zone #火墙的域
service #服务名称
state #状态enabled开启disabled关闭
immediate #立即生效
port #设定端口允许
- 示例
ansible server -m firewalld -a "zone=public port=8080/tcp permanent=yes state=enabled immediate=yes"
ansible server -m firewalld -a "zone=public port=8080/tcp permanent=yes state=enabled "
15.user模块
- 参数
uid #用户uid
name #用户名称
comment #添加用户注释
group #指定基本组
groups #指定附加组可同时指多个附加组
append #建立用户的同时不建立所有组
shell #指定用户的shell
state=absent #删除
state=prestnt #建立
generate_ssh_key #生成ssh_key
remove #删除用户的家目录默认为no
password #用户的密码为明码密码
- 命令
ansible server -m user -a 'name=ansible uid=6666 comment=shiyan group=tss'
ansible server -m user -a 'remove=yes name=ansible state=absent'
ansible server -m user -a 'name=westos generate_ssh_key=yes'
ansible server -m user -a 'name=ansible groups="tss,cgred" shell=/bin/bash' #建立用户同时更改附加组以及不建立所有组
openssl passwd -6 westos #生成密码
ansible server -m user -a 'password=$6$h09ikkmvl/DGIwbD$sZClEuJipX2Kyc8tuUnJ361SftQS9Ujh3aB2sczPpx2Rfa49xwUrWdixus5ZeSQ9S0QQ/.UpOlAV72asrPMtl1 name=ansible' #建立密码同时建立密码可以将倒入其中
16.group模块
- 参数
name #组名称
gid #组id
state=present #建立
state=absent #删除
- 命令示例
ansible server -m group -a 'gid=2048 name=group state=present'
ansible server -m group -a 'name=group state=absent'
17.lineinfile模块
- 参数
path #指定要操作的文件。
line #指定文本内容。 "|+" 表示格式化输入
regexp ##使用正则表达式匹配对应的行当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换
#当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。
state=absent #删除所有的符合条件的数据
state=present #建立
backup #是否在修改文件之前对文件进行备份。
create #当要操作的文件并不存在时,是否创建对应的文件。
insertafter #借助insertafter参数可以将文本插入到“指定的行”之后,可设定值为正则表达式也可设定为指定的字符串
2.命令
ansible server -m lineinfile -a 'path=/mnt/westos line="westos hellow" create=yes' #将内容定义到文集爱你中文件不存在则自动创建
ansible server -m lineinfile -a 'path=/mnt/westos line="hellow" ' #将内容定义到存在的文件中create默认为NO
ansible server -m lineinfile -a 'path=/mnt/westos regexp="[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\/[0-9]{0,2}" state=absent'
#按照正则表达式进行删除内容
ansible server -m lineinfile -a 'path=/mnt/westos regexp="[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\/[0-9]{0,2}" line='hellow'' #按照正则表达式进行搜索后进行插入
ansible server -m lineinfile -a 'path=/mnt/westos insertafter='test' line='jaja'' #插入到指定行之后
ansible server -m lineinfile -a 'path=/mnt/westos insertbefore='fa' line='test'' #插入到指定行之前
ansible server -m lineinfile -a 'path=/mnt/westos insertbefore='fa' line='hell' backup=yes' #进行更改同时备份
ansible server -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes' #将变量向后同步延伸
18.reqlace模块
- 参数
path #指定要操作的文件
regexp #指定一个正则表达式,文件中与正则匹配的字符串将会被替换。
replace #指定最终要替换成的字符串。
backup #是否在修改文件之前对文件进行备份,最好设置为yes。
- 命令示例
ansible server -m replace -a 'path=/mnt/westos regexp="hello*" replace="#hello"' #可以用来关闭指定服务
19.setup模块
- 参数
filter #用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。
- 命令示例
ansible server -m setup #显示所有的清单信息以及内容显示