zoukankan      html  css  js  c++  java
  • 自动化运维工具--Ansible常用模块

    注意:-a 参数后的命令用单引号;双引号有可能会出问题,特别是在user模块

    模块一:测试目标主机是否在线,ping模块

    主机如果在线,则回复Pong

    [root@ansible ~]# ansible web -m ping
    192.168.30.36 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.30.32 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }

    模块二:command模块和shell模块

    用于在各被管理主机节点运行指定的命令

    shell与command的区别:command模块是执行ansible自带模块执行,如果要用> < | & ‘ ‘等管道,需要使用shell模块

    [root@ansible tmp]# ansible web -m command -a 'uptime'
    192.168.30.32 | SUCCESS | rc=0 >>
     21:10:43 up 138 days,  1:34,  1 user,  load average: 0.00, 0.01, 0.05
    192.168.30.36 | SUCCESS | rc=0 >>
     21:08:30 up 95 days,  3:02,  4 users,  load average: 0.00, 0.00, 0.00

    shell模块:

    [root@ansible tmp]# ansible web -m shell -a 'cat /etc/fstab | grep ^UUID'
    192.168.30.36 | SUCCESS | rc=0 >>
    UUID=28b3513d-9e6f-42a8-ade0-7700573fed2a /boot                   ext4    defaults        1 2
    192.168.30.32 | SUCCESS | rc=0 >>
    UUID=de2e369a-c0e8-4188-89e0-db1f4aded764 /boot                   xfs     defaults        0 0

    相关选项如下:

    creates:一个文件名,当文件名存在,则该命令不执行
    chdir:在执行指令之前,先切换到该目录
    free_from:要执行的Linux指令
    removes:一个文件名,当文件名不存在,则该命令不执行
    executable:切换shell来执行指令,该执行路径必须是一个绝对路径

    参考:<https://docs.ansible.com/ansible/latest/modules/shell_module.html>

    模块三:远程复制备份--copy模块

    [root@ansible tmp]# ansible web -m copy -a 'src=/etc/hosts dest=/tmp/hosts'

    相关选项:

    backup:在覆盖前,将源文件备份,备份文件包含时间信息,有俩选项:yes|no
    content:用于替代”src”,可以直接设定指定文件的值
    dest:必选项,要将源文件复制到远程主机的绝对路径,如果源文件是一个目录,那么该路径必须是个目录
    directory_mode:递归设定目录的权限,默认为系统默认权限
    force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
    others:所有的file模块里的选项都可以在这里使用
    src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,他将递归复制。这种情况路径使用”/”来结尾,则只复制目录里的内容,如果没有使用“/”,则包含目录在内的全部内容复制,类似与rsync。
    owner:指定属主
    group:指定属组
    mode:指定权限,可以以数字指定比如0644

    参考:<https://docs.ansible.com/ansible/latest/modules/copy_module.html>

    模块四:对远程文件管理--file模块

    修改文件权限:
    [root@ansible ~]# ansible web -m file -a 'path=/srv/a.txt mode=600 owner=snow group=snow'
    创建一个文件:
    [root@ansible ~]# ansible web -m file -a 'dest=/srv/a.txt state=touch'
    创建一个目录文件:
    [root@ansible ~]# ansible web -m file -a 'path=/srv/haha mode=600 state=directory'
    创建一个软链接:
    [root@ansible ~]# ansible web -m file -a 'dest=/srv/a.lnk src=/srv/a.txt state=link'
    删除文件:
    [root@ansible ~]# ansible web -m file -a 'path=/srv/a.lnk state=absent'

    相关选项:

    force:需要在两种情况下强制创建软链接,一种源文件不存在,但之后会建立的情况;另一种是目标软链接存在,需要先取消之前的链接,再创建新链接,有两个选项:yes|no
    path:必选项,定义文件/目录的路径(alias:dest,name)
    recurse:递归设置文件的属性,只对目录有效
    src:被链接的源文件路径,只能用于state=link情况
    dest:目标文件路径,只能用于state-link情况
    state:
            directory:如果目录文件不存在,就创建目录文件
            *file:即使文件不存在也会被创建
            link:创建软链接
            hard:创建硬链接
            touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
            absent:删除目录、文件或者取消链接文件

    参考:<https://docs.ansible.com/ansible/latest/modules/file_module.html>

    fetch模块: 从远程服务器拉取文件至本机,只能fetch文件,不能fetch目录,如果拉目录,先tar/zip 再拉到本机即可。

    使用参考:<https://docs.ansible.com/ansible/latest/modules/fetch_module.html>

    模块五:程序包管理工具--yum模块

    安装程序包(可接版本号):
    [root@ansible ~]# ansible web -m yum -a 'name=httpd state=present'
    安装最新程序包:
    [root@ansible ~]# ansible web -m yum -a 'name=httpd state=latest'
    卸载程序包:
    [root@ansible ~]# ansible web -m yum -a 'name=httpd state=absent'

    相关选项:

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

    参考:<https://docs.ansible.com/ansible/latest/modules/yum_module.html>

    注意:此模块只能在Python2上使用,在Python3上需要使用dnf模块用于包管理

    使用参考:<https://docs.ansible.com/ansible/latest/modules/dnf_module.html>

    模块六:服务程序管理--Service模块

    启动服务:
    [root@ansible ~]# ansible web -m service -a 'name=httpd state=started'
    开机启动服务:
    [root@ansible ~]# ansible web -m service -a 'name=httpd enabled=yes'

    相关选项:

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

    参考:<https://docs.ansible.com/ansible/latest/modules/service_module.html>

    模块七:管理计划任务--cron模块

    [root@ansible ~]# ansible web -m cron -a 'name="sync time" minute=*/10 job="/usr/sbin/ntpdate ntp.aliyun.com"'

    相关选项:

    backup=        # 如果设置,创建一个crontab备份
    cron_file=     #如果指定, 将使用这个文件替换远程主机上cron.d目录下用户的任务计划,而不是单个用户计划任务
    
    day=           # 日应该运行的工作( 1-31, *, */2, etc )
    hour=          # 小时 ( 0-23, *, */2, etc )
    
    minute=        #分钟( 0-59, *, */2, etc )
    
    month=         # 月( 1-12, *, */2, etc )
    
    weekday        # 周 ( 0-6 for Sunday-Saturday, *, etc )
    job=           #指明运行的命令是什么
    name=          #定时任务描述
    reboot         # 任务在重启时运行,不建议使用,建议使用special_time
    special_time   # 指定什么时候执行,参数:reboot,annually,monthly,weekly,daily,hourly
    state          #指定状态,prsent表示添加定时任务,也是默认设置,absent表示删除定时任务
    user           # 以哪个用户的身份执行
    

    参考:<https://docs.ansible.com/ansible/latest/modules/cron_module.html>

    模块八:远程主机执行本地脚本--script模块

    [root@ansible ~]# ansible web -m script -a '/tmp/a.sh'

    相关选项:

    creates:一个文件名,当文件名存在,则该命令不执行
    chdir:在执行指令之前,先切换到该目录
    free_from:要执行的Linux指令
    removes:一个文件名,当文件名不存在,则该命令不执行
    executable:切换shell来执行指令,该执行路径必须是一个绝对路径
    decrypt:使用Vault控制源文件的自动解码,选项yes|no

    参考:<https://docs.ansible.com/ansible/latest/modules/script_module.html>

    模块九:用户管理--user模块

    创建加密密码:
    [root@ansible ~]# echo 123456 | openssl passwd -1 -stdin
    $1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0
    
    创建新用户:
    [root@ansible ~]# ansible web -m user -a 'name=wolf system=yes password=$1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0 state=present'
    更新用户密码:
    [root@ansible ~]# ansible web -m user -a 'name=wolf update_password=always password=$1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0'
    删除用户:
    [root@ansible ~]# ansible web -m user -a 'name=wolf remove=yes state=absent'

    相关选项:

    comment    # 用户的描述信息
    createhom  # 是否创建家目录,yes,默认项,即创建家目录;no,创建用户时不创建家目录.
    force      # 在使用`state=absent'是, 行为与`userdel --force'一致.
    group      # 指定基本组
    groups     # 指定附加组,如果指定为('groups=')表示删除所有组
    home       # 指定用户家目录
    login_class     #可以设置用户的登录类 FreeBSD, OpenBSD and NetBSD系统.
    move_home       # 如果设置为`home='时, 试图将用户主目录移动到指定的目录
    name=           # 指定用户名
    non_unique      # 该选项允许改变非唯一的用户ID值
    password        # 指定用户密码,password参数需要接受md5加密后的值
    remove          # yes:删除家目录,需要指定此参数;no:默认项,删除用户时默认不删除用户家目录.
    shell           # 指定默认shell
    state           #设置帐号状态,默认值为present表示创建,可不写,指定值为absent表示删除
    system          # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户。
    uid             #指定用户的uid
    update_password # 更新用户密码,always:新密码和旧密码不同时进行修改,on_create:为新建用户指定密码
    expires         #指明密码的过期时间

    参考:<https://docs.ansible.com/ansible/latest/modules/user_module.html>

    模块十:收集远程主机的信息--setup模块

    收集可用的facts用于内建变量。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。使用filter参数过滤,如redhat系列用yum安装,而debian系列用apt来安装软件

    [root@ansible ~]# ansible web -m setup –a ‘filter=ansible_*_mb’

    参考:<https://docs.ansible.com/ansible/latest/modules/setup_module.html>

  • 相关阅读:
    python下载文件(图片)源码,包含爬网内容(爬url),可保存cookie
    查看linux下各数据类型的大小
    linux 内核代码精简
    前序 中序 后序 遍历 递归 非递归算法 java实现
    netflix turbine概述
    How Hystrix Works?--官方
    netflix ribbon概述
    spring-cloud-netflix集成的服务
    支付系统设计包含:账户,对账,风控...!史上最全的!--转
    利用CORS实现跨域请求--转
  • 原文地址:https://www.cnblogs.com/walkman-sky/p/9458682.html
Copyright © 2011-2022 走看看