Automated operation and maintenance of the common module Ansible

0, Ansible grammar module

In ansible refers need to quickly execute a command, and the need to save the command, for complex command was playbook

View module helps: ansible-doc -l

// 统计模块数量
$ ansible-doc -l |wc -l
3387  // ansible有大量的模块

Ansible module syntax:

ansible [管理主机信息或者主机组信息] -m [模块名称] -a [相关模块参数] 
主机信息:远程主机IP地址;远程主机组名称;远程所有主机all
-m:指定相应模块
-a:利用模块中某些参数功能
-f :定义每次输出内容的数量

Ansible Note Color Information:
Green: the remote node does not make the appropriate changes to the remote node information or just view
Red: abnormal operation execution command
yellow: remote nodes modified accordingly
dark purple: that the orders to issue a warning message ( possible problems and give you tips recommended)

1, Command Module

the role of command module: the default module 在远程主机执行命令; the default module, can be ignored -m option

// chaidr 先切换到特定的目录,然后在执行命令
[root@m01 ~]# ansible 10.4.7.8 -m command -a "chdir=/tmp/ pwd"

// creates 判断一个文件是否存在。文件如果存在,后面命令则不会执行;如果不存在,则执行
[root@m01 /]# ansible 10.4.7.8 -m command -a "creates=/etc/rsyncd.conf hostname"

// removes 判断一个文件是否存在。文件如果存在,后面命令执行;如果不存在,则不执行
[root@m01 /]# ansible 10.4.7.8 -m command -a "removes=/etc/exports hostname"

// fress_form 使用command模块批量获取执行hostname命令
[root@m01 /]# ansible sa -m command -a "hostname"  

The command module can not and shell, like, like $ HOME variable, and some, like "<", ">", "|", ";" and "&" This operation is no way to perform (if you want to support these special symbols on must use shell module) errors demo:

[root@m01 /]# ansible sa -m command -a "hostname;date"

2, Shell module

shell module role: shell module 万能模块, all functions to meet the command module, and can be 支持识别特殊字符.

// 在10.4.7.8的机器的当前目录下执行ls和pwd命令
[root@m01 /]# ansible 10.4.7.8 -m shell -a "ls;pwd"

3, Scripts module

scripts module effect: 专门运行脚本模块in the local execution of the script, the script generates all actions are performed on the remote host

// 编写脚本,里面添加yum安装keepalived命令,将脚本执行命令放入到受控端执行 
[root@m01 ~]# cat /server/scripts/yum.sh
#!/bin/bash
yum install -y keepalived

[root@m01 ~]# ansible 10.4.7.8 -m script -a "/server/scripts/yum.sh"
[root@m01 ~]# ansible 10.4.7.8 -m shell -a "rpm -qa keepalived"

Note: The script we execute on the management machine, the distal (controlled terminal) does not require the existence of the script

4, Copy module

copy module role: the files from a local or remote machine 复制到远程机器上to a location

// 将本端的/etc/hosts文件推送到远端,并且重命名为test.txt
[root@m01 ~]# ansible sa -m copy -a "src=/etc/hosts dest=/tmp/test.txt"

// 本端在/tmp/下创建文件file01.txt,推送到远端
[root@m01 ~]# touch /tmp/file01.txt
[root@m01 ~]# ansible 10.4.7.7 -m copy -a "src=/tmp/file01.txt dest=/tmp/" 

// 为本端的/tmp/file01.txt文件追加内容,并推送到远端,覆盖时备份原文件
[root@m01 ~]# cat /tmp/file01.txt
[root@m01 ~]# echo 123456 >/tmp/file01.txt
[root@m01 ~]# cat /tmp/file01.txt
123456
[root@m01 ~]# ansible 10.4.7.7 -m copy -a "src=/tmp/file01.txt dest=/tmp/ backup=yes"

// 在/root/下创建test.txt文件,在复制的时候修改属主和属组为root,权限644
[root@m01 ~]# ansible 10.4.7.7 -m copy -a "src=/root/test.txt dest=/test/oldboy/ owner=root group=root mode=644"

// 为对端的/tmp/test.txt文件写入内容“1”
[root@m01 ~]# ansible 10.4.7.7 -m copy -a "content='1' dest=/tmp/test.txt"

parameter

- backup   # 对推送传输过去的文件,进行备份
- src      # 推送数据的源文件信息
- dest     # 推送数据的目标路径
- content  # 直接批量在被管理端文件中添加内容
- owner    # 将本地文件推送到远端,指定文件属主权限
- group    # 将本地文件推送到远端,指定文件属组权限
- mode     # 将本地文件推送到远端,指定文件权限信息

5, File module

file module role: the controlled end文件属性修改/目录创建/文件创建

// 为远端主机修改/tmp/file01.txt文件的属主和属组为root,权限为600
[root@m01 ~]# ansible 10.4.7.7 -m file -a "path=/tmp/file01.txt owner=root group=root mode=600"

// 为远端主机创建文件和目录,远程创建文件/tmp/file01.txt
[root@m01 ~]# ansible 10.4.7.7 -m file -a " path =/tmp/file01.txt state=touch"

// 远程创建目录/tmp/dir01
[root@m01 ~]# ansible 10.4.7.7 -m file -a "path =/tmp/dir01 state=directory"

// 递归修改远端主机的/data目录及目录下的所有文件的属主和属组都为root
[root@m01 /]# ansible 10.4.7.7 -m file -a "path=/data state=directory owner=root group=root recurse=yes"

Module Parameters

- path    # 指定远程主机目录或文件信息
- recurse # 递归授权:
    yes   # 递归修改权限
    no    # 不递归修改权限(默认)
- state   # 指定需要执行的动作:
    directory  # 在远端创建目录
    touch      # 在远端创建文件
    hard       #  硬链接
    link       #软链接

6, Yum module

yum action module: controlled terminal can 执行yum安装, 卸载和查看软件包like

// yum安装iftop软件包
[root@m01 ~]# ansible 10.4.7.7 -m yum -a "name=iftop state=installed"

// 移除iftop软件
[root@m01 ~]# ansible 10.4.7.7 -m yum -a "name=iftop state=absent"

// 查看指定软件包名的列表
[root@m01 ~]# ansible 10.4.7.7 -m yum -a "list=iftop"

Module Parameters

- name  # 执行要安装软件的名称,以及软件的版本
- state # 指定需要执行的动作:
    installed、present   # 安装软件包
    latest               # 安装最新软件包
    removed、absent      # 移除软件包
- list  # 指定软件名称,查看软件是否已经安装了

7, Service Module

service module effect: 管理服务状态模块on the controlled end managed services

// 将远端的crond服务关闭,并且开机不启动
[root@m01 ~]# ansible 10.4.7.7 -m service -a "name=crond state=stopped enabled=no"

// 将远端的crond服务开启,并且开机自启动
[root@m01 ~]# ansible 10.4.7.7 -m service -a "name=crond state=started enabled=yes"

Module Parameters

- name    # 指定需要管理的服务名称(管理的服务一定在chkconfig中可以看到)
- state   # 指定需要执行的动作:
    started     # 启动服务
    reloaded    # 平滑重启
    restarted   # 重启服务
    stopped     # 停止服务
    running     # 运行(启动)服务
- enable  # 设置服务是否开机自启动:
    yes    # 服务开机自启动
    no     # 服务开机不启动

8, Cron module

cron module effect: a controlled end添加定时任务

x x x x x /bin/sh /server/scripts/test.sh >/dev/null 2>&1

The timing of the mission system into the command line syntax ansilbe
traditional wording

"minute=0 hour=0 daay=* month=* weekday=* job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1'"

ansible wording (asterisk may be omitted)

ansible 10.4.7.7 -m cron -a "minute=0 hour=0 job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1'"

Timing Example

// 添加一条定时任务
[root@m01 ~]# ansible 10.4.7.7 -m cron -a "minute=0 hour=0 job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1'"

// 设置定时任务注释信息,防止重复设置
[root@m01 ~]# ansible 10.4.7.7 -m cron -a "name='cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1'"

// 删除指定的name
[root@m01 ~]# ansible 10.4.7.7 -m cron -a "name='cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1' state=absent"

// 也可以直接指定定时任务项的名称,进行定时任务的删除
[root@m01 ~]# ansible 10.4.7.7 -m cron -a "name='cron02' state=absent"

// 使用 disabled 参数来注释定时任务
[root@m01 ~]# ansible 10.4.7.7 -m cron -a "name='cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1' disabled=yes"

// 取消注释
[root@m01 ~]# ansible 10.4.7.7 -m cron -a "name='cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh >/dev/null 2>&1' disabled=no"

Module Parameters

- name # 为新的定时任务条目设置名称(防止定时任务项的重复)
- state  # 需要指定的动作:
    absent   # 添加定时任务
    preset   # 删除定时任务 
- disabled # 是否禁用定时任务(是否注释):
    yes   # 禁用定时任务项(注释掉)
    no    # 开启定时任务项(取消注释)
# 该参数需要和job一起使用
- minute  # 分钟 0-59
- hour    # 小时 0-23
- day     # 天数 1-31
- month   # 月份 1-12
- weekday # 周 0-6

9, Group module

action module group: it may be the distal end创建用户组

// 创建组,组名为demo,组id为1012
[root@m01 ~]# ansible 10.4.7.7  -m group -a "name=demo gid=1012"

Module Parameters

- name # 必须参数,指定创建的组名
- gid  # 指定用户的gid
- state
    absent   # 移除远端主机的组
    present  # 创建远端主机的组(默认)

10, User Modules

user action module: for the distal end can be 创建用户, 修改用户, 删除用户, etc.

// 创建用户名test,uid为999,组为test,shell为/sbin/nologin,并且不创建家目录
[root@m01 ~]# ansible 10.4.7.7 -m user -a "name=test uid=1010 shell=/sbin/nologin createhome=no"

// 创建普通用户alx,并配置对应的用户密码(-1 使用MD5加密)
[root@m01 /]# echo '123456' | openssl passwd -1 -stdin
$1$yGnjv/n3$J.tO.qSIyLy5q547tSisz/
[root@m01 /]# ansible 10.4.7.7 -m user -a 'name=alx password="$1$yGnjv/n3$J.tO.qSIyLy5q547tSisz/"'

Module Parameters

- name     # 必须参数,指定用户名
- group    # 指定用户组名称
- groups   # 指定附加组名称,多个组之间用逗号","分隔
- shell    # 指定用户登录的shell
- uid      # 指定用户的uid
- comment  # 指定用户的注释信息
- password # 给用户添加密码
- state  # 指定用户是否存在于受控主机中:
    preset  # 创建用户(默认)
    absent  # 删除用户
- createhome # 是否创建家目录:
    yes  # 创建家目录(默认)
    no   # 不创建家目录
- home  # 指定家目录,需要和createhome互相配合使用

Password: password must To do a bunch of pre-set value output after openssl encryption process, then the value will also create a new user's password, if you add a plain text password, then it is invalid.

11, Mount module

mount action module: may be 挂载参数写入到/etc/fastb文件in

// 仅将挂载的配置写入到/etc/fastb,并不会执行挂载
[root@m01 ~]# ansible sa -m mount -a "src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=present"

// 临时挂载设备,并将挂载的配置写入到/etc/fastb
[root@m01 ~]# ansible sa -m mount -a "src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=mounted"

// 临时卸载,不会清理/etc/fstab
[root@m01 ~]# ansible sa -m mount -a "src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=unmounted"

// 卸载,不仅临时卸载,同时会清理/etc/fstab
[root@m01 ~]# ansible sa -m mount -a "src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=absent"

Module Parameters

- present   # 开机挂载,不会执行挂载指令,仅将挂载的配置写入到/etc/fastb
- mounted   # 挂载设备,并将挂载的配置写入到/etc/fastb
- unmounted # 卸载设备,不会清除/etc/fastb写入的配置
- absent    #卸载设备,会清理/etc/fastb写入的配置

Note: path specified mount directory does not need to be created manually, when performing the mount command will automatically create and mount

12, Unarchive module

Unarchive module role: Unzip

The module has two uses:

  • The archive on the ansible host after local decompression spread to the remote host, in this case, copy = yes. Local decompression, decompression position other than the default directory, or pass did not find the finish after deleted reached the remote host computer

  • The compressed on a remote host unzip the package to the specified path. In this case, the host needs to be set above the remote copy = no operation, the server does not involve ansible

// 将本地的压缩文件解压后传到远程主机
[root@m01 ~]# ansible 10.4.7.7 -m unarchive -a "src=/opt/src/apache-tomcat-8.5.53.tar.gz dest=/opt copy=yes mode=0755"

// 将远程主机的压缩文件解压
[root@m01 ~]# ansible 10.4.7.7 -m unarchive -a "src=/opt/src/apache-tomcat-8.5.53.tar.gz dest=/opt copy=no mode=0755"

Module Parameters

- src  # 源压缩包路径
- dest # 压缩包解压后存放路径
- copy # yes:本地压缩,no:远程压缩
    yes
    no
- mode # 解压后的目录/文件权限

13, Git module

Git module Role: Management git repository git checkout files or software to deploy.

// 拉取git仓库commit id 为78d5d96的代码到指定目录
[root@m01 ~]# ansible 10.4.7.7 -m git -a "repo=https://gitee.com/jasonminghao/dubbo-demo-service.git dest=/data/git_repo/dubbo-demo-service version=78d5d96 accept_hostkey=yes"

Module Parameters

- repo    # git仓库地址(https/ssh)
- dest    # 将代码克隆到指定路径
- version # 克隆指定版本分支/commit id
- accept_hostkey # 类似于-o StrictHostKeyChecking=no
    yes
    no

14, Systemd module

systemd action module: If systemctl management program, you can use systemd module, the control program can systemctl, reload, start, status, restart, etc.

// 拉取git仓库commit id 为78d5d96的代码到指定目录
[root@m01 ~]# ansible 10.4.7.7 -m systemd -a  "name=nfs state=started enabled=yes daemon_reload=true"

Module Parameters

- name     # 需要管理的服务名称
- state    # 执行动作
    reloaded  # 平滑重启
    restarted # 重启
    started   # 启动
    stopped   # 停止
- enabled # 是否开机启动
- daemon_reload # 在执行任何其他操作之前运行daemon-reload,以确保systemd已经读取了任何更改。
     yes
     no

Guess you like

Origin www.cnblogs.com/jasonminghao/p/12635616.html