目录
二、Ad-Hoc执行方式中如何获得帮助
一、Ansible实现管理的方式
Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ansible脚本,主要用于大型项目场景,需要前期的规划
二、Ad-Hoc执行方式中如何获得帮助
ansible-doc 显示模块帮助的指令
格式
ansible-doc [参数] [模块...]
常用参数
-l 列出可用模块
-s 显示指定模块的playbook片段
vim test.yml
cat test.yml
ansible-playbook test.yml
ansible-doc
ansible-doc -l | wc -l
ansible-doc -l | grrep shell
三、Ansible命令运行方式及常用参数
参数 | 功能 |
---|---|
-i | 指定hosts文件路径,默认在/etc/ansible/hosts |
-m | 指定使用的module名称,默认command模块 |
-a | 指定模块参数 |
-k(小写) | 提示输入ssh密码,并非基于ssh密钥认证 |
-K(大写) | 提示输入sudo密码 |
-b | 使用sudo执行命令 |
-become-user= | 指定sudo的用户 |
-f,-forks=NUM | NUM默认是整数5,指定fork开启同步进程的个数 |
-u | 指定远程主机的执行用户 |
-v | 详细模式,如果执行成功,输出详细结果,-vv -vvv 更详细过程 |
-C | 预执行检测 |
-T | 执行命令的超时时间,默认10s |
--list | 显示主机列表,也可以用--list-hosts |
--version | 显示版本 |
ansible --version
ansible all -m shell -a 'whoami' -C
ansible all -m shell -a 'whoami' -u test
ansible all -m shell -a 'whoami' -utest
ansible all -m shell -a 'whoami'
ansible all -m shell -a 'whoami' -b -become-user=yyl
ansible all -m shell -a 'whoami' -b -become-user=yyl -K
四、Ansible的基本颜色代表信
绿色 | 执行成功但未对远程主机做任何改变 |
黄色 | 执行成功并对远程主机做改变 |
红色 | 执行失败 |
五、Ansible中的常用模块
1、command模块
在远程主机执行命令,此模块为默认模块
常用参数 | 功能 |
---|---|
chdir | 执行命令前先进入到指定目录 |
cmd | 运行命令指定 |
creates | 如果文件存在将不运行 |
removes | 如果文件存在将运行 |
ansible all -m command -a 'chdir=/mnt pwd'
ansible all -m command -a 'chdir=/mnt cmd=pwd'
ansible all -m command -a 'chdir=/mnt creates=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt removes=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt removes=/mnt/file cat passwd'
ansible all -m command -a 'chdir=/mnt touch file'
ansible all -m command -a 'chdir=/mnt creates=/mnt/file pwd'
ansible all -m command -a 'chdir=/mnt removes=/mnt/file pwd'
注意
Linux中的很多通配符在command模块中不支持
ansible all -m command -a "chdir=/mnt touch file{1..10}"
2、shell模块、script模块
(1)shell模块
shell模块与command模块类似
常用参数 | 功能 |
---|---|
chdir | 执行命令前先进入到指定目录 |
cmd | 运行命令指定 |
creates | 如果文件存在将不运行 |
removes | 如果文件存在将运行 |
executable | 指定执行环境,默认为sh |
ansible all -m shell -a 'chdir=/mnt/ touch file{1..3}'
ansible all -m shell -a 'chdir=/mnt/ ls -ld /mnt'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
ansible all -m shell -a 'ps ax | grep $$'
ansible all -m shell -a 'executable=/bin/bash ps ax | grep $$'
(2)script模块
在ansible主机中写好的脚本在受控主机中执行
vim test.sh
cat test.sh
ansible all -m script -a "test.sh"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
3、copy模块、fetch模块
(1)copy模块
从ansible主机复制文件到受控主机
参数 | 功能 |
---|---|
src | 源文件 |
dest | 目的地文件 |
owner | 指定目的地文件所有人 |
group | 指定目的地文件所有组 |
mode | 指定目的地文件权限 |
backup=yes | 当受控主机中存在文件时备份原文件 |
content | 指定文本内容直接在受控主机中生成文件 |
复制当前目录的test.sh
到受控主机的/mnt
下,文件所有人为yyl
,权限为755
ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
在ansible主机上修改文件内容,开启备份,再次发送 先修改一下文件
ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755 backup=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
直接输入文件内容,在受控主机上生成文件
ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/test.sh owner=yyl mode=755"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
(2)fetch模块
从受控主机把文件复制到ansible主机,但不支持目录
参数 | 功能 |
---|---|
src | 受控主机的源文件 |
dest | 本机目录 |
flat | 基本名称功能,单纯只要文件,不要路径的层层目录 |
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible"
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible/ flat=yes"
4、file模块
设置文件的属性
参数 | 功能 |
---|---|
path | 指定文件名称 |
state | 指定操作状态touch :建立absent :删除directory :递归目录link :建立软链接hard :建立硬链接 |
mode | 设定权限 |
owner | 设定文件用户 |
group | 设定文件组 |
src | 源文件 |
dest | 目标文件 |
recurse=yes | 递归更改 |
ansible all -m shell -a 'rm -fr /mnt/*'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
创建test.sh文件
ansible all -m file -a 'path=/mnt/test.sh state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
删除
ansible all -m file -a 'path=/mnt/test.sh state=absent'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
创建目录
ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
ansible all -m file -a 'path=/mnt/westos/file1 state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
递归更改目录权限
ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
软连接
ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl state=link'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
硬链接
ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl1 state=hard'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
5、archive模块、unarchive模块
(1)archive模块——压缩
参数 | 功能 |
---|---|
path | 打包目录名称 |
dest | 声称打包文件名称 |
format | 打包格式 |
owner | 指定文件所属人 |
mode | 指定文件权限 |
ansible all -m archive -a "path=/mnt dest=/mnt/mnt.tar.gz format=gz owner=yyl mode=700"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
(2)unarchive模块——解压缩
copy | 默认为yes ,从ansible主机复制文件到受控主机设定为 no ,从受控主机中寻找src 源文件 |
remote_src | 功能同copy且相反 设定为 yes 表示包在受控主机设定为 no 表示包在ansible主机 |
src | 压缩文件的路径 |
dest | 受控主机目录 |
mode | 解压后文件的权限<copy=yes> |
tar zcf ansible.tar.gz .
ls
ansible all -m unarchive -a 'src=./ansible.tar.gz dest=/mnt copy=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
提前在vm1,2上压缩一个不被限制的gz包
ansible all -m unarchive -a 'src=/mnt/file.tar.gz dest=/mnt copy=no'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
6、hostname模块、cron模块
(1)hostname模块
管理主机名称
常用参数:name
:指定主机名称
ansible 192.168.67.112 -m hostname -a 'name=vm22'
ansible 192.168.67.112 -m shell -a 'hostname'
ansible 192.168.67.112 -m hostname -a 'name=vm1'
ansible 192.168.67.112 -m shell -a 'hostname'
(2)cron模块
计划任务
minute | 分钟 |
hour | 小时 |
day | 天 |
month | 月 |
weekday | 周 |
name | 任务名称 |
job | 任务脚本或命令 |
disabled | yes 禁用计划任务no 启动计划任务 |
state | absent 删除计划任务 |
创建计划任务
ansible all -m cron -a 'job="echo hello world" name=test hour=0 minute=0'
cd /var/spool/cron/ ls
cat root
crontab -l
取消计划任务
ansible all -m cron -a 'job="echo hello world" name=test hour=0 minute=0 disabled=yes'
crontab -l
删除计划任务
ansible all -m cron -a 'job="echo hello world" name=test hour=0 minute=0 state=absent'
crontab -l
7、yum_repository模块、yum(dnf)模块
(1)yum_repository模块
配置系统软件仓库源文件
参数 | 功能 |
---|---|
name | 指定仓库名称 |
baseurl | 指定源路径 |
description | 指定仓库描述 |
file | 指定仓库文件名称 |
enabled | 仓库是否启用 |
gpgcheck | 仓库是否检测gpgkey |
state | 默认值present : 建立absent :删除 |
创建网络仓库
ansible all -m yum_repository -a 'file=test name=test description=test baseurl=file:///test gpgcheck=0 '
ansible all -m shell -a 'cat /etc/yum.repos.d/test.repo'
删除网络仓库
ansible all -m yum_repository -a 'name=test state=absent'
ansible all -m shell -a 'cat /etc/yum.repos.d/test.repo'
ansible all -m shell -a 'ls /etc/yum.repos.d/'
(2)yum(dnf)模块
管理系统中的yum(dnf)仓库及管理软件(rhrl7中为yum,rhel8中为dnf)
参数 | 功能 |
---|---|
name | 指定包 |
state | 指定动作present :安装latest :更新absent :删除 |
list | 列出指定信息httpd 、installed 、all 、available |
disable_gpg_check | 禁用gpgkey检测 |
enablerepo | 指定安装包来源 |
disablerepo | 禁用安装包来源 |
autoremove | yes :移除依赖性no :不移除依赖性 |
安装软件
ansible all -m yum -a 'list=vsftpd'
ansible all -m yum -a 'name=vsftpd state=present'
ansible all -m yum -a 'name=vsftpd state=absent'
rpm -aq | grep vsftpd
8、service模块、firewalld模块
(1)service模块
管理系统服务状态
参数 | 功能 |
---|---|
name | 指定服务名称 |
state | 指定对服务的动作started 、stoped 、restarted 、reloaded |
enabled | 设定开机是否启动yes 开机启动;no 开机不启动 |
ansible all -m service -a 'name=firewalld state=started enabled=yes'
ansible all -m shell -a 'systemctl status firewalld.service'
ansible all -m service -a 'name=firewalld state=stopped enabled=no'
(2)firewalld模块
参数 | 功能 |
---|---|
zone | 火墙的域 |
service | 服务名称 |
permanent | 永久生效 |
state | enabled :允许disabled :拒绝 |
immediate | 立即生效 |
ansible all -m firewalld -a 'zone=public service=ftp permanent=yes state=enabled immediate=yes'
firewall-cmd --list-all
9、user模块、group模块
(1)group模块
管理远程主机上的组
name | 指定要操作的组名称 |
state | 指定组的状态present :建立absent :删除 |
gid | 指定组的gid |
ansible all -m group -a 'name=test state=present gid=8888'
ansible all -m shell -a 'tail -n 3 /etc/group'
ansible all -m group -a 'name=test state=absent'
ansible all -m shell -a 'tail -n 3 /etc/group'
(2)user模块
管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
name | 必须参数,指定要操作的用户名称 |
group | 指定用户所在基本组 |
groups | 指定用户所在附加组 |
append | 指定添加附加组默认组为no |
shell | 指定用户的默认shell |
uid | 指定用户的uid 号 |
comment | 指定用户的注释信息 |
state | 用于指定用户是否存在于远程主机present :建立absent :删除 |
remove | 当删除用户时删除用户家目录,默认值为no |
password | 指定用户的密码,但密码为明文 用 openssl password -6 '密码' 生成加密字符 |
generate_ssh_key | 生成sshkey |
ansible all -m user -a 'name=test state=present group=1000 groups=yyl'
ansible all -m shell -a 'id test'
ansible all -m user -a 'name=test password=$1$XkdUyGyt$Mj8pk2V5kTs9M7Xoa42Hk/'
tail -n 3 /etc/shadow
ssh test@localhost
ansible all -m user -a 'name=test state=absent remove=yes'
ansible all -m shell -a 'id test'
10、lineinfile模块、replace模块
(1)lineinfile模块
文件内容管理
path | 指定要操作的文件 |
line | 指定文本内容,“|+” 表示格式化输入 |
regexp | 使用正则表达式匹配对应的行 替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的文本才被替换 当删除文本时,如果有多行文本都能被匹配,那么这些行都会被删除 |
state | state的默认值为present 当想要删除对应的文本时需要将state参数的值设置为 absent |
backrefs | 默认值为no ,当为yes 时表示当内容无匹配规则时不对文件做任何更改,还有向后引用regexp变量信息的作用 |
insertafter | 将文本插入到“指定的行”之后,参数的值可以设置为EOF 或者正则表达式 |
insertbefore | 将文本插入到“指定的行”之前,参数的值可以设置为BOF 或者正则表达式 |
backup | 是否在修改文件之前对文件进行备份 |
create | 当要操作的文件并不存在时,是否创建对应的文件 |
ansible all -m lineinfile -a 'path=/mnt/file line="hello yyl\nhello linux\nhello ale" create=yes'
ansible all -m shell -a 'cat /mnt/file'
ansible all -m lineinfile -a 'path=/mnt/file line="haha ale" regexp="ale"'
ansible all -m shell -a 'cat /mnt/file'
ansible all -m lineinfile -a 'path=/mnt/file regexp="ale" state=absent'
ansible all -m shell -a 'cat /mnt/file'
ansible all -m lineinfile -a 'path=/mnt/file regexp="haha" line="haha ale"'
ansible all -m shell -a 'cat /mnt/file'
ansible all -m lineinfile -a 'path=/mnt/file regexp="haha" line="haha ale" backrefs=yes'
ansible all -m shell -a 'cat /mnt/file'
ansible all -m lineinfile -a 'path=/mnt/file regexp="(h.{4}).*(l.{4})" line="\1" backrefs=yes'
ansible all -m shell -a 'cat /mnt/file'
ansible all -m lineinfile -a 'path=/mnt/file line="####begin####" insertbefore=BOF'
ansible all -m lineinfile -a 'path=/mnt/file line="#####end#####" insertbefore=EOF'
ansible all -m shell -a 'cat /mnt/file'
(2)replace模块
根据指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
path | 指定要操作的文件 |
regexp | 指定一个正则表达式,文件中与正则匹配的字符串将会被替换 |
replace | 指定最终要替换成的字符串 |
backup | 是否在修改文件之前对文件进行备份 |
ansible all -m replace -a 'path=/mnt/file regexp="yyl" replace="YYL"'
ansible all -m shell -a 'cat /mnt/file'
11、setup模块、debug模块
(1)setup模块
收集远程主机的一些基本信息
常用参数:filter
:用于进行条件过滤,如果设置,仅返回匹配过滤条件的信息
ansible all -m setup -a "filter='ansible_all_ipv4_addresses'"
(2)debug模块
调试模块,用于在调试中输出信息
msg | 调试输出的消息 |
var | 将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出 |
verbosity | debug的级别(默认是0级,全部显示) |
ansible all -m debug -a 'msg="hello yyl"'
ansible all -m debug -a 'msg="hello yyl" verbosity=0'