• Ansible运维自动化工具19个常用模块使用实例【转】


    一、模块列表

    1、setup

    2、ping

    3、file

    4、copy

    5、command

    6、shell

    7、script

    8、cron

    9、yum

    10、service

    11、group

    12、user

    13、stat

    14、mount

    15、fetch

    16、synchronize

    17、get_url

    18、hostname

    19、wait_for

     

    二、模块示例

    1、setup 功能:搜集系统信息

    #通过命令获取所有的系统信息,搜集主机的所有系统信息

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m setup

    注意:/etc/ansible/.hosts 文件的写法很关键,官网也出了相关的说明,只不过官方的文档分版本描述的,稍不注意就掉坑了,具体格式如下:

    [xxxx-root]

    192.168.1.51 ansible_become=True ansible_become_method=su ansible_become_user=root ansible_become_pass=1111111111

    192.168.1.52 ansible_become=True ansible_become_method=su ansible_become_user=root ansible_become_pass=1111111111

    #搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m setup --tree /tmp/facts

    #搜集和内存相关的信息

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m setup -a 'filter=ansible_*_mb'

    #搜集网卡信息

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m setup -a 'filter=ansible_eth[0-2]'


    2、ping 功能:测试网络连通性, ping模块没有参数

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m ping


    3、file 功能:文件属性设置

    force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no

    group:定义文件/目录的属组

    mode:定义文件/目录的权限

    owner:定义文件/目录的属主

    path:必选项,定义文件/目录的路径

    recurse:递归设置文件的属性,只对目录有效

    src:被链接的源文件路径,只应用于state=link的情况

    dest:被链接到的路径,只应用于state=link的情况

    state包括以下:

    directory:如果目录不存在,就创建目录

    file:即使文件不存在,也不会被创建

    link:创建软链接

    hard:创建硬链接

    touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间

    absent:删除目录、文件或者取消链接文件

    创建软链接:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"

    删除软连接:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m file -a "path=/tmp/resolv.conf state=absent"

    创建目录(文件):

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m file -a "path=/root/testfile group=ihavecar owner=ihavecar mode=700 state=directory"


    4、copy 功能:复制文件到远程主机

    backup: #在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no

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

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

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

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

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

    group # 复制到远程主机后,指定文件或目录的属

    mode # 复制到远程主机后,指定文件或目录权限,类似与 `chmod’指明如 0644

    owner # 复制到远程主机后,指定文件或目录属主

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

    将本地文件“/etc/ansible/ansible.cfg”复制到远程服务器,设置属主和属组及文件权限

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=ihavecar group=root mode=400"


    5、command 功能:在远程主机上执行命令

    Command不适用于有shell变量的情况,也不适用于有管道符或者&&的情况,如果要使用此种情况,那么可以使用shell模块

    相关选项如下:

    creates:一个文件名,当该文件存在,则该命令不执行

    free_form:要执行的linux指令

    chdir:在执行指令之前,先切换到该目录

    removes:一个文件名,当该文件不存在,则该选项不执行

    executable:切换shell来执行指令,该执行路径必须是一个绝对路径

    远程执行查询系统负载:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m command -a "uptime"

    如带有管道的,则会如下报错,command是不允许有管道的:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m command -a "uptime | grep 192.168.1.50"


    6、shell 功能:切换到某个shell执行指定的指令

    切换到某个shell执行指定的指令,与command不同的是,此模块可以支持命令管道,同时还有另一个模块也具备此功能:raw

    chdir # 执行之前,先cd到指定目录在执行命令

    creates # 一个文件名,当这个文件存在,则该命令不执行

    executable # 切换shell来执行命令,需要使用命令的绝对路径

    free_form= # 执行的命令

    removes # 一个文件名,这个文件不存在,则该命令不执行

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m shell -a "cat /etc/passwd | grep root"

    可远程执行脚本:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m shell -a "sh /tmp/echo.sh"


    7、script 功能:指定本地的脚本文件,到远程主机运行一次

    注意这个模块和shell模块的不同,shell模块是要求客户端上有这个脚本才能执行;script是要求ansible服务端有这个脚本就可以了,执行的时候是不会拷贝这个脚本到客户端的。

    creates # 一个文件名,当这个文件存在,则该命令不执行

    free_form= # 本地脚本路径

    removes # 一个文件名,这个文件不存在,则该命令不执行

    ansible端的已有脚本,就只有一条/sbin/ifconfig 命令,该脚本被拿到客户端上执行,并返回结果:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m script -a "/etc/ansible/masterecho.sh"


    8、cron 功能:计划任务管理

    backup # 如果设置,创建一个crontab备份

    cron_file # 如果指定, 使用这个文件cron.d,而不是单个用户crontab

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

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

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

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

    month # 月( 1-12, *, */2, etc )

    name # 定时任务描述

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

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

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

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

    weekday # 周 ( 0-6 for Sunday-Saturday, *, etc )

    定义一个时间任务,每隔3分钟执行一次时间记录任务:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m cron -a 'name="ntp datatime job" minute=*/3 hour=* day=* month=* weekday=* job="echo `date` >> /tmp/linshidata.txt"'

    效果如下:

    删除一个时间任务:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m cron -a 'name="ntp datatime job" minute=*/3 hour=* day=* month=* weekday=* job="echo `date` >> /tmp/linshidata.txt" state=absent'

     


    9、yum 功能:软件包安装管理

    conf_file # yum的配置文件

    disable_gpg_check # 关闭gpg_check

    disablerepo # 不启用某个源

    enablerepo # 启用某个源

    List # 非幂等性命令

    name= # 指定要安装的包,如果有多个版本需要指定版本,否则安装最新的包

    state # 安装(`present’),安装最新版(`latest’),卸载程序包(`absent’)

    指定版本安装包:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m yum -a "name=httpd-devel-2.2.15 state=present"

    指定安装最新版本的包:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m yum -a "name=httpd-devel-2.2.15 state=latest"

    指定rpm包来安装:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m yum -a "name=/usr/local/src/kel.noarch.rpm state=present"

    指定远程网址rpm包来进行安装:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6.0.el6.ngx.noarch.rpm state=present"

    删除包:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m yum -a "name=httpd-devel-2.2.15 state=absent"


    10、service 功能:系统服务管理

    arguments # 向服务传递的命令行参数

    enabled # 设置服务开机自动启动,参数为yes|no

    name= # 控制服务的名称

    pattern # 如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行

    runlevel # 设置服务自启动级别

    sleep # 如果执行了restarted,则在stop和start之间沉睡几秒钟

    state # 启动`started’ 关闭`stopped’ 重新启动 `restarted’ 重载 `reloaded’

    设置httpd服务为开机自启动模式,并限制开启httpd服务

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m service -a "name=httpd state=started enabled=yes"

    重启httpd服务,中间sleep 10秒钟

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m service -a "name=httpd state=restarted sleep=10"


    11、group 功能:系统用户组管理

    gid # 设置组的GID号

    name= # 管理组的名称

    state # 指定组状态,默认为创建,设置值为absent为删除

    system # 设置值为yes,表示为创建系统组

    创建一个foo组,指定gid号

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m group -a "name=foo gid=360 system=no"

    删除一个组:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m group -a "name=foo gid=360 system=no state=absent"


    12、user 功能:系统用户账号管理

    comment # 用户的描述信息

    createhome # 是否创建家目录

    force # 在使用`state=absent’时, 行为与`userdel –force’一致.

    group # 指定基本组

    groups # 指定附加组,如果指定为(‘groups=’)表示删除所有组

    home # 指定用户家目录

    login_class # 可以设置用户的登录类 FreeBSD, OpenBSD and NetBSD系统.

    move_home # 如果设置为`home=’时, 试图将用户主目录移动到指定的目录

    name= # 指定用户名

    non_unique # 该选项允许改变非唯一的用户ID值

    password # 指定用户密码

    remove # 在使用 `state=absent’时, 行为是与 `userdel –remove’一致.

    shell # 指定默认shell

    state #设置帐号状态,不指定为创建,指定值为absent表示删除

    system # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户。

    uid #指定用户的uid

    update_password # 更新用户密码

    添加用户foo,指定密码,设置家目录,不允许远程登录

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m user -a "name=foo password=123456 home=/home/foo shell=/sbin/nologin"

    彻底删除一个用户,包括家目录:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m user -a "name=foo remove=yes state=absent"

        


    13、stat 功能:获取远程文件信息

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m stat -a "path=/etc/passwd"


    14、mount 功能:配置挂载点

    fstype:必选项,挂载文件的类型

    name:必选项,挂载点

    opts:传递给mount命令的参数

    src:必选项,要挂载的文件

    state:必选项

    present:只处理fstab中的配置

    absent:删除挂载点

    mounted:自动创建挂载点并挂载之

    umounted:卸载

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m mount -a "name=/mnt src=/dev/sda5 fstype=ext4 opts=ro state=present"


    15、fetch 功能:文件拉取模块,主要是将远程主机中的文件拷贝到本机中

    和copy模块的作用刚刚相反,并且在保存的时候使用hostname来进行保存,当文件不存在的时候,会出现错误,除非设置了选项fail_on_missing为yes

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

    Fail_on_missing: Yes/no,当源文件不存在的时候,标识为失败

    Flat: 允许覆盖默认行为从hostname/path到/file的,如果dest以/结尾,它将使用源文件的基础名称

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

    Validate_checksum Yes/no,当文件fetch之后进行md5检查

    从远程机器上传送文件到ansible服务器

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m fetch -a "src=/root/123 dest=/root"

    src表示为远程主机上需要传送的文件的路径,dest表示为本机上的路径,在传送过来的文件,是按照IP地址或hostname进行分类,然后路径是源文件的路径,例如上面的最终路径为/root/192.168.1.50/root/123在拉取文件的时候,必须拉取的是文件,不能拉取文件夹

    指定路径目录进行保存:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m fetch -a "src=/root/Ssh.py dest=/root/kel/ flat=yes"

    在使用参数为flat的时候,如果dest的后缀名为/,那么就会保存在目录中,然后直接保存为文件名,如上例的结果为 dest”: “/root/kel/Ssh.py;

    当dest后缀不为/的时候,那么就会直接保存为kel的文件,如上例1所示。主要是在于dest是否已/结尾,从而来区分这是个目录还是路径。


    16、synchronize 功能:将主控方/root/a目录推送到指定节点的/tmp目录下

    选项说明

    archive # 是否采用归档模式同步,即以源文件相同属性同步到目标地址

    checksum # 是否效验

    compress # 开启压缩,默认为开启

    copy_links # 同步的时候是否复制连接

    delete # 删除源中没有而目标存在的文件(即以推送方为主)

    dest= # 目标地址

    dest_port # 目标接受的端口,ansible配置文件中的 ansible_ssh_port 变量优先级高于该 dest_port 变量

    dirs # 以非递归的方式传输目录

    existing_only # Skip creating new files on receiver.

    group # Preserve group

    links # Copy symlinks as symlinks.

    mode # 模式,rsync 同步的方式 PUSHPULL,默认都是推送push。如果你在使用拉取pull功能的时候,可以参考如下来实现mode=pull 更改推送模式为拉取模式

    recursive # 是否递归 yes/no

    rsync_opts # 使用rsync 的参数

    rsync_path # 服务的路径,指定 rsync 命令来在远程服务器上运行。这个参考rsync命令的--rsync-path参数,--rsync-path=PATH # 指定远程服务器上的rsync命令所在路径信息

    rsync_timeout # 指定 rsync 操作的 IP 超时时间,和rsync命令的 --timeout 参数效果一样.

    set_remote_user # put user@ for the remote paths. If you have a custom ssh config to define the remote user for

    src=‘#‘" # 源,同步的数据源

    times #

    --exclude=.Git 忽略同步.git结尾的文件

    由于模块默认启用了archive参数,该参数默认开启了recursive, links, perms, times, owner,group和-D参数。如果你将该参数设置为no,那么你将停止很多参数,比如会导致如下目的递归失败,导致无法拉取

    压缩传输a目录到被控节点:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'

    注意:主控端的a目录传输到被控节点,文件属性过去被控节点后,只能是remote user的权限,也就是ihavecar的,不能是root的,也不允许传输到ihavecar这个用户无权限的目录下去,不然会报错。

    使用pull模式拉取文件到主控节点:

    由于模块,默认都是推送push。因此,如果你在使用拉取pull功能的时候,可以参考如下来实现

    mode=pull 更改推送模式为拉取模式。

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m synchronize -a 'mode=pull src=/tmp/a dest=/root/ compress=yes'

    注意:从被控节点传目录回到主控节点,可以遵循权限,被控节点端目录的属性是什么,传回主控节点的目录权限就是什么,并不会像push方式那样属性改变成普通用户的。还有一点需要注意的是,从被控节点传输回主控节点,只有一个节点能传输成功,其他的节点都会失败。当然,如果各个被控节点的文件如果不是一样的话,那么各个节点都会传文件回来。那样主控端就会收到各个节点的全部文件。


    17、get_url 功能:将某个url的文件下载到被控节点的某个位置

    将simplejson-3.8.2.tar.gz 下载到/tmp/下:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m get_url -a "url=https://pypi.python.org/packages/source/s/simplejson/simplejson-3.8.2.tar.gz dest=/tmp/"


    18、hostname 功能:主要用来修改主机的名称

    在查看的时候,主要查看文件/etc/sysconfig/network,重启之后才能生效

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m hostname -a "name=ansible-test"


    19、wait_for 功能:等待一个事件发生或处理完后,再继续运行下面的事情

    connect_timeout 默认5秒,在下一个事情发生前等待链接的时间,单位是秒

    delay 延时,大家都懂,在做下一个事情前延时多少秒

    host 默认127.0.0.1,执行这个模块的host

    path 当一个文件存在于文件系统中,下一步才继续。

    port 端口号,如8080

    state 默认started,对象是端口的时候start状态会确保端口是打开的,stoped状态会确认端口是关闭的

    present 对象是文件的时候present或者started会确认文件是存在的,而absent会确认文件是不存在的。

    started

    stopped

    absent

    10秒后在当前主机开始检查8000端口,直到端口启动后返回:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m wait_for -a "port=22612 delay=10"

    检查path=/tmp/foo直到文件存在后继续:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m wait_for -a "path=/tmp/foo"

    确认/var/lock/file.lock不存在继续:

    # ansible -i /etc/ansible/.hosts-root jr-root -c paramiko -m wait_for -a "path=/var/look/file.lock state=absent"

    本文转自

    精心汇总,史上最全-Ansible运维自动化工具19个常用模块使用实例(root用户角度)-Ansible-运维人生 http://www.ywadmin.com/?id=91

    https://www.cnblogs.com/paul8339/p/10608024.html#_label18 

  • 相关阅读:
    Java学习笔记(一)语法
    【转,整理】C# 非托管代码
    HTML5学习笔记(七)WebSocket
    HTML5学习笔记(七)HTML5 服务器发送事件(Server-Sent Events)
    MySQL修改表格内容3
    MySQL修改表格内容2
    MySQL修改表格内容
    MySQL创建表格
    if-else if-else;多选择结构
    面向对象和面向过程的初步概念
  • 原文地址:https://www.cnblogs.com/paul8339/p/10608024.html
Copyright © 2020-2023  润新知