- 『4-2』Linux之Ansible中常用模块
-
- ###########test setting#############
- 一.ansible实现管理的方式
- 二.Ad-Hoc执行方式中如何获得帮助
- 三.ansible命令运行方式及常用参数
-
-
- 格式:`ansible 清单 -m 模块 -a 模块参数`
- 常用参数
-
-
- `ansible --version` ##显示版本
- `ansible -m` module ##指定模块,默认为command模块
- `ansible -v` ##详细过程 -vv -vvv更详细过程
- `ansible --list ` ##显示主机列表,也可以用--list-hosts
- `ansible -k ` ##提示输入ssh连接密码,默认key认证
- `ansible -C` ##预执行检测
- `ansible -T ` ##执行命令的超时时间,默认10s
- `ansible -u ` ##指定远程执行的用户
- `ansible -b ` ##执行sudo切换身份操作
- `ansible -become-user=USERNAME `##指定sudo的用户
- `ansible -K` ##提示输入sudo密码
-
-
- 四.ansible的基本颜色代表信息
- 五.ansible中的常用模块
-
- 1.command
- 2.shell
- 3.script
- 4.copy
- 5.fetch
- 6.file
- 7.archive
- 8.unarchive##
- hostname
- 作用:管理主机名称
- cron
- yum_repository
- dnf
- service
- firewalld
- user
-
- 作用:模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
- 常用参数
-
-
- `name ` ##必须参数,用于指定要操作的用户名称。
- `group ` ##指定用户所在的基本组。
- `gourps ` ##指定用户所在的附加组。
- `append ` ##指定添加附加组默认值为no
- `shell ` ##指定用户的默认 shell。
- `uid ` ##指定用户的 uid 号。
- `comment` ##指定用户的注释信息。
- `state` ##用于指定用户是否存在于远程主机
- `state=absent ` 删除
- `remove ` ##当删除用户是删除用户家目录,默认值为no
- `password` ##此参数用于指定用户的密码。但密码为明文,
- `generate_ssh_key=yes` ##生成sshkey
-
- group
- lineinfile
-
-
-
- `path` ##指定要操作的文件。
- `line ` ##指定文本内容。
- `regexp `
- ##使用正则表达式匹配`对应的行`当`替换文本`时
- ##如果有`多行文本都能被匹配`
- ##则只有`最后面被匹配到的那行`文本`才会被替换`
- ##当`删除文本`时,如果有`多行文本都能被匹配`
- ##这么这些行`都会被删除`。
- `state` ##当想要`删除`对应的文本时需要将state参数的值设置为`absent`
- `backrefs `##当内容无匹配规则时不对文件做任何更改,默认值为`no`
- ##向后引用regexp变量信息
- `insertafter` ##借助insertafter参数可以将文本插入到“指定的行”之后
- ##insertafter参数的值可以设置为`EOF`或者正则表达式
- `insertbefore` ##借助insertbefore参数可以将文本插入到“指定的行”之前
- #insertbefore参数的值可以设置为`BOF`或者正则表达式
- `backup` ##是否在修改文件之前对文件进行备份。
- `create` ##当要操作的文件并不存在时,是否创建对应的文件。
-
-
- replace
- setup#
- debug#
『4-2』Linux之Ansible中常用模块
###########test setting#############
#ssh key crypt node{1..3} root,westos
#id_rsa ----> ansible -----> lee,root
#sudo node{1..3} westos ALL=(ALL) NOPASSWD: ALL
#
#vim test.sh
#!/bin/bash
echo "####################node1###################"
ssh [email protected] "$*"
echo ""
echo "#####################node2#################"
ssh [email protected] "$*"
echo ""
echo "##################node3################"
ssh [email protected] "$*"
一.ansible实现管理的方式
Ad-Hoc
##利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook
##ansible脚本,主要用于大型项目场景,需要前期的规划
[root@student_26 .ansible]# vim test.yml
[root@student_26 .ansible]# ansible-playbook test.yml
二.Ad-Hoc执行方式中如何获得帮助
ansible-doc
##显示模块帮助的指令
格式:ansible-doc [参数] [模块...]
常用参数
ansible-doc -l
##列出可用模块
ansible-doc -s
##显示指定模块的playbook片段
三.ansible命令运行方式及常用参数
格式:ansible 清单 -m 模块 -a 模块参数
常用参数
ansible --version
##显示版本
ansible -m
module ##指定模块,默认为command模块
ansible -v
##详细过程 -vv -vvv更详细过程
ansible --list
##显示主机列表,也可以用–list-hosts
ansible -k
##提示输入ssh连接密码,默认key认证
ansible -C
##预执行检测
ansible -T
##执行命令的超时时间,默认10s
ansible -u
##指定远程执行的用户
ansible -b
##执行sudo切换身份操作
ansible -become-user=USERNAME
##指定sudo的用户
ansible -K
##提示输入sudo密码
ansible --version
##显示版本
ansible -m
module ##指定模块,默认为command模块
ansible -v
##详细过程 -vv -vvv更详细过程
ansible --list
##显示主机列表,也可以用–list-hosts
ansible -k
##提示输入ssh连接密码,默认key认证
ansible -C
##预执行检测
ansible -T
##执行命令的超时时间,默认10s
ansible -u
##指定远程执行的用户
ansible -b
##执行sudo切换身份操作
ansible -become-user=USERNAME
##指定sudo的用户
ansible -K
##提示输入sudo密码
四.ansible的基本颜色代表信息
绿色
##执行成功但为对远程主机做任何改变
黄色
##执行成功并对远程主机做改变
红色
##执行失败
五.ansible中的常用模块
1.command
功能: 在远程主机执行命令,此模块为默认模块
常用参数
chdir=
##执行命令前先进入到指定目录
cmd=
##运行命令指定,默认命令就为cmd,可不加
creates=
##如果文件存在将不运行
removes=
##如果文件存在在将运行
free_form=
##在远程主机中执行的命令,此参数不需要加
#实例
ansible all -m command -a “useradd lee” -u root -k
ansible all -m command -a “userdel -r lee” -u root -k
ansible all -m command -a "chdir=/etc cat passwd " -u root -k
ansible all -m command -a "chdir=/etc creates=/etc/passwd cat passwd " -u root -k
ansible all -m command -a "chdir=/etc removes=/etc/passwd cat passwd " -u root -k
##注意##
Linux中的很多通配符在command模块中不支持
2.shell
功能:和command功能类似
常用参数
chdir
##执行命令前先进入到指定目录
cmd
##运行命令指定
creates
##如果文件存在将不运行
removes
##如果文件存在将运行
free_form
##在远程主机中执行的命令,此参数不需要加
executable
##指定执行环境,默认为sh
#实例
ansible all -m shell -a "executable=bash ps ax | grep $$ " -k
3.script
#功能:在ansible主机中写好的脚本在受控主机中执行
#实例
vim /mnt/westos.sh
#!/bin/bash
echo $HOSTNAME
ansible all -m script -a "/mnt/westos.sh" -k
4.copy
功能:从ansible主机复制文件到受控主机
常用参数
src
##源文件
dest
##目的地文件
目录不存在时,会报错
owner
##指定目的地文件所有人
group
mode
##指定目的地文件权限
backup=yes
##当受控主机中存在文件时备份原文件
content
##指定文本内容直接在受控主机中生成文件
#实例
ansible all -m copy -a "src=/root/westos dest=/mnt/westos owner=lee mode=777 backup=yes"
ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=lee mode=600"
5.fetch
功能:从受控主机把文件复制到ansible主机,但不支持目录
常用参数
src
##受控主机的源文件
dest
##本机目录
flat
##基本名称功能
只有一个hostname名,后者覆盖了前者
#实例
ansible all -m fetch -a “src=/etc/hostname dest=/mnt” -k
ansible all -m fetch -a “src=/etc/hostname dest=/mnt flat=yes” -k
6.file
功能:设置文件的属性
[Errno 13] Permission denied
权限拒绝,调整主配置文件
或进入~/.ansible目录
常用参数#
path
##指定文件名称
state
##指定操作状态
state=touch
建立
state=absent
删除
*无效
state=directory
可递归,建立目录
state=link
建立链接
state=hard
mode
##设定权限
owner
##设定文件用户
group
##设定文件组
src
##源文件
dest
##目标文件
recurse=yes
##递归更改
不加参数
加参数
#example
ansible all -m file -a 'path=/mnt/test.sh state=touch'
ansible all -m file -a 'path=/mnt/test.sh state=absent'
ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
ansible all -m file -a 'src=/mnt/file dest=/mnt/westos state=link'
ansible all -m file -a 'src=/mnt/file dest=/mnt/westos1 state=hard'
ansible all -m file -a 'path=/mnt/file state=touch owner=lee group=westos mode=777'
7.archive
作用:压缩
常用参数
path
##打包目录名称
dest
##声称打包文件名称
format
##打包格式
owner
##指定文件所属人
mode
##指定文件权限
#实例
ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=lee mode=700' -k
8.unarchive##
功能:解压缩
常用参数
copy
##默认为yes 从ansible主机复制文件到受控主机
copy=no
设定为no 从受控主机中寻找src源文件
remote_src
##功能同copy且相反
##设定为yes 表示包在受控主机
##设定为no表示包在ansible主机
src
##包路径,可以使ansible主机也可以使受控主机
dest
##受控主机目录
mode
##解压后文件权限 copy=no时不生效
#实例
ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt owner=lee' -k
ansible all -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"
hostname
作用:管理主机名称
常用参数
name
##指定主机名称
#实例
[root@student_26 .ansible]# ansible all -m shell -a 'hostname'
[root@student_26 .ansible]# ansible all -m hostname -a 'name=node1.westos.org' -b --become-user=root
cron
作用:计划任务
常用参数
minute
##分钟
hour
##小时
day
##天
month
##月
weekday
##周
name
##任务名称
job
##任务脚本或命令
[root@student_26 .ansible]# ansible all -m cron -a 'name=westostest1 job=date minute=*/3'
[root@student_26 .ansible]# ansible all -m shell -a 'crontab -l'
disabled=yes
禁用计划任务
[root@student_26 .ansible]# ansible all -m cron -a 'name=westostest job=date disabled=yes'
disabled=no
启动计划任务
[root@student_26 .ansible]# ansible all -m cron -a 'name=westostest job=date disabled=no'
state=absent
删除计划任务
##实例##
ansible list1 -m cron -a "job='echo hello' name=test disable=yes" -k
ansible list1 -m cron -a "job='echo hello' name=test disabled=yes" -k
ansible list1 -m cron -a "job='echo hello' name=test state=absent" -k
yum_repository
作用:配置系统软件仓库源文件
常用参数
name
##指定仓库名称
baseurl
##指定源路径
description
##指定仓库描述
file
##指定仓库文件名称
enabled
##仓库是否启用
gpgcheck
##仓库是否检测gpgkey
state
##默认值present 建立
[root@student_26 .ansible]# ansible test -m yum_repository -a "name=baseOS baseurl=http://172.25.254.26/westos/BaseOS description=westosBaseOS gpgcheck=0 enabled=0 file=lee "
state=absent
为删除
#实例
ansible all -m yum_repository -a "name=AppStream baseurl=http://172.25.254.250/rhel8.2/AppStream description=AppStream_westos gpgcheck=no file=westos_test" -k
ansible all -m yum_repository -a "name=AppStream file=westos_test state=absent" -k
dnf
作用:管理系统中的dnf仓库及管理软件
常用参数
name
##指定包
state
##指定动作
state=present
安装
state=latest
更新
[root@student_26 .ansible]# ansible test -m dnf -a 'name=vsftpd state=latest'
state=absent
删除
[root@student_26 .ansible]# ansible test -m dnf -a 'name=vsftpd state=absent'
autoremove
卸载依赖性
[root@student_26 .ansible]# ansible test -m dnf -a 'name=httpd state=absent autoremove=yes'
[root@student_26 .ansible]# ansible test -m dnf -a 'name="@Virtualization Tools" state=present'
list
##列出指定信息
list httpd
list installed
list all
list available
disable_gpg_check=yes
#禁用gpgkey检测
[root@student_26 .ansible]# ansible test -m dnf -a 'name=http://172.25.254.250/software/linuxqq_2.0.0-b2-1084_x86_64.rpm state=latest disable_gpg_check=yes'
enablerepo
##指定安装包来源
disablerepo
##禁用安装包来源
##实例
ansible all -m dnf -a "name=httpd state=latest"
ansible all -m dnf -a 'name="httpd,mariadb-server" state=present'
ansible all -m dnf -a 'name=httpd state=absent'
ansible all -m dnf -a 'name=httpd state=absent autoremove=no'
ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream'
ansible all -m dnf -a 'name="*" state=latest'
ansible all -m dnf -a 'name=http://172.25.254.250/software/wps-office-xxx.rpm state=present'
ansible all -m dnf -a 'name="@Virtual Tools" state=present'
service
作用:管理系统服务状态
常用参数
name
##指定服务名称
state
##指定对服务的动作
state=started
state=stopped
state=restarted
state=reloaded
enabled
##设定服务开机是否启动
enabled=yes
开启启动
enabled=no
开机不启动
##实例
ansible all -m service -a "name=httpd state=started enabled=yes" -k
ansible all -m service -a "name=httpd state=restarted enabled=yes" -k
firewalld
常用参数
zone
防火墙的域
service
服务名称
permanent=yes
永久生效
state
state=enabled
允许的
state=disabled
拒绝
immediate=yes
立即生效
user
作用:模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
常用参数
name
##必须参数,用于指定要操作的用户名称。
group
##指定用户所在的基本组。
gourps
##指定用户所在的附加组。
append
##指定添加附加组默认值为no
shell
##指定用户的默认 shell。
uid
##指定用户的 uid 号。
建立用户时
用户存在时
comment
##指定用户的注释信息。
state
##用于指定用户是否存在于远程主机
#`present` 建立
state=absent
删除
remove
##当删除用户是删除用户家目录,默认值为no
password
##此参数用于指定用户的密码。但密码为明文,
##可以用`openssl passwd -6` '密码'生成加密字符
generate_ssh_key=yes
##生成sshkey
##实例
ansible all -m user -a 'name=lee'
ansible all -m user -a 'name=lee state=absent'
ansible all -m user -a 'name=lee remove=yes state=absent'
ansible all -m user -a 'name=lee group=888'
ansible all -m user -a 'name=lee group=888 groups="user1,user2"'
ansible all -m user -a 'name=lee groups="user3"'
ansible all -m user -a 'name=lee groups="user1,user2" append=yes'
openssl passwd -6 'westos'
ansible all -m user -a 'name=lee password="$6$F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/"'
ansible all -m user -a 'name=lee generate_ssh_key=yes'
group
作用:group 模块可以帮助我们管理远程主机上的组。
常用参数
name
##用于指定要操作的组名称。
state
##用于指定组的状态
#present 建立
[root@student_26 .ansible]# ansible test -m group -a 'name=lala state=present'
#absent
删除
gid
##用于指定组的gid。
##实例
ansible all -m group -a 'name=westoslee'
ansible all -m group -a 'name=westoslee state=absent'
ansible all -m group -a 'name=westoslee gid=8888'
lineinfile
path
##指定要操作的文件。
line
##指定文本内容。
regexp
##使用正则表达式匹配对应的行
当替换文本
时
##如果有多行文本都能被匹配
##则只有最后面被匹配到的那行
文本才会被替换
##当删除文本
时,如果有多行文本都能被匹配
##这么这些行都会被删除
。
state
##当想要删除
对应的文本时需要将state参数的值设置为absent
#`state的默认值`为`present`。
backrefs
##当内容无匹配规则时不对文件做任何更改,默认值为no
##向后引用regexp变量信息
insertafter
##借助insertafter参数可以将文本插入到“指定的行”之后
##insertafter参数的值可以设置为EOF
或者正则表达式
insertbefore
##借助insertbefore参数可以将文本插入到“指定的行”之前
#insertbefore参数的值可以设置为BOF
或者正则表达式
backup
##是否在修改文件之前对文件进行备份。
create
##当要操作的文件并不存在时,是否创建对应的文件。
vim /mnt/westos
hello westos
hello test
hello linux
##实例
ansible all -m lineinfile -a 'path=/mnt/westos line="hello westos"'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^westos" line="hello westos" '
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^test" line="westos test"'
ansible all -m lineinfile -a 'path=/mnt/westos regexp='^test' line="westos test new" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end #####" insertafter=EOF'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end lee #####" insertafter="hello"'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test #####" insertbefore=BOF'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test lee #####" insertbefore="hello"'
replace
作用:replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
常用参数
path
##指定要操作的文件
regexp
##指定一个正则表达式
#文件中与正则匹配的字符串将会被替换。
replace
##指定最终要替换成的字符串。
backup
##是否在修改文件之前对文件进行备份,最好设置为yes。
##实例
ansible all -m replace -a 'path=/mnt/westos regexp="WESTOS" replace="westos_lee" backup=yes'
setup#
作用:setup模块用于收集远程主机的一些基本信息
常用参数
filter
##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。
##实例
ansible all -m setup -k
ansible all -m setup -a "filter='ansible_all_ipv4_addresses'" -k
debug#
作用:调试模块,用于在调试中输出信息
常用参数:
msg
: ##调试输出的消息
[root@student_26 .ansible]# ansible test -m debug -a "msg='hello world'"
var
: ##将某个任务执行的输出作为变量传递给debug模块
##debug会直接将其打印输出