ansible的管理方式

ansible实现管理方式
Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ansible脚本,主要用于大型项目场景,需要前期的规划。
playbook方式

vim test.yml
---
- name: test
  hosts: westos
  tasks:
    - name: test #可不写
      shell: hostname

ansible-playbook test.yml
ansible-playbook test.yml -vv #显示详细信息
Ad-Hoc方式
ansible westos -m shell -a "hostname"
ansible-doc  #显示模块帮组的命令
ansible-doc [参数] [模块...]

常用参数

-l #列出可用模块
-s #显示指定模块的playbook片段
ansible-doc ping #查看ping帮助
格式
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密码

ansible的基本颜色代表
绿色 执行成功但未对远程主机做任何改变
黄色 执行成功并对远程主机做改变
红色 执行失败

A、command模块 shell模块 script模块

ansible-doc command -s #查看command命令
chdir 切换目录
stdin 输入一个值
ansible westos -m command -a 'chdir=/etc cat passwd'
ansible westos -m command -a 'creates=/mnt/file chdir=/etc cat passwd'  
#如果文件不存在就做
ansible westos -m command -a 'removes=/mnt/file cat passwd'  #如果文件存在就做
ansible westos -m command -a 'ps ax | grep $$' #当前的命令是cmd,有些符号用不了,因此,要在shell下运行
ansibele westos -m shell -a 'pa ax | grep $$'  $$当前进程
ansible wetsos -m shell -a 'executable=/bin/bash ps ax | grep $$' #指定执行环境,executable只有在shell模块这才有
free_form #在远程主机中执行的命令,此参数不需要加

script模块
在受控主机中写好的脚本在受控主机中执行
scr #源文件
dest #目的地文件
owner #指定目的地文件所有人
group #
mode #指定目的地文件权限
backup=yes #当受控主机中存在文件时备份原文件
content #指定文本内容直接在受控主机中生成文件

ansible all -m script -a "/mnt/westos.sh" -k

B、copy模块

cd /root/.ansible/
ls   #执行以下命令时,配置目录如果不在当前目录下,读取的便是默认的。
ansible westos -m copy -a 'scr=/mnt/westos dest=/mnt'
ansible wetsos -m copy -a 'scr=/mnt/westos dest=/mnt mode=755' #指定权限
ansible westos -m copy -a 'scr=/mnt/westos dest=/mnt mode=755 owner=westos group=westos' #指定用户和组
ansible westos -m copy -a 'content="hello westos\n" dest=/mnt/jin mode=755 owner=westos group=westos' 
ansible westos -m copy -a 'scr=/mnt/westos dest=/mnt mode=755 owner=westos group=westos backup=yes'  #备份原始文件

C、fetch模块
从受控主机把文件复制到ansible主机,但不支持目录。
常用参数
src 受控主机的源文件
dest 本机目录
flat 基本名称功能,只把文件名称拷贝过来。?

ansible all -m fetch -a "src=/etc/hostname dest=/mnt" -k
ansible all -m fetch -a "src=/etc/hostname dest=/mnt flat=yes" #同时使用两个主机时,后面的会把之前的覆盖掉。flat=yes表示dest后跟的是文件,flat=no表示dest后面跟的是目录。

D、file
功能:设置文件的属性
path 指定文件名称
state 指定操作状态
touch 建立
absent 删除
directory 递归
link 建立链接
hard 硬链接
mode 设定权限
owner 设定文件用户
group 设定文件组
src 源文件
dest 目标文件
recurse=yes 递归更改

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 westos -m file -a 'path=/mnt/westos state=touch mode=777 owner=westos group=westos'
ansible westos -m shell -a 'ls -l /mnt' 
ansible westos -m file -a 'src=/mnt/westosfile dest=/mnt/westosjin state=link'

ansible westos -m file -a 'path=/mnt/westos/file state=touch'
ansible westos -m shell -a 'ls - lR /mnt'
ansible westos -m file -a 'path=/mnt/westos mode=777 recurse=yes'递归目录本身和其中文件的权限
anisble westos -m file -a 'src=/mnt/westoslee dest=/mnt/westostest state=hard'建立硬链接
ansible westos -m shell -a 'ls -li /mnt'查看文件属性
硬链接链接起来的两个文件的id是相同的。

E、打包压缩模块
archive:作用:压缩
常用参数
path 打包目录名称
dest 生成打包文件名称
format 打包格式
owner 指定文件所属人
mode 指定文件权限

ansible westos -m shell -a 'ls /mnt'
ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=755'
ansible westos -m shell -a 'ls /mnt'

unarchive
功能:解压缩
copy 默认为yes 从ansible主机复制文件到受控主机,设定为no,从受控主机中寻找src源文件,解压。解压时,文件可以在受控主机,也可以在ansible主机中。如果解压文件已经在受控主机中了,设定为no。否则要复制到受控主机中,设定为yes。
remote_src 功能同copy且相反,设定为yes表示包在受控主机
src 包路径,可以是ansible主机也可以是受控主机
dest 受控主机目录
mode 加压后文件权限,设置mode时,copy是no时,mode是不生效的。

ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt owner=jin' -k
ansible all -m unarchive -a 'src=/opt/etc.tar.gz dest=/mnt copy=no' #解压文件在受控主机
如果要删除文件
ansible all -m file -a 'path=/mnt/etc state=absent'

F、定时任务cron
常用参数
minute 分钟
hour 小时
day 天
month 月
weekday 周
name 任务名称
job 任务脚本或命令
disabled yes 禁止计划命令 no 启动计划命令
state absent 删除计划任务

ansible westos -m cron -a "job='echo hello' name=test minute=*/2 " -k
ansible westos -m cron -a "job='echo hello' name=test disabled=yes" -k
ansible westos -m cron -a "job='echo hello' name=test state=absent" -k

G、hostname模块

ansible ip -m hostname -a 'name=westosa.westos.org'

K、yum_repository模块
配置系统软件仓库源文件
常用参数
name 指定仓库名称
baseurl 指定源路径
description 指定仓库描述
file 指定仓库文件名称
enabled 仓库是否启用
gpgcheck 仓库是否检测gpgcheck
state absent 为删除,默认值present建立

ansible westos -m yum_repository -a "name=AppStream baseurl=http://172.25.254.250/rhel8.2/Appstream description=AppStream_westos gpgcheck=no file=westos" -k
ansible westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.250/rhel8.2/BaseOS descriptong=BaseOS_westos gpgcheck=no file=westos" -k
ansible westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.250/rhel8.2/BaseOS descriptong=BaseOS_westos enabled=0 file=westos" -k

H、dnf模块
作用:管理系统中的dnf仓库及管理软件
常用参数
name 指定动作
state 指定动作 present 安装 latest 更新 absent 删除
list 列出指定信息 httpd installed all available
disable_gpg_check 禁用gpgcheck检测
enablerepo 指定安装包来源
disablerepo 禁用安装包来源

ansible westos -m dnf -a 'name=vsftpd state=latest'
ansible westos -m dnf -a 'name="httpd,dhcp-server" state=latest disable_gpg_check=yes'
ansible westos -m dnf -a 'name=http state=absent autoremove=yes'卸载软件的时候,同时卸载其依赖性
ansible westos -m dnf -a 'name="@Virtualization Tools" state=present disable_gpg_check=yes'安装组件

I、service模块
作用:管理系统服务状态
name 指定服务名称
state 指定服务的动作 started stoped restarted reloaded
enabled 设定服务开机是否启动 yes 开启启动 no 开机不启动

ansible westos -m service -a 'name=vsftpd state=restarted'
ansible westos -m shell -a 'systemctl status vsftpd'

L、firewalld模块
zone 火墙的域
service 服务名称
Immediate 立即

anisble westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'
ansible westos -m shell -a 'firewall-cmd --list-all'

M、user模块
name 必须参数,用于指定要操作的用户名称
group 制定个用户所在的基本组
gourps 指定用户所在的附加组
append 指定添加付家组默认值为no
shell 指定用户的默认shell
uid 指定用户的uid号

remove 家目录和用户都删掉了,当删除用户时删除用户家目录,默认值为no
password 当参数用于指定用户的密码,但密码为明文,可以用openssl password -6 '密码’生成加密字符
comment 说明,用户的注释信息
generate_ssh_key 生成sshkey,在生成用户的时候,自动生成ssh自动加密文件

watch -n 1 'tail -n 3 /etc/passed /etc/group /etc/shadow ; echo === ; ls -l /home'
ansible westos -m user -a 'name=test state=absent'删除用户test
ansible westos -m user -a 'name=test state=present uid=6666 comment="test user" shell=/bin/sh'
ansible westos -m user -a 'name=test state=present uid=6666 group=6666 commnent="test user" shell=/bin/sh group=apache append=yes'表示添加附加组,又不改变原来的组
openssl passwd -6 生成加密字符串
ansible westos -m user -a 'name=test state=present uid=6666 group=6666 commnent="test user" shell=/bin/sh group=apache append=yes passwd="加密字符串"'
ansible westos -m user -a 'name=test state=present uid=6666 group=6666 commnent="test user" shell=/bin/sh group=apache append=yes generate_ssh_key=yes home=/home/jin'更改家目录之后,会建立一个文件

N、group模块
作用:管理远程主机上的组
常用参数:
name 指定要操作的组名称
state 指定组的状态 present 建立 absent 删除
gid 指定组的gid

ansible westos -m group -a 'name=westosjin state=absent'
ansible westos -m group -a 'name=westosjin gid=8888'

O、lineinfile模块
作用:载文件
path 指定要操作的文件
line 指定文本内容
regexp 使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本能被匹配。当删除文本时,如果有多行文本能被匹配,那么只有最后一行都会被删除
state 当需要删除对应的文本时,需要将state参数设置为absent,state的默认值为present
backrefs 当内容无匹配规则时不对文件做任何更改,默认值为no。
insertafter 借助Insertafter参数可以将文本插入到指定的行之后,insertafter参数的值可以设置为EOF或正则表达式
insertbefore 借助insertbefore 参数的值可以将文本插入到指定的行之前,insertbefore参数的值可以设置为BOF或者正则表达式
backup 是否在修改文件之前对文件进行备份
create 当要操作的文件并不存在时,是否创建对应的文件。

在远程主机中建立
#vim /mnt/westos
#hello westos
#hello test 
#hello linux
watch -n 1 cat /mnt/*
在ansible主机中
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello westos" create=yes'
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello test" create=yes'
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello linux" create=yes'
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello test1" regexp="test"'
ansible westos -m lineinfile -a 'path=/mnt/westos state=absent regexp='test''
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello jin" regexp="test" backrefs=yes' 当regrxp的内容与line中的内容不匹配时,这行命令不生效
ansible westos -m lineinfile -a 'path=/mnt/westos regexp=(h.{4}).*(w.{5}) line="\1" backrefs=yes'
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello lee" insertafter="linux"'
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello java1" insertbefore="lee"'
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello java1" insertbefore=BOF'在最前面添加
ansible westos -m lineinfile -a 'path=/mnt/westos line="hello java2" insertafter=EOF'
ansible westos -m lineinfile -a 'path=/mnt/westos regexp="hello" state=absent backup=yes'

h.{4}表示h后面有4个字符
(h.{4}).*(w.{5})首字符是h后面有4个字符后面一个任意字符以w为首字符后面有5个字符 .*表示分隔符
\1表示第一个变量

P、replace模块
作用:根据指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
path 指定要操作的文件
regexp 指定一个正则表达式,文件中与正则匹配的字符串将会被替换。
replace 指定最终要替换成的字符串
backeup 是否在修改文件前对文件进行备份,最好设置为yes。

ansible westos -m replace -a 'path=/mnt/westos regexp="hello" replace="westos" backup=yes'

Q、setup模块
作用:用于手机远程主机的一些基本信息
filter 用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息

ansible westos -m setup 可以看到所有受控主机的信息
ansible westos -m setup -a 'filter="ansible _all_ipv4_addresses"'

R、debug模块
作用:用于在调试中输出信息
msg: 调试输出的消息
var: 将某个人物执行的输出作为变量传递给debug模块,debug模块会直接将其打印输出
verbosity: debug 的级别(默认是0级,全部显示)

ansible westos -m debug -a 'msg="hello world"'
ansible westos -m debug -a 'msg="hello world" verbosity=0'
vim westos.yum
---
- name: test
  hosts: westos
  tasks:
   - debug:
       var: ansible_all_ipv4_addresses
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值