1、ansible实现管理的方式
Ad-Hoc ##利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ##ansible脚本,主要用于大型项目场景,需要前期的规划
2、Ad-Hoc执行方式中如何获得帮助
ansible-doc ##显示模块帮助的指令
格式
ansible-doc [参数] [模块...]
常用参数
-l ##列出可用模块
-s ##显示指定模块的playbook片段
3、ansible命令运行方式及常用参数
格式:
ansible 清单 -m 模块 -a 模块参数
常用参数
--version ##显示版本
-m module ##指定模块,默认为command模块
-v ##详细过程 -vv -vvv更详细过程
--list ##显示主机列表,也可以用--list-hosts
-k ##提示输入ssh连接密码,默认key认证
-C ##预执行检测
-T ##执行命令的超时时间,默认10s
-u ##指定远程执行的用户
-b ##执行sudo切换身份操作
-become-user=USERNAME ##指定sudo的用户
-K ##提示输入sudo密码
--version ##显示版本
-m module ##指定模块,默认为command模块
-v ##详细过程 -vv -vvv更详细过程
--list ##显示主机列表,也可以用--list-hosts
-k ##提示输入ssh连接密码,默认key认证
-C ##预执行检测
-T ##执行命令的超时时间,默认为10s
-u ##指定远程执行的用户
-b ##执行sudo切换身份操作
-become-user=USERNAME ##指定sudo的用户
4、ansible的基本颜色代表
绿色 ##执行成功但为对远程主机做任何改变
黄色 ##执行成功并对远程主机做改变
红色 ##执行失败
5、ansible中的常用模块
1.command
功能
在远程主机执行命令,此模块为默认模块
常用参数
chdir | 执行命令前先进入到指定目录 |
cmd | 运行命令指定 |
creates | 如果文件存在将不运行 |
removes | 如果文件存在将运行 |
free_form | 在远程主机中执行的命令,此参数不需要加 |
##注意##
Linux中的很多通配符在command模块中不支持
不支持所以并未删除
2.shell
功能
和command功能类似
常用参数
chdir | 执行命令前先进入到指定目录 |
cmd | 运行命令指定 |
creates | 如果文件存在将不运行 |
removes | 如果文件存在将运行 |
free_form | 在远程主机中执行的命令,此参数不需要加 |
executable | 指定执行环境,默认为sh |
实例
新建文件
文件更改时间不变,文件存在将不运行
文件更改时间变化,文件存在将运行
executable ##指定执行环境,默认为sh echo $$可以查看当前进程
3.script
功能
在ansible主机中写好的脚本在受控主机中执行
实例
4.copy
功能
从ansible主机复制文件到受控主机
常用参数
src | 源文件 |
dest | 目的地文件 |
owner | 指定目的地文件所有人 |
group | 指定目的地文件所有组 |
mode | 指定目的地文件权限 |
backup=yes | 当受控主机中存在文件时备份原文件 |
content | 指定文本内容直接在受控主机中生成文件 |
实例
[devops@nodea111 .ansible]$ ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos mode=700' ##更改westos权限为700
[root@nodeb211 mnt]# ll ##查看
[devops@nodea111 .ansible]$ ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos mode=700 group=westos owner=westos' ##更改权限、所有组、所有人
[root@nodeb211 mnt]# ll
total 4
-rwx------. 1 westos westos 9 Nov 30 11:45 westos
[devops@nodea111 .ansible]$ ansible westos -m copy -a 'content="hello westos" dest=/mnt/westos backup=yes' ##指定文本内容并备份原文件
[root@nodeb211 mnt]# cat westos
hello westos
[root@nodeb211 mnt]# cat westos.14752.2021-11-30@11:50:50~
hostname
5.fetch
功能
从受控主机把文件复制到ansible主机,但不支持目录
常用参数
src | 受控主机的源文件 |
dest | 本机目录 |
flat | 基本名称功能 |
实例
[devops@nodea111 .ansible]$ ansible westos -m fetch -a 'src=/mnt/westos dest=/tmp'
[devops@nodea111 .ansible]$ ls /tmp/172.25.254.211/mnt/westos ##可以看到名称为来源主机ip的目录
[devops@nodea111 .ansible]$ cat /tmp/172.25.254.211/mnt/westos
hello westos
[devops@nodea111 .ansible]$ ansible westos -m fetch -a 'src=/mnt/westos dest=/tmp/haha flat=yes' ##复制到指定目录下
[devops@nodea111 .ansible]$ cat /tmp/haha
6.file
功能
设置文件的属性
常用参数
path | 指定文件名称 |
state | 指定操作状态 touch 建立 absent 删除 directory 递归 link 建立链接 hard |
mode | 设定权限 |
owner | 设定文件用户 |
group | 设定文件组 |
src | 源文件 |
dest | 目标文件 |
recurse=yes | 递归更改 |
实例
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=touch'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=absent'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=absent'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory mode=777'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory mode=777 owner=westos group=westos'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/mnt/westos state=touch'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'src=/mnt/westos dest=/mnt/westos1 state=hard'
[devops@nodea111 .ansible]$ ansible westos -m file -a 'src=/mnt/westos dest=/mnt/westos2 state=link'
7.archive
功能
压缩
常用参数
path | 打包目录名称 |
dest | 生成打包文件名称 |
format | 打包格式 |
owner | 指定文件所有人 |
mode | 指定文件权限 |
ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777'
8.unarchive
功能
解压缩
常用参数
copy | 默认为yes 从ansible主机复制文件到受控主机 设定为no 从受控主机中寻找src源文件 |
remote_src | 功能同copy相反 设定为yes 表示包在受控主机 设定为no 表示包在ansible主机 |
src | 包路经,可以是ansible主机,也可以是受控主机 |
dest | 受控主机目录 |
mode | 加压后文件权限 <copy=yes> |
[devops@nodea111 .ansible]$ ansible westos -m unarchive -a 'src=./usr.tar.bz2 dest=/opt owner=westos copy=yes'
[root@nodeb211 usr]# ls /opt/usr/bin
[devops@nodea111 .ansible]$ ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/opt remote_src=yes' ##设定为yes 表示包在受控主机,解压到受控机
9.hostname
功能
管理主机名称
常用参数
name ##指定主机名称
实例
[devops@nodea111 .ansible]$ ansible westos -m hostname -a 'name=nodeb.westos.org'
10.cron
功能
计划任务
常用参数
minute | 分钟 |
hour | 小时 |
day | 天 |
month | 月 |
weekday | 周 |
name | 任务名称 |
job | 任务脚本或命令 |
disabled | yes 禁用计划任务 no 启动计划任务 |
state | absent 删除计划任务 |
[devops@nodea111 .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=55 hour=14'
[devops@nodea111 .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=*/2 hour=14'
[devops@nodea111 .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test disabled=yes'
[devops@nodea111 .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test disabled=no
[devops@nodea111 .ansible]$ ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test state=absent'
11.yum_repository
功能
配置系统软件仓库源文件
常用参数
name | 指定仓库名称 |
baseurl | 指定源路径 |
description | 指定仓库描述 |
file | 指定仓库文件名称 |
enabled | 仓库是否启用 |
gpgcheck | 仓库是否检测gpgkey |
state12.dnf | 默认值present建立 absent 删除 |
[devops@nodea111 .ansible]$ ansible westos -m file -a 'path=/etc/yum.repos.d/westos.repo state=absent'
[devops@nodea111 .ansible]$ ansible westos -m yum_repository -a 'file=westos name=AppStream description="rhel8.2 AppStream" baseurl=http://172.25.254.250/rhel8.2/AppStream gpgcheck=no enabled=yes gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release'
[devops@nodea111 .ansible]$ ansible westos -m yum_repository -a 'file=westos name=BaseOS description="rhel8.2 BaseOS" baseurl=http://172.25.254.250/rhel8.2/BaseOS gpgcheck=no enabled=yes gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
实例 ![](https://img-blog.csdnimg.cn/354a55ccd1264c91912ab3c46f7d0945.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATW9yZ2FuaXRl,size_20,color_FFFFFF,t_70,g_se,x_16)
12.dnf
功能
管理系统中的dnf仓库及管理软件
常用参数
name | 指定包 |
state | 指定动作 present 安装 latest 更新 absent 删除 |
list | 列出指定信息 httpd installed all available |
disable_gpg_check | 禁用gpgkey检测 |
enablerepo | 指定安装包来源 |
disablerepo | 禁用安装包来源 |
实例
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=present' ##安装[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=absent' ##卸载
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=latest' ##更新
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=absent autoremove=no'
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=absent autoremove=yes' ##删除所有相关文件
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=latest'
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=httpd state=absent enablerepo=AppStream'
13.service
功能
管理系统服务状态
常用参数
name | 指定服务名称 |
state | 指定对服务的动作 started stoped restarded reloaded |
enabled | 设定服务开机是否自启动 yes 开机启动 no 开机不启动 |
实例
给被控机联网
[root@nodeb211 yum.repos.d]# ip route add default via 172.25.254.250
[root@nodeb211 yum.repos.d]# echo nameserver 114.114.114.114 > /etc/resolv.conf
[root@nodeb211 yum.repos.d]# ping www.baidu.com
在百度搜索epel for rhel8,找到下载网址
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm state=present' ##安装epel源
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name=epel-release state=absent'##删除
[devops@nodea111 .ansible]$ ansible westos -m dnf -a 'name="@Virtualization Client" state=present' ##dnf group list --hidden 查询到
[devops@nodea111 .ansible]$ ansible westos -m service -a 'name=httpd state=started enabled=yes'
[root@nodeb211 ~]# systemctl status httpd
14.firewalld
常用参数
zone | 火墙副域 |
service | 服务名称 |
permanent | 永久生效 |
state | 指定对服务的动作 |
enabled | 允许 |
disbled | 拒绝 |
immediate | 立即生效 |
实例
[devops@nodea111 .ansible]$ ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'
15.user
功能
模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
常用参数
name | 必须参数,用于指定要操作的用户名称 |
group | 指定用户所在的基本组 |
groups | 指定用户所在的附加组 |
append | 指定添加附加组默认值为no |
shell | 指定用户的默认 shell |
uid | 指定用户的 uid 号 |
comment | 指定用户的注释信息 |
state | 用于指定用户是否存在于远程主机 present 建立 absent 删除 |
remove | 当删除用户是删除用户家目录,默认值为no |
password | 此参数用于指定用户的密码。但密码为明文 可以用openssl password -6 '密码'生成加密字符 |
generate_ssh_key | 生成sshkey |
实例
[devops@nodea111 .ansible]$ ansible westos -m user -a 'name=test'
[devops@nodea111 .ansible]$ ansible westos -m user -a 'name=test uid=6666'
[devops@nodea111 .ansible]$ ansible westos -m user -a 'name=t
[devops@nodea111 .ansible]$ ansible westos -m user -a 'name=test groups=westos append=yes'est groups=72
[devops@nodea111 .ansible]$ ansible westos -m user -a 'name=test comment="test user"'
[devops@nodea111 .ansible]$ openssl passwd -6
[devops@nodea111 .ansible]$ ansible westos -m user -a "name=test password='\$6\$GOcfEzuS1DCcDurG$doGWB7VhHsTyr10qdQkwwspt/YROi9Pwkb37Yx4GcyKxEnfvio3w0//5uDzZLxOfrgPdkjzEsJmcb8TmWamFa.'"
[root@nodeb211 ~]# cat /etc/shadow
[devops@nodea111 .ansible]$ ansible westos -m user -a "name=test password='\$6\$GOcfEzuS1DCcDurG$doGWB7VhHsTyr10qdQkwwspt/YROi9Pwkb37Yx4GcyKxEnfvio3w0//5uDzZLxOfrgPdkjzEsJmcb8TmWamFa.' generate_ssh_key=yes"
[root@nodeb211 ~]# cd /home/test
[root@nodeb211 test]# cd .ssh
[root@nodeb211 .ssh]# ls
id_rsa id_rsa.pub
[devops@nodea111 .ansible]$ ansible westos -m user -a 'name=test state=absent'
16.group
功能
group 模块可以帮助我们管理远程主机上的组。
常用参数
name | 用于指定要操作的组名称 |
state | 用于指定组的状态 present 建立 absent 删除 |
gid | 用于指定组的gid |
实例
[devops@nodea111 .ansible]$ ansible westos -m group -a 'name=test'
[devops@nodea111 .ansible]$ ansible westos -m group -a 'name=test gid=6666'
[root@nodeb211 etc]# cat /etc/group
[devops@nodea111 .ansible]$ ansible westos -m group -a 'name=test state=absent'
17.lineinfile
path | 指定要操作的文件 |
line | 指定文本内容。 "|+" 表示格式化输入 |
regexp | 用正则表达式匹配对应的行当替换文本时 如果有多行文本都能被匹配 则只有最后面被匹配到的那行文本才会被替换 当删除文本时,如果有多行文本都能被匹配 |
state | 当想要删除对应的文本时需要将state参数的值设置为absent state的默认值为present。 |
backrefs | 当内容无匹配规则时不对文件做任何更改,默认值为no 向后引用regexp变量信息 |
insertafter | 借助insertafter参数可以将文本插入到“指定的行”之后 nsertafter参数的值可以设置为EOF或者正则表达式 |
insertbefore | 借助insertbefore参数可以将文本插入到“指定的行”之前 insertbefore参数的值可以设置为BOF或者正则表达式 |
backup | 是否在修改文件之前对文件进行备份 |
create | 当要操作的文件并不存在时,是否创建对应的文件 |
实例
[devops@nodea111 .ansible]$ ansible westos -m copy -a 'content="hello westos\nhello linux\nhello test\n" dest=/mnt/westos'
[root@nodeb211 mnt]# cp westos westos1
[devops@nodea111 .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos" '
[root@nodeb211 mnt]# cat westos
hello westos
hello linux
hello westos
[devops@nodea111 .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" state=absent '
[root@nodeb211 mnt]# cat westos
[devops@nodea111 .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^test" line="westos lee" '
[root@nodeb211 mnt]# cat westos
hello westos
hello linux
hello test
westos lee
[devops@nodea111 .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes '##找出 hello westos替换为hello
[root@nodeb211 mnt]# cat westos
hello
hello linux
hello test
westos lee
[root@nodeb211 mnt]# cat westos1 > westos
[devops@nodea111 .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="#####ok#####" insertbefore="BOF" '
[root@nodeb211 mnt]# cat westos
#####ok#####
hello westos
hello linux
hello test
[devops@nodea111 .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="####ok####" insertbefore="hello" '
[root@nodeb211 mnt]# cat westos
#####ok#####
hello westos
hello linux
####ok####
hello test
[devops@nodea111 .ansible]$ cat test.yml
- name: test
hosts: westos
tasks:
- lineinfile:
path: /mnt/westos
line:
123
456
789
[devops@nodea111 .ansible]$ ansible-playbook test.yml
[root@nodeb211 mnt]# cat westos
#####ok#####
hello westos
hello linux
####ok####
hello test
123 456 789
[devops@nodea111 .ansible]$ cat test.yml
- name: test
hosts: westos
tasks:
- lineinfile:
path: /mnt/westos
line: |+
123
456
789
[devops@nodea111 .ansible]$ ansible-playbook test.yml
[root@nodeb211 mnt]# cat westos
#####ok#####
hello westos
hello linux
####ok####
hello test
123 456 789
123
456
789
18.replace
功能
replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
常用参数
path | 指定要操作的文件 |
regexp | 指定一个正则表达式 文件中与正则匹配的字符串将会被替换 |
replace | 指定最终要替换成的字符串 |
backup | 是否在修改文件之前对文件进行备份,最好设置为yes |
实例
[devops@nodea111 .ansible]$ ansible westos -m replace -a 'path=/mnt/westos regexp="hello" replace="lee" backup=yes'
19.setup
功能
setup模块用于收集远程主机的一些基本信息
常用参数
filter ##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息
实例
[devops@nodea111 .ansible]$ ansible all -m setup
[devops@nodea111 .ansible]$ ansible all -m setup -a 'filter="ansible_fqdn"' ##查看主机名相关信息
20.debug
功能
调试模块,用于在调试中输出信息
常用参数
msg | 调试输出的消息 |
var | 将某个任务执行的输出作为变量传递给debug模块 debug会直接将其打印输出 |
verbosity | debug的级别(默认是0级,全部显示) |
实例
[devops@nodea111 .ansible]$ ansible westos -m debug -a 'msg=hello'
172.25.254.211 | SUCCESS => {
"msg": "hello"
}
[devops@nodea111 .ansible]$ ansible westos -m debug -a 'var=hello' ##不可以直接写
172.25.254.211 | SUCCESS => {
"hello": "VARIABLE IS NOT DEFINED!"
}
[devops@nodea111 .ansible]$ cat test.yml
- name: test
hosts: westos
tasks:
- name: debug
debug:
var: ansible_facts['fqdn']
[devops@nodea111 .ansible]$ ansible-playbook test.yml
PLAY [test] *****************************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [172.25.254.211]
TASK [debug] ****************************************************************************
ok: [172.25.254.211] => {
"ansible_facts['fqdn']": "nodeb211.westos.org"
}
PLAY RECAP ******************************************************************************
172.25.254.211 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[devops@nodea111 .ansible]$ cat test.yml
- name: test
hosts: westos
tasks:
- name: debug
debug:
var: ansible_facts['fqdn']
verbosity: 1 ##debug的级别(默认是0级,全部显示)
[devops@nodea111 .ansible]$ ansible-playbook test.yml
PLAY [test] *****************************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [172.25.254.211]
TASK [debug] ****************************************************************************
skipping: [172.25.254.211]
PLAY RECAP ******************************************************************************
172.25.254.211 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0