Ansible-常用模块

前言

ansible 有着诸多的模块,虽然模块众多,但最为常用的模块也就 20-30 个左右

1、command-模块

1.1、作用

功能:在远程主机执行 Shell 命令;此为默认模块,可忽略 -m 选项;
注意:不支持管道命令 |

1.2、参数解析

参数      选项                  含义
chdir     chdir /opt           执行ansible时,切换到指定的目录
creates   creates /data/file   如果文件存在,则跳过执行
removes   removes /data/file   如果文件存在,则执行

1.3、示例

1.3.1、chdir 切换目录执行 Shell 命令

~]# ansible webservers -m command -a 'chdir=/root echo $PWD'
192.168.10.8 | CHANGED | rc=0 >>
/root
192.168.10.4 | CHANGED | rc=0 >>
/root

1.3.2、creates 如果 /data/file 文件存在,则跳过执行

ansible webservers -m command -a 'creates=/data/file ifconfig ens33'

1.3.3、removes 如果 /data/file 文件存在,则执行

ansible webservers -m command -a 'removes=/data/file ifconfig ens33'

2、shell-模块

2.1、作用

功能:在远程主机执行 Shell 命令,执行管道等特殊符号的操作;

2.2、参数解析

参数      选项                 含义
chdir     chdir /opt          执行ansible时,切换到指定的目录
creates   creates /data/file  如果文件存在,则跳过执行
removes   removes /data/file  如果文件存在,则执行

2.3、示例

2.3.1、使用 Shell 命令过滤被控端主机的 IP 地址

~]# ansible webservers -m shell -a "ifconfig ens33|awk 'NR==2'"
192.168.10.8 | CHANGED | rc=0 >> inet 192.168.10.8 netmask 255.255.255.0 broadcast 192.168.10.255 192.168.10.4 | CHANGED | rc=0 >> inet 192.168.10.4 netmask 255.255.255.0 broadcast 192.168.10.255

3、script-模块

3.1、作用

功能:在被控节点,运行 Ansible 主机的脚本

3.2、示例

3.2.1、在 Ansible 主机上编写脚本,然后推送至被控端运行

# 1、创建测试脚本
cat << 'CAT_END' > /tmp/echo.sh
#!/bin/bash
name=`hostname`
echo "Hello ${name}"
CAT_END

# 2、在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
~]# ansible webservers -m script -a "/tmp/echo.sh"

4、yum-模块

4.1、作用

功能:管理各个操作系统的软件包;

4.2、参数解析

参数              选项                                 含义
name              httpd、nginx...                      指定安装软件包名或软件包URL
state             present(Defaults)、absent、latest    指定yum对应的方法
enablerepo        epel、base、...                      允许从哪些仓库获取软件
disablerepo       epel、base、...                      禁止从哪些仓库获取软件
exclude           kernel、...                          排除某些软件包
download_only     yes、no                              仅下载软件包,不安装

4.3、示例

4.3.1、安装当前最新的 Apache 软件,如果存在则不安装

ansible webservers -m yum -a "name=httpd state=present"

4.3.2、安装当前最新的Apache软件,通过epel仓库安装

ansible webservers -m yum -a "name=httpd state=present enablerepo=epel"

4.3.3、通过公网 URL 安装 rpm 软件

ansible webservers -m yum -a "name=https://xx.rpm state=present"

4.3.4、安装最新版本的 Apache 软件,如果存在则更新 Apache

ansible webservers -m yum -a "name=httpd state=latest"

4.3.5、更新所有的软件包,但排除和 kernel 相关的

ansible webservers -m yum -a "name=* state=latest exclude=kernel"

4.3.6、删除 Apache 软件

ansible webservers -m yum -a "name=httpd state=absent"

5、copy-模块

5.1、作用

功能:从 ansible 服务端主控端复制文件到远程主机;

5.2、参数解析

参数           选项                         含义
src                                        复制本地目录下的文件至远程服务器
dest                                       文件复制到远程的绝对路径
owner          root(Defaults)              文件复制到远程并设定属主
group          root(Defaults)              文件复制到远程并设定属组
mode           file=644,directory=755     文件复制到远程并设定权限
backup yes                                 备份被修改前的配置文件
content                                    新建文件并给文件添加内容

5.3、示例

5.3.1、将本地的 httpd.conf 文件 Listen 端口修改为 8080 ,推送至远程服务器

ansible webservers -m copy -a "src=./httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644"

5.3.2、将本地的 httpd.conf 文件 Listen 端口修改为 9090 ,推送至远程服务器,然后检查远端是否存在上一次的备份文件

ansible webservers -m copy -a "src=./httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644 backup=yes"

5.3.3、往远程的主机文件中写入内容,如果文件不存在则创建

ansible webservers -m copy -a "content="Http_Server" dest=/var/www/html/index.html"

6、file-模块

6.1、作用

功能:为被控端创建文件或目录,设定权限属性;

6.2、参数解析

参数        选项                               含义
path                                          指定远程服务器的路径
recurse                                       递归方式(可以是递归授权)
state       touch、directory、link、absent     文件复制到远程的状态
owner       root(Defaults)                    文件复制到远程并设定属组
group       root(Defaults)                    备份被修改前的配置文件
mode        file=644,directory=755           文件复制到远程并设定权限

6.3、示例

6.3.1、创建文件,并设定属主、属组、权限

ansible webservers -m file -a "path=/tmp/foo.conf state=touch mode=666"

6.3.2、创建目录,并设定属主、属组、权限

ansible webservers -m file -a "path=/tmp/foo state=directory mode=777"

6.3.3、递归授权目录

ansible webservers -m file -a "path=/tmp/foo state=directory owner=root group=root mode=777 recurse=yes"

7、lineinfile-模块

7.1、作用

功能:修改或删除文件内容,与系统中的 sed 命令类似;

7.2、参数解析

参数             选项                          含义
path                                           指定要操作的文件
regexp                                         使用正则表达式匹配对应的行
line                                           修改为新的内容
insertafter                                    将文本插入到“指定的行”之后
insertbefore                                   将文本插入到“指定的行”之前
state            absent、present(Defaults)   删除对应的文本时,需要state=absent
backrefs         yes、no                       1、支持后向引用。2、当未匹配到内容则不操作文件
backup                                         是否在修改文件之前对文件进行备份
create                                         当要操作的文件并不存在时,是否创建对应的文件

7.3、示例

7.3.1、将 SELINUX 修改为 disabled 状态

ansible localhost -m lineinfile -a 'path=/etc/selinux/config regexp='^SELINUX=' line=SELINUX=disabled'

7.3.2、删除 /etc/sudoers 文件中 %wheel 开头的行

ansible localhost -m lineinfile -a 'path=/etc/sudoers regexp='^%wheel' state=absent'

7.3.3、替换 /etc/hosts 文件中以 127.0.0.1 的行为 127.0.0.1 ansible.example.com

ansible localhost -m lineinfile -a 'path=/etc/hosts regex='^127\.0\.0\.1' line="127.0.0.1 ansible.example.com"'

7.3.4、修改默认 Apache 的端口为 8080 ,使用 insertafter 

ansible localhost -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line="Listen 8080" insertafter='^#Listen'"

7.3.5、网口信息修改

ansible all -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 regexp="^GATEWAY" line="GATEWAY=172.16.1.200"'
ansible all -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 regexp="^DNS" state=absent'
ansible all -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 line="DNS1=223.5.5.5"'

8、get_url-模块

8.1、作用

功能:通过互联网下载软件至被控端本地

8.2、参数解析

参数          选项               含义
url           HTTP, HTTPS...    资源文件在互联网上的具体位置
dest                            文件下载位置的绝对路径
mode                            文件下载后的权限
checksum      md5、sha256       对下载的资源进行校验
timeout       10(Default)       URL请求超时时间

8.3、示例

8.3.1、下载互联网的软件至本地

ansible webservers -m get_url -a "url=https://mirrors.aliyun.com/xx.rpm dest=/tmp"

8.3.2、下载互联网文件并进行 md5 校验

ansible webservers -m get_url -a "url=http,https dest=/opt checksum=md5:76eb3af80ffd"

9、systemd-模块

9.1、作用

功能:管理服务启动与停止,与 service 模块用法一致;

9.2、参数解析

参数        选项                                     含义
name        httpd、nginx、...                        定义要启动服务的名称
state       started、stopped、restarted、reloaded    指定服务状态
enabled     yes、no                                 允许服务开机自启或禁止服务开机自启

9.3、示例

9.3.1、启动 Httpd 服务

ansible webservers -m service -a "name=httpd state=started"

9.3.2、重载 Httpd 服务

ansible webservers -m service -a "name=httpd state=reloaded"

9.3.3、重启 Httpd 服务

ansible webservers -m service -a "name=httpd state=restarted"

9.3.4、停止 Httpd 服务

ansible webservers -m service -a "name=httpd state=stopped"

9.3.5、启动 Httpd 服务,并加入开机自启

ansible webservers -m service -a "name=httpd state=started enabled=yes"

10、group-模块

10.1、作用

功能:管理被控端用户组

10.2、参数解析

参数     选项                        含义
name                                指定创建的组名
gid                                 为组设置可选gid
state    present(Default)、absent   是否将组创建在远程主机上
system   yes、no(Default)           是否创建系统组

10.3、示例

10.3.1、创建 news 基本组,指定uid为9999

ansible webservers -m group -a "name=news gid=9999 state=present"

10.3.2、创建 http 系统组,指定uid为8888

ansible webservers -m group -a "name=http gid=8888 system=true state=present"

10.3.3、删除 news 基本组

ansible webservers -m group -a "name=news state=absent"

11、user-模块

11.1、作用

功能:管理被控端用户

11.2、参数解析

参数              选项                       含义
name                                         创建或删除的用户名
uid                                          为用户设置可选uid
group                                        为用户设置主要的组
groups                                       为用户设置附加的组
shell             present(Default)、absent   为用户设置登陆时的Shell
create_home       yes(Default)、no           为用户创建主目录
state             present(Default)、absent   用户是否应该存在                       
remove            yes、no(Default)           删除与用户关联的目录,只有当state=absent时生效
generate_ssh_key  yes、no(Default)           为相关用户生成ssh密钥。不会覆盖现有的ssh密钥
ssh_key_bits      2048                       创建用户ssh密钥中位数
ssh_key_file      .ssh/id_rsa(Default)       可以实现ssh密钥改名,或变更存放ssh密钥位置

11.3、示例

11.3.1、创建 joh 用户, uid 是 1040 ,主要的组是 adm

ansible webservers -m user -a "name=joh uid=1040 group=adm"

11.3.2、创建 joh 用户,登录 shell 是 /sbin/nologin ,追加 bin、sys 两个组

ansible webservers -m user -a "name=joh shell=/sbin/nologin groups=bin,sys"

11.3.3、创建 jsm 用户,为其添加 123 作为登录密码,并且创建家目录

# 创建密码
ansible localhost -m debug -a "msg={{ '123' | password_hash('sha512', 'sal1t') }}"
$6$salt$jkH

# 创建用户并且设置密码
ansible webservers -m user -a 'name=jsm password="$6$salt$jkH" create_home=yes'

11.3.4、移除 joh 用户

ansible webservers -m user -a "name=joh remove=yes state=absent"

11.3.5、创建 http 用户,并为该用户创建 2048 字节的私钥,存放在~/http/.ssh/id_rsa

ansible webservers -m user -a "name=http uid=8888 group=8888 generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"

12、cron-模块

12.1、作用

功能:管理被控端计划任务

12.2、参数解析

参数         选项               含义
name                            定时任务基本描述
job                             定时任务要执行的命令
minute       (Default)、0-59    分
hour         (Default)、0-23    时
day          (Default)、1-31    日
month        (Default)、1-12    月
weekday      (Default)、0-6

12.3、示例

12.3.1、添加定时任务,每分钟执行一次ls, * * * * * ls >/dev/null

ansible webservers -m cron -a "name='cron01' job='ls >/dev/null'"

12.3.2、添加定时任务, 每天的凌晨2点和凌晨5点执行一次ls, 0 5,2 * * * ls>/dev/null

ansible webserver -m cron -a "name='cron02' minute=0 hour=2,5 job='ls >/dev/null'"

12.3.3、关闭定时任务,使定时任务失效

ansible webservers -m cron -a "name='cron02' minute=0 hour=2,5 job='ls >/dev/null' disabled=yes"

13、mount-模块

13.1、作用

功能:管理被控端设备挂载

13.2、参数解析

参数      选项                            含义
src                                      本地或远程设备的路径
path                                     设备挂载至本地的路径
fstype    xfs、nfs...                    文件系统类型
opts      defaults、ro...                挂载的参数
state     absent、mounted、unmounted     挂载的状态

13.3、环境准备

# 安装nfs
ansible localhost -m yum -a 'name=nfs-utils state=present'

# 创建共享目录
ansible localhost -m file -a 'path=/ops state=directory owner=nfsnobody group=nfsnobody'

# 编辑exports
ansible localhost -m copy -a 'content="/ops 192.168.10.0/24(rw,sync)" dest=/etc/exports'

# 开启nfs服务
ansible localhost -m systemd -a "name=nfs state=restarted"

13.4、示例

13.4.1、挂载 nfs 至本地的 /opt 目录,并实现开机自动挂载

ansible webservers -m mount -a "src=192.168.10.14:/ops path=/opt fstype=nfs opts=defaults state=mounted"

13.4.2、临时卸载 nfs 的挂载,但不清理 /etc/fstab

ansible webservers -m mount -a "src=192.168.10.14:/ops path=/opt fstype=nfs opts=defaults state=unmounted"

13.4.3、永久卸载 nfs 挂载,同时清理 /etc/fstab

ansible webservers -m mount -a "src=192.168.10.14:/ops path=/opt fstype=nfs opts=defaults state=absent"

14、hostname-模块

14.1、作用

功能:管理被控端主机名称;

14.2、示例-设置主机名称为 ansible-hostname

ansible localhost -m hostname -a 'name=ansiblehostname'

15、archive-模块

15.1、作用

功能:打包与压缩;

15.2、参数解析

参数      选项                       含义
path                                 要压缩的文件或目录
dest                                 压缩后的文件
format    bz2、gz、tar、xz、zip      指定打包压缩的类型

15.3、示例

15.3.1、将 /var/log 目录压缩为 tar 格式,并存储至 /opt 目录下

ansible localhost -m archive -a 'path=/var/log dest=/opt/log.tar.gz format=gz'

16、unarchive-模块

16.1、作用

功能:解包与解压缩;

16.2、参数解析

参数            选项                 含义
src                                 要解压的软件包路径
dest                                解压到目标位置
remote_src      yes、no(default)    yes:要解压的包在被控端、no:要解压的包在控制端

16.3、示例

16.3.1、解压被控端

ansible localhost -m cp -a 'src=php.zip dest=/root/'
ansible localhost -m unarchive -a 'src=/root/php.zip dest=/tmp/ remote_src=yes'

16.3.2、解压控制端

ansible localhost -m unarchive -a 'src=/root/php.zip dest=/tmp/'

17、selinux-模块

17.1、作用

功能:管理 SELINUX 防火墙;

17.2、参数解析

参数        选项                                  含义
state       enforcing、permissive、disabled       Selinux模式

17.3、示例

17.3.1、设置 selinux 为 enforcing

ansible webservers -m selinux -a "state=enforcing"

17.3.2、设置 selinux 为 disabled

ansible webservers -m selinux -a "state=disabled"

18、yum_repository-模块

18.1、作用

功能:配置yum仓库

18.2、参数解析

name: nginx   # *.repo文件名字和项名[nginx]
description:  # 说明
baseurl: http # 仓库地址
gpgcheck: no  # 是否检查gpg
enabled: no   # 是否启用

18.3、示例

18.3.1、配置nginx仓库

cat << 'CAT_END' > when_repo.yaml
- hosts: all
  tasks:
  - name: Add nginx yum repo
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://nginx.org/packages/centos/7/$basearch/
      gpgcheck: no
      enabled: no
    when: ( ansible_hostname is match("http*") )
CAT_END

 

posted @ 2023-05-11 23:15  小粉优化大师  阅读(38)  评论(0编辑  收藏  举报