一、ansible实现管理的方式
1、Ad-Hoc #ansible命令直接操作管理,用于临时操作
2、playbook #ansible脚本,由于大型项目操作
二、ansible帮助
ansible-doc ##显示模块帮助的指令
#格式
ansible-doc [参数] [模块...]
#常用参数
ansible-doc -l ##列出可用模块
ansible-doc -s ##显示指定模块的playbook片段
3.ansible命令运行方式及常用参数
命令格式:
ansible 清单 -m 模块 -a 模块参数
常用参数:
--version ##显示版本
-m module ##指定模块,默认为command模块
-v ##详细过程 -vv v越多会显示更详细的执行过程
--list ##显示主机列表,也可以用--list-hosts
-k ##提示输入ssh连接密码,默认key认证
-C ##预执行检测
-T ##执行命令的超时时间,默认10s
-u ##指定远程执行的用户
-b ##执行sudo切换身份操作
-become-user=USERNAME ##指定sudo的用户
-K ##提示输入sudo密码
4.ansible的基本颜色代表信
绿色 ##执行成功但为对远程主机做任何改变
黄色 ##执行成功并对远程主机做改变
红色 ##执行失败
5.ansible中的常用模块
1、Ansible ping 模块实战
Ansible 最基础的模块为 ping 模块,主要用于判断远程客户端是否在线,用于 ping本身服务器,返回值为 changed、ping。
这里的anible主机和node1被控机做了ssh免密码登陆,所以不在需要-k进行密码登陆
(1) Ansible ping 服务器状态,如图所示:
ansible all -m ping -u wang
2、Ansible command 模块实战
Ansible command 模块为 ansible 默认模块,主要用于执行 Linux 基础命令,可以执行远程服务器命令执行、任务执行等操作。
Command 模块使用详解:
Chdir | 执行命令前,切换到目录; |
---|---|
Creates | 当该文件存在时,则不执行该步骤; |
Executable | 换用 shell 环境执行命令; |
Free_form | 需要执行的脚本; |
Removes | 当该文件不存在时,则不执行该步骤; |
Warn | 如果在 ansible.cfg 中存在告警,如果设定了 False,不会警告此行。 |
Ansible command 模块企业常用案例如下:
(1) Ansible command 模块远程执行 date 命令,执行结果如图所示:
ansible -k -i /etc/ansible/hosts all -m command -a "date"
(2) Ansible command 模块远程执行 ping 命令,执行结果如图所示:
ansible -u wang all -m command -a "ping -c 1 36.152.44.95"
(3) Ansible Hosts 正则模式远程执行 df -h,执行结果如图所示:
ansible -u wang 172.25.254.17* -m command -a "df -h"
3、Ansible copy 模块实战
Ansible copy 模块主要用于文件或者目录拷贝,支持文件、目录、权限、用户组功能
copy 模块使用详解:
src | Ansible 端源文件或者目录,空文件夹不拷贝 |
---|---|
content | 用来替代 src,用于将指定文件的内容,拷贝到远程文件内 |
dest | 客户端目标目录或者文件,需要绝对路径 |
backup | 拷贝之前,先备份远程节点上的原始文件 |
directory_mode | 用于拷贝文件夹,新建的文件会被拷贝,而老旧的不会被拷贝 |
follow | 支持 link 文件拷贝 |
force | 覆盖远程主机不一致的内容 |
group | 设定远程主机文件夹的组名 |
mode | 指定远程主机文件及文件及的权限 |
owner | 设定远程主机文件夹的用户名 |
Ansible copy 模块企业常用案例如下:
(1) Ansible copy 模块 拷贝文件 操作,src 表示源文件,dest 表示目标目录或者文件,owner指定拥有者,执行结果如图所示:
ansible -u wang all -m copy -a 'src=/etc/passwd dest=/tmp/ mode=755 owner=wang'
(2) Ansible copy 模块 追加内容 操作,content 文件内容,dest 目标文件,owner 指定拥有者,执行结果如图所示:
ansible -u wang all -m copy -a 'content="Hello World" dest=/tmp/jfedu.txt mode=755 owner=wang'
(3) Ansible copy 模块操作,content 文件内容,dest 目标文件,owner 指定拥有者,backup=yes 开启备份,执行结果如图所示:
ansible -u wang all -m copy -a 'content="Hello World" dest=/tmp/beifen.txt backup=yes mode=755 owner=wang'
4、Ansible fetch 模块实战
Ansible fetch 模块主要用于从受控主机把文件复制到ansible主机,但是不支持目录复制
fetch模块使用详解:
src | 受控主机的源文件 |
---|---|
dest | 本机目录 |
flat | 基本名称功能 |
(1) Ansible fetch 模块操作,src受控主机的源文件 ,dest 目标文件,执行结果如图所示:
(2) Ansible fetch 模块操作,src受控主机的源文件 ,dest 目标文件,flat基本名称,执行结果如图所示:
5、Ansible file 模块实战
Ansible file 模块主要用于对文件的创建、删除、修改、权限、属性的维护和管理
File 模块使用详解:
src | Ansible 端源文件或者目录 |
---|---|
follow | 支持 link 文件拷贝 |
force | 覆盖远程主机不一致的内容 |
group | 设定远程主机文件夹的组名 |
mode | 指定远程主机文件及文件及的权限 |
owner | 设定远程主机文件夹的用户名 |
path | 目标路径,也可以用 dest,name 代替 |
state | 状态包括:file、link、directory、hard、touch、absent |
attributes | 文件或者目录特殊属性 |
Ansible file 模块企业常用案例如下:
(1) Ansible file 模块操作,path 表示目录的名称和路径, state=directory 表示
创建目录,执行结果如图所示:
ansible -u wang all -m file -a "path=/tmp/`date +%F` state=directory mode=755"
(2) Ansible file 模块操作,path 表示目录的名称和路径, state=touch 表示创建文件,执行结果如图所示:
ansible -u wang all -m file -a "path=/tmp/file.txt state=touch mode=755"
7.Ansible unarchive 模块实战
Ansible unarchive 模块用于解压缩
常用参数:
copy | 默认为yes 从ansible主机复制文件到受控主机 / 设定为no 从受控主机中寻找src源文件 |
---|---|
remote_src | 功能同copy且相反 / 设定为yes 表示包在受控主机 / 设定为no表示包在ansible主机 |
src | 包路径,可以使ansible主机也可以使受控主机 |
dest | 受控主机目录 |
mode | 加压后文件权限 <copy=yes> |
示例:
1.先用 tar -zcvf etc.tar.gz /etc 在ansible主机制作一个压缩包
2.向受控主机解压 etc.tar.gz
ansible -u wang all -m unarchive -a 'src=/etc/ansible/etc.tar.gz dest=/tmp copy=yes'
8.Ansible archive 模块实战
作用压缩
常用参数:
path | 打包目录名称 |
---|---|
dest | 声称打包文件名称 |
format | 打包格式 |
owner | 指定文件所属人 |
mode | 指定文件权限 |
示例:
ansible -u wang all -m archive -a 'path=/etc dest=/tmp/6666666.tar.gz format=gz'
9.Ansible hostname 模块实战
作用管理主机名称
常用参数
name | 指定主机名称 |
---|
实例
ansbile 172.25.254.174 -m hostname -a 'name=lee.westos.com'
10.cron
作用计划任务
常用参数:
minute ##分钟
hour ##小时
day ##天
month ##月
weekday ##周
name ##任务名称
job ##任务脚本或命令
disabled ##yes 禁用计划任务
##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
11.yum_repository
#作用
配置系统软件仓库源文件
常用参数:
name ##指定仓库名称
baseurl ##指定源路径
description ##指定仓库描述
file ##指定仓库文件名称
enabled ##仓库是否启用
gpgcheck ##仓库是否检测gpgkey
state ##默认值present 建立
#absent 为删除
示例:
ansible all -m yum_repository -a "name=AppStream baseurl=http://172.25.254.73/westos/AppStream description=AppStream_westos gpgcheck=no file=westos_test" -k
ansible all -m yum_repository -a "name=BasmOS baseurl=http://172.25.254.73/westos/BasnOS description=BasmOS_westos gpgcheck=no file=westos_test" -k
ansible all -m yum_repository -a "name=AppStream file=westos_test state=absent" -k
12.dnf
作用管理系统中的dnf仓库及管理软件
常用参数
name ##指定包
state ##指定动作
#present 安装
#latest 更新
#absent 删除
list ##列出指定信息
# httpd
# installed
# all
# available
disable_gpg_check #禁用gpgkey检测
enablerepo ##指定安装包来源
disablerepo ##禁用安装包来源
示例:
ansible -u wang all -m dnf -a "name=httpd state=latest"
ansible -u wang all -m dnf -a 'name="httpd,mariadb-server" state=present'
ansible -u wang all -m dnf -a 'name=httpd state=absent'
ansible -u wang all -m dnf -a 'name=httpd state=absent autoremove=no'
ansible -u wang all -m dnf -a 'name=httpd state=present enablerepo=AppStream'
ansible -u wang all -m dnf -a 'name="*" state=latest'
ansible -u wang all -m dnf -a 'name=http://172.25.254.250/software/wps-office-xxx.rpm state=present'
ansible -u wang all -m dnf -a 'name="@Virtual Tools" state=present'
13.service
作用管理系统服务状态
常用参数
name ##指定服务名称
state ##指定对服务的动作
#started
#stoped
#restarted
#reloaded
enabled ##设定服务开机是否启动
#yes开启启动
#no开机不启动
实例 :
ansible all -m service -a "name=httpd state=started enabled=yes"
ansible all -m service -a "name=httpd state=restarted enabled=yes"
14. firewalld
常用参数
zone ##火墙的域
service ##服务名称
permanent ##永久生效
state
enabled ##允许
disabled ##拒绝
immediate ##立即生效
15. user
作用:
模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
常用参数
name ##必须参数,用于指定要操作的用户名称。
group ##指定用户所在的基本组。
gourps ##指定用户所在的附加组。
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=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'
16. group
作用:
group 模块可以帮助我们管理远程主机上的组。
常用参数
name ##用于指定要操作的组名称。
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'
17. lineinfile
path ##指定要操作的文件。
line ##指定文本内容。 "|+" 表示格式化输入
regexp ##使用正则表达式匹配对应的行当替换文本时
##如果有多行文本都能被匹配
##则只有最后面被匹配到的那行文本才会被替换
##当删除文本时,如果有多行文本都能被匹配
##这么这些行都会被删除。
state ##当想要删除对应的文本时需要将state参数的值设置为absent
#state的默认值为present。
backrefs ##当内容无匹配规则时不对文件做任何更改,默认值为no
##向后引用regexp变量信息
insertafter ##借助insertafter参数可以将文本插入到“指定的行”之后
##insertafter参数的值可以设置为EOF或者正则表达式
insertbefore ##借助insertbefore参数可以将文本插入到“指定的行”之前
#insertbefore参数的值可以设置为BOF或者正则表达式
backup ##是否在修改文件之前对文件进行备份。
create ##当要操作的文件并不存在时,是否创建对应的文件。
在westos-node1被控机中编写westos文本
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"'
18、replace
作用
replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
常用参数
path ##指定要操作的文件
regexp ##指定一个正则表达式
#文件中与正则匹配的字符串将会被替换。
replace ##指定最终要替换成的字符串。
backup ##是否在修改文件之前对文件进行备份,最好设置为yes。
实例
ansible all -m replace -a 'path=/mnt/westos regexp="WESTOS" replace="westos_lee" backup=yes'
19、setup
作用
setup模块用于收集远程主机的一些基本信息
常用参数
filter ##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。
##实例
ansible all -m setup -k
ansible all -m setup -a "filter='ansible_all_ipv4_addresses'" -k
20、debug#
作用
调试模块,用于在调试中输出信息
常用参数:
msg: ##调试输出的消息
var: ##将某个任务执行的输出作为变量传递给debug模块
##debug会直接将其打印输出
verbosity: ##debug的级别(默认是0级,全部显示)