Ansible自动化运维模块详解

发布时间 2023-12-05 11:16:01作者: 小代小代

ansible  ad-hoc点对点模块

ping模块主机连通性测试

[root@node2 ~]# ansible all -m ping

192.168.200.10 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": false,

    "ping": "pong"

}

command命令模块 远程主机上执行命令

chdir      # 在执行命令之前,先切换到该目录

creates  # 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断

removes       # 一个文件名,这个文件不存在,则该命令不执行,与creates相反的判断

executable   # 切换shell来执行命令,需要使用命令的绝对路径(不常用,常用下面shell 模块)

free_form    # 要执行的Linux指令,一般使用Ansible-a参数代替(不常用,常用下面shell 模块)

[root@node2 ~]# ansible all -m command -a 'creates=/data/f1 touch /data/f2'

[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If

you need to use command because file is insufficient you can add 'warn: false' to this

command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

192.168.200.10 | CHANGED | rc=0 >>

 

[root@node2 ~]# ansible all -m command -a 'removes=/data/f1 touch /data/f2'

192.168.200.10 | SUCCESS | rc=0 >>

skipped, since /data/f1 does not exist

[root@node2 ~]#

[root@node2 ~]# ansible all -m command -a 'chdir=/tmp ls'

192.168.200.10 | CHANGED | rc=0 >>

123

ansible_command_payload_xJ1PZW

ansibleplaybook_test.txt

ansible_test.txt

qq.txt

systemd-private-4b79af62d3ad4f759f7735cc99135b88-chronyd.service-cm2Obp

test.sh

vmware-root

shell模块

shell模块在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道、echo

在配置文件中将模块进行修改,就不需要输入-m shell选项,修改位置:

$ vim /etc/ansible/ansible.cfg

module_name = shell

[root@node2 ~]# ansible all -a 'cat /etc/passwd |grep test'

192.168.200.10 | CHANGED | rc=0 >>

test:x:1000:1000::/home/test:/bin/bash

test1:x:1001:1001::/home/test1:/bin/bash

test2:x:1002:1002::/home/test2:/bin/bash

script模块在指定节点运行服务端的脚本

[root@node2 ~]# ansible all -m script -a 'test.sh'

[root@node2 ~]# ansible all -a "chdir=/data ls"

[root@node2 ~]# ansible all  -m shell -a "cat /data/test"

192.168.200.10 | CHANGED | rc=0 >>

文件系统                 容量  已用  可用 已用% 挂载点

/dev/mapper/centos-root   17G  1.2G   16G    7% /

devtmpfs                 899M     0  899M    0% /dev

tmpfs                    911M     0  911M    0% /dev/shm

tmpfs                    911M   18M  894M    2% /run

tmpfs                    911M     0  911M    0% /sys/fs/cgroup

/dev/sda1               1014M  142M  873M   14% /boot

tmpfs                    183M     0  183M    0% /run/user/0

/dev/sr0                 4.2G  4.2G     0  100% /opt/centos

copy模块

复制文件到远程主机,可以改权限等

复制文件

-a "src= dest= "

给定内容生成文件

-a "content= dest= "

相关选项如下:

src:源,被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync

dest:目标,必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录

backup:被管理的远程主机已经有文件了,在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no

content:用于替代src”,可以直接设定指定文件的值

directory_mode:递归设定目录的权限,默认为系统默认权限

force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes

others:所有的file模块里的选项都可以在这里使用

---------------------------------

#ansible all -m copy -a "src=/etc/issue dest=/data/fstab owner=nobody mode=600 backup=yes"  src(源)/etc/issue文件复制到dest(目标)的data目录下,起名叫fstab,所有者为nobody,权限为600,如果有此文件,进行备份。

[root@node2 ~]# ansible all -a "ls -l /data"

192.168.200.10 | CHANGED | rc=0 >>

总用量 8

-rw-r--r--. 1 root   root   0 9月   1 10:02 f2

-rw-------. 1 nobody root  23 9月   1 10:21 fstab

-rw-r--r--. 1 root   root 520 9月   1 10:12 test

 

fetch模块从远程复制文件到本地

从远程某主机获取文件到本地

dest:用来存放文件的目录,例如存放目录为backup,源文件名称为/etc/profile在主机pythonserver中,那么保存为/backup/pythonserver/etc/profile

Src:在远程拉取的文件,并且必须是一个file,不能是目录

注意:从远程获取到本地的文件,会保存到以远程主机的IP 为名的目录中,且保存目录结构

------------------------------------

# ansible all -m fetch -a "src=/etc/hosts dest=/data"

# ls /data/

192.168.200.10

# ansible all -m shell -a "tar -cvf /root/data.tar /data"

# ansible all -m shell -a "ls -l /root/"

192.168.200.10 | CHANGED | rc=0 >>

总用量 16

-rw-------. 1 root root  1231 7月   1 20:54 anaconda-ks.cfg

-rw-r--r--. 1 root root 10240 9月   1 10:34 data.tar

file模块设置文件属性

创建目录:-a path= state=directory

创建链接文件:-a path= src= state=link

删除文件:-a path= state=absent

 

创建文件

# ansible all -m file -a "path=/data/f4 state=directory"

删除文件

# ansible all -m file -a "path=/data/f4 state=absent"

创建软链

# ansible all -m file -a "src=/data/fstab path=/data/fstab.link state=link"

创建硬链接 

# ansible all -m file -a "src=/data/fstab path=/data/fstab.link state=hard"

hostname模块  管理主机名

ansible 192.168.200.10 -m hostname -a name=centos102” 修改为centos102

cron模块 计划任务

管理cron计划任务;-a “”: 设置管理节点生成定时任务

crontab

day=     #日应该运行的工作( 1-31, *, */2, )

hour=   # 小时 ( 0-23, *, */2, )

minute=    #分钟( 0-59, *, */2, )

month=     #( 1-12, *, /2, )

weekday   # ( 0-6 for Sunday-Saturday,, )

job=       #指明运行的命令是什么

name=   #定时任务描述

reboot    # 任务在重启时运行,不建议使用,建议使用special_time

special_time   #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)

state   #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务

user    #以哪个用户的身份执行 

# ansible all -m cron -a "minute=*/1 weekday=4 job="/use/bin/date 192.168.200.10 &> /dev/null" name=synctime"

# ansible all -m cron -a "minute=*/1 weekday=4 job="/use/bin/date 192.168.200.10 &> /dev/null" name=synctime disable=ture"

 

 ansible all -m cron-a "name=' clean iptables' minute=*/5 job='/sbin/iptables -F &> /dev/null' "

 

state=absent 删除计划

 

yum模块

  • conf_file    #设定远程yum安装时所依赖的配置文件。如配置文件没有在默认的位置。
  • disable_gpg_check   #是否禁止GPG checking,只用于`presentor `latest’。
  • disablerepo   #临时禁止使用yum库。 只用于安装或更新时。
  • enablerepo   #临时使用的yum库。只用于安装或更新时。
  • name=    #所安装的包的名称
  • state=     #present安装, latest安装最新的, absent 卸载软件。
  • update_cache    #强制更新yum的缓存。

安装应用

# ansible all -m yum -a "name=httpd "

卸载应用

# ansible all -m yum -a "name=httpd state=absent"

安装多个应用

# ansible all -m yum -a "name=httpd,vsftpd,net-tools"

卸载多个应用

# ansible all -m yum -a "name=httpd,vsftpd,net-tools state=absent"

service模块服务程序管理

  • arguments   #命令行提供额外的参数
  • enabled   #设置开机启动。
  • name=     #服务名称
  • runlevel    #开机启动的级别,一般不用指定。
  • sleep    #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。
  • state     #started启动服务, stopped停止服务, restarted重启服务, reloaded重载配置

# ansible all -m service -a "name=httpd state=started"

[root@node2 ~]# ansible all -a "ss -nlt"

192.168.200.10 | CHANGED | rc=0 >>

State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              

LISTEN     0      128          *:22                       *:*                  

LISTEN     0      100    127.0.0.1:25                       *:*                  

LISTEN     0      128         :::80                      :::*                  

LISTEN     0      128         :::22                      :::*                  

LISTEN     0      100        ::1:25                      :::*                  

[root@node2 ~]# # ansible all -m service -a "name=httpd state=stopped"

user模块 用户模块

  • comment        # 用户的描述信息
  • createhome    # 是否创建家目录
  • force      # 在使用state=absent, 行为与userdel -force一致.
  • group     # 指定基本组
  • groups   # 指定附加组,如果指定为(groups=)表示删除所有组
  • home     # 指定用户家目录
  • move_home    # 如果设置为home=, 试图将用户主目录移动到指定的目录
  • name     # 指定用户名
  • non_unique     # 该选项允许改变非唯一的用户ID
  • password       # 指定用户密码,若指定的是明文密码,是不能用的,需用md5加密过后的密码
  • remove   # 在使用state=absent, 行为是与userdel -remove一致
  • shell      # 指定默认shell
  • state      # 设置帐号状态,不指定为创建,指定值为absent表示删除
  • system  # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
  • uid     # 指定用户的uid
  • update_password    # 更新用户密码

创建用户

# ansible all -m user -a 'name=test comment="test user" uid=200 home=/data/testhome group=root groups=bin,nobody shell=/sbin/nologin'

移除用户

# ansible all -m user -a "name=test state=absent"

group模块

  • gid         # 设置组的GID
  • name=  # 管理组的名称
  • state     # 指定组状态,默认为创建,设置值为absent为删除
  • system  # 设置值为yes,表示为创建系统组

 ansible webs -m group -a "name=testgroup system=yes“  创建系统组

 ansible webs -m group -a "name=testgroup state=absent"  删除组

 

setup模块 查机器的facts信息

  • facts 组件是Ansible 用于采集被管机器设备信息的一个功能,我们可以使用setup 模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
  • acts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。
  • setup模块,主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。
  • setup模块下经常使用的一个参数是filter 参数,查询的是全部信息,很多,filter 相当于匹配筛选。

查看机器全部信息

# ansible all -m setup

查看主机总内存

# ansible all -m setup  -a "filter=ansible_memtotal_mb"  其中filter是过滤的含义

 

查看centos版本号

# ansible all -m setup -a "filter=ansible_distribution_major_version"

 

 

unarchive 解压模块

ansible web01 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/'

- name: Unarchive a file that is already on the remote machine

  unarchive:

    src: /tmp/foo.zip            #要解压的包

    dest: /usr/local/bin        #解压到目标位置

    remote_src:

        yes                        #要解压的包在受控端

        no                        #要解压的包在控制端

archive 压缩模块

ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz'

EXAMPLES:

- name: Compress directory /path/to/foo/ into /path/to/foo.tgz

  archive:

    path: /path/to/foo            #要压缩的文件或目录

    dest: /path/to/foo.tgz        #压缩后的文件

    formatbz2, gz, tar, xz, zip    #指定打包的类型