Ansible 第二章 ansible中常用的模块

一.ansible实现管理的方式

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

[devops@ansible .ansible]$ vim test.yml
[devops@ansible .ansible]$ cat test.yml 
- name: test
  hosts: westos
  tasks:
    - name: check hostname
      shell:
        hostname
[devops@ansible .ansible]$ ansible-playbook test.yml 
[devops@ansible .ansible]$ ansible-playbook test.yml -v  ##详细过程 -vv -vvv更详细过程

 

 二.Ad-Hoc执行方式中如何获得帮助

ansible-doc         ##显示模块帮助的指令
格式:
ansible-doc [参数] [模块...]
常用参数:
-l  ##列出可用模块
-s  ##显示指定模块的playbook片段

 

 

三.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密码

[devops@ansible .ansible]$ vim ansible.cfg ##将第8行注释
  1 [defaults]
  2 inventory      = ~/.ansible/inventory
  3 host_key_checking = False
  4 remote_user = devops
  5 module_name = shell
  6 
  7 [privilege_escalation]
  8# become=True
  9 become_method=sudo
 10 become_user=root
 11 become_ask_pass=False

-u         ##指定远程执行的用户
-b         ##执行sudo切换身份操作.
--become-user=root         ##指定sudo的用户
-K          ##提示输入sudo密码

[devops@ansible .ansible]$ vim ansible.cfg ##去掉第8行注释

 四.ansible的基本颜色代表信息

绿色          ##执行成功但为对远程主机做任何改变
黄色          ##执行成功并对远程主机做改变
红色          ##执行失败

五.ansible中的常用模块

1.command

功能: 在远程主机执行命令,此模块为默认模块

常用参数:

chdir     ##执行命令前先进入到指定目录
cmd  ##运行命令指定
creates  ##如果文件存在将不运行
removes     ##如果文件存在在将运行
free_form ##在远程主机中执行的命令,此参数不需要加



注意:Linux中的很多通配符在command模块中不支持

 

 在受控主机中查看:

 

 2.shell

功能:shell和command功能类似
常用参数:

chdir     ##执行命令前先进入到指定目录
cmd  ##运行命令指定
creates  ##如果文件存在将不运行
removes     ##如果文件存在在将运行
free_form ##在远程主机中执行的命令,此参数不需要加
executable##指定执行环境,默认为sh

 在受控主机中查看:

 

executable  ##指定执行环境,默认为sh 

 3.script

功能: 在ansible主机中写好的脚本在受控主机中执行

[devops@ansible .ansible]$ vim westos.sh
#!/bin/bash
echo $HOSTNAME
[devops@ansible .ansible]$ sh westos.sh
[devops@ansible .ansible]$ ansible westos -m script -a './westos.sh'

 4.copy

功能:从ansible主机复制文件到受控主机

常用参数:

src##源文件
dest##目的地文件
owner##指定目的地文件所有人
group##指定目的地文件所有组
mode##指定目的地文件权限
backup=yes##当受控主机中存在文件时备份原文件
content##指定文本内容直接在受控主机中生成文件
ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos'
ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos mode=700'
ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos mode=700 group=westos  owner=westos'

 backup=yes  ##当受控主机中存在文件时备份原文件
content      ##指定文本内容直接在受控主机中生成文件

ansible westos -m copy -a 'content="hello westos" dest=/mnt/westos backup=yes' 

 在受控主机中查看:

 5.fetch

功能:从受控主机把文件复制到ansible主机,但不支持目录

常用参数:

src受控主机的源文件
dest本机目录
flat基本名称功能 
[root@node1 mnt]# rm -fr *
[root@node1 mnt]# echo $HOSTNAME > westos
[root@node1 mnt]# ls
westos
[root@node1 mnt]# cat westos 
node1.westos.org
[devops@ansible .ansible]$ ansible westos -m fetch -a 'src=/mnt/westos dest=/tmp'
[devops@ansible .ansible]$ ls /tmp
[devops@ansible .ansible]$ cat /tmp/172.25.254.210/mnt/westos

[devops@ansible .ansible]$ ansible westos -m fetch -a 'src=/mnt/westos dest=/tmp/gyy flat=yes'
[devops@ansible .ansible]$ cat /tmp/gyy

6.file

功能:设置文件的属性
常用参数:

path指定文件名称
state

指定操作状态

touch          ##建立
 absent        ##删除
directory      ##递归
 link             ##建立链接

 hard

mode设定权限
owner 设定文件用户
group 设定文件组
src源文件
dest         目标文件
recurse=yes递归更改

实例:

ansible westos -m file -a 'path=/mnt/westos state=touch'
ansible westos -m file -a 'path=/mnt/westos state=absent'
ansible westos -m file -a 'path=/mnt/westos state=directory'
ansible westos -m file -a 'path=/mnt/westos state=absent' ##absent 可以删除目录/删除文件
ansible westos -m file -a 'path=/mnt/westos state=directory mode=777 owner=westos group=westos'    

ansible westos -m file -a 'path=/mnt/westos state=directory mode=700 owner=westos group=westos'       ##只修改了目录本身的权限
ansible westos -m file -a 'path=/mnt/westos state=directory mode=700 recurse=yes'

mode        ##设定权限
owner        ##设定文件用户
group        ##设定文件组

 

 recurse=yes  ##递归更改

 在受控主机中查看:

 

 

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'

在受控主机中查看:
[root@node1 mnt]# ls
etc.tar.gz  westos

 

 8.unarchive

功能:解压缩

copy

默认为yes 从ansible主机复制文件到受控主机,设定为no 从受控主机中寻找src源文件

remote_src

功能同copy且相反,设定为yes 表示包在受控主机

设定为no表示包在ansible主机

src包路径,可以使ansible主机也可以使受控主机
dest受控主机目录
mode加压后文件权限 <copy=yes>

实例:

tar jcf usr.tar.bz2 /usr/bin/
ansible westos -m unarchive -a 'src=./usr.tar.bz2 dest=/opt owner=westos copy=yes'  ##copy=yes,从ansible主机解压文件到受控主机

ansible westos -m file -a 'path=/opt/usr state=absent' ##删除受控主机中之前解压的文件
ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/opt owner=westos remote_src=yes'##从受控机解压文件到受控主机

 

 9.hostname

作用:管理主机名称

常用参数:
        name  ##指定主机名称
实例:

ansible westos -m hostname -a 'name=nodeb.westos.org'

ansible westos -m hostname -a 'name=node1.westos.org'

  在受控主机中查看:

10. cron

作用:计划任务
常用参数:
minute        ##分钟
hour            ##小时
day             ##天
month         ##月
weekday     ##周
name            ##任务名称
job                ##任务脚本或命令
disabled        ##yes 禁用计划任务
                     ##no 启动计划任务

state             ##absent 删除计划任务

实例:
ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=21 hour=15'
ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=*/2 hour=15'  ##每两秒执行一次
ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test disabled=yes'  ##yes 禁用计划任务
ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test disabled=no' ##no 启动计划任务
ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test state=absent'    ##absent 删除计划任务

   在受控主机中查看:

 

11.yum_repository

作用:配置系统软件仓库源文件

常用参数:

name

baseurl
description
file
enabled
gpgcheck
state 

指定仓库名称
指定源路径
指定仓库描述
指定仓库文件名称
仓库是否启用
仓库是否检测gpgkey
默认值present 建立,absent 为删除

实例:

ansible westos -m file -a 'path=/etc/yum.repos.d/westos.repo state=absent' ##先删除之前配置的软件仓库
ansible westos -m yum_repository -a 'file=westos name=AppStream description="local 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'

ansible westos -m yum_repository -a 'file=westos name=BaseOS description="local 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'

ansible westos -m yum_repository -a 'file=westos name=BOS description="local BOS" baseurl=http://172.25.254.250/rhel8.2/BOS gpgcheck=no enabled=yes gpgkey=file///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release'  ##注意:错误的也可以写进去
ansible westos -m yum_repository -a 'file=westos name=BOS state=absent'  ##删除

 12.dnf

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

ansible westos -m dnf -a 'name=httpd state=present'   ##present 安装
ansible westos -m dnf -a 'name=httpd state=absent'  ##absent  删除
ansible westos -m dnf -a 'name=httpd state=latest'  ##latest  更新
ansible westos -m dnf -a 'name=httpd state=absent autoremove=yes' ##全部删除
ansible westos -m dnf -a 'name=httpd state=present'
ansible westos -m dnf -a 'name=httpd state=absent autoremove=no' ##默认为no,只删除包和依赖

ansible westos -m dnf -a 'name=httpd state=latest enablerepo=AppStream'   ##enablerepo  指定安装包来源
ansible westos -m dnf -a 'name=https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm state=present'  ##从网络安装epel源
ansible westos -m dnf -a 'name=epel-release state=absent' ##删除epel源

 给受控机联网:

 

 从网络下载epel源:

 

dnf group list
dnf group list --hidden
ansible westos -m dnf -a 'name="@Virtualization Client" state=present'

 

13.service

作用:管理系统服务状态

常用参数:

name        ##指定服务名称
state        ##指定对服务的动作
                #started
                #stoped
                #restarted
                #reloaded
enabled      ##设定服务开机是否启动
                     #yes开启启动
                     #no开机不启动

实例:

[devops@ansible .ansible]$ ansible westos -m dnf -a 'name=httpd state=latest enablerepo=AppStream'
[devops@ansible .ansible]$ ansible westos -m service -a 'name=httpd state=started enabled=yes' 

 

 14.firewalld

常用参数:
zone                ##火墙的域
service             ##服务名称
permanent        ##永久生效
state
        enabled        ##允许
        disabled        ##拒绝
immediate        ##立即生效

[devops@ansible .ansible]$ ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes' ##火墙允许Apache通过,并且永久生效,immediate表示立即生效
[devops@ansible .ansible]$ ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=disabled 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@ansible .ansible]$ ansible westos -m user -a 'name=test'
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test uid=6767'
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test group=westos'
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test group=test'
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test group=72'
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test group=westos append=yes'
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test comment="test user"' ##指定用户的注释信息
[devops@ansible .ansible]$ openssl passwd -6
Password: 
Verifying - Password: 
$6$JZbY2oHSi7ht12F4$GHNLmWhItFLC7lGqROQUsfvxasawwpvEQWS/ezR.EKqsinz5Xog5MPJw/KxYYQnmJgkK8XGgl3nOt6oHoCd8h1
[devops@ansible .ansible]$ ansible westos -m user -a "name=test password='\$6\$JZbY2oHSi7ht12F4\$GHNLmWhItFLC7lGqROQUsfvxasawwpvEQWS/ezR.EKqsinz5Xog5MPJw/KxYYQnmJgkK8XGgl3nOt6oHoCd8h1'"
[devops@ansible .ansible]$ ansible westos -m user -a 'name=test generate_ssh_key=yes'

[devops@ansible .ansible]$ ansible westos -m user -a 'name=test state=absent' ##删除用户

 

 

 

 16.group

作用:group 模块可以帮助我们管理远程主机上的组。

常用参数:

name用于指定要操作的组名称
state

用于指定组的状态

present        建立

absent         删除

gid用于指定组的gid

实例:

[devops@ansible .ansible]$ ansible westos -m group -a 'name=test'
[devops@ansible .ansible]$ ansible westos -m group -a 'name=test gid=6666'
[devops@ansible .ansible]$ ansible westos -m group -a 'name=test state=absent' ##删除组

[root@node1 ~]# cat /etc/group ##在受控主机中查看组

 17.lineinfile

path指定要操作的文件
line指定文本内容。 "|+" 表示格式化输入
regexp用正则表达式匹配对应的行当替换文本时
如果有多行文本都能被匹配
则只有最后面被匹配到的那行文本才会被替换
当删除文本时,如果有多行文本都能被匹配
state当想要删除对应的文本时需要将state参数的值设置为absent
state的默认值为present。
backrefs当内容无匹配规则时不对文件做任何更改,默认值为no
向后引用regexp变量信息
insertafter借助insertafter参数可以将文本插入到“指定的行”之后
nsertafter参数的值可以设置为EOF或者正则表达式
insertbefore借助insertbefore参数可以将文本插入到“指定的行”之前
insertbefore参数的值可以设置为BOF或者正则表达式
backup是否在修改文件之前对文件进行备份
create当要操作的文件并不存在时,是否创建对应的文件
[devops@ansible .ansible]$ ansible westos -m copy -a 'content="hello westos\nhello test\nhello linux" dest=/mnt/westos' ##\n换行
[root@node1 mnt]# cp westos westos1

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos"' ##如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换
[root@node1 mnt]# cat westos ##只有最后一行文本被替换
hello westos
hello test
hello westos

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" state=absent'
[root@node1 mnt]# cat westos

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="test" line="westos lee"'
[root@node1 mnt]# cat westos
hello westos
westos lee
hello linux

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}.*(w.{5}))" line="\1"'
[root@node1 mnt]# cat westos
\1
westos lee
hello linux

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'
[root@node1 mnt]# cat westos
hello
hello test
hello linux

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="######ok#######"'
[root@node1 mnt]# cat westos      
hello westos
hello test
hello linux
######ok#######

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="######ok#######" insertbefore="BOF"'
[root@node1 mnt]# cat westos
######ok#######
hello westos
hello test
hello linux

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="######ok#######" insertafter="test"'
[root@node1 mnt]#  cat westos
hello westos
hello test
######ok#######

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="######ok#######" insertbefore="hello"'
[root@node1 mnt]# cat westos
hello westos
hello test
######ok#######
hello linux

 

 

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="test\hello\lee"'
[root@node1 mnt]# cat westos
hello westos
hello test
hello linux
test\hello\lee

[devops@ansible .ansible]$ ansible westos -m lineinfile -a 'path=/mnt/westos line="test\nhello\nlee"'  ##\n换行
[root@node1 mnt]# cat westos
hello westos
hello test
hello linux
test
hello
lee

[devops@ansible .ansible]$ vim test.yml 
- name: test
  hosts: westos
  tasks:
    - lineinfile:
        path: /mnt/westos
        line: |+
           123
           234
           237
[devops@ansible .ansible]$ ansible-playbook test.yml 
[root@node1 mnt]# cat westos
hello westos
hello test
hello linux
test\hello\lee
test
hello
lee
123
234
237

18.replace

replace
作用:replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换。

常用参数:

path指定要操作的文件
regexp指定一个正则表达式
文件中与正则匹配的字符串将会被替换
replace指定最终要替换成的字符串
backup是否在修改文件之前对文件进行备份,最好设置为yes
[devops@ansible .ansible]$ ansible westos -m replace -a 'path=/mnt/westos regexp="hello" replace="lee" backup=yes'
[root@node1 mnt]# cat westos      ##将hello替换为lee

19.setup

功能

setup模块用于收集远程主机的一些基本信息

常用参数

filter        ##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息

[devops@ansible .ansible]$ ansible all -m setup 
[devops@ansible .ansible]$ ansible westos -m setup -a 'filter="ansible_fqdn"'

 

20.debug

功能:

调试模块,用于在调试中输出信息

msg调试输出的消息
var将某个任务执行的输出作为变量传递给debug模块
debug会直接将其打印输出
verbositydebug的级别(默认是0级,全部显示)
[devops@ansible .ansible]$ ansible westos -m debug -a 'msg=hello'
172.25.254.205 | SUCCESS => {
    "msg": "hello"
}
[devops@ansible .ansible]$ ansible westos -m debug -a 'var=hello'
172.25.254.205 | SUCCESS => {
    "hello": "VARIABLE IS NOT DEFINED!"
}
[devops@ansible .ansible]$ vim test.yaml
- name: test
  hosts: westos
  tasks:
    - name: debug
      debug:
        var: ansible_facts['fqdn']
[devops@ansible .ansible]$ ansible-playbook test.yaml 
[devops@ansible .ansible]$ vim test.yaml
- name: test
  hosts: westos
  tasks:
    - name: debug
      debug:
        var: ansible_facts['fqdn']
        verbosity: 1
[devops@ansible .ansible]$ ansible-playbook test.yaml 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值