• Ansible常用模块


    Ansible模块

    command 模块

    不支持管道符等特殊字符,用于执行系统命令,不仅限于linux。和shell模块差不多。

    [root@m01 ~]# ansible web_group -m shell -a 'df -h'
    [root@m01 ~]# ansible web_group -m command -a 'df -h'
    
    

    shell 模块

    用于执行命令

    [root@m01 ~]# ansible web_group -m shell -a 'df -h'
    

    script 模块

    在本地执行的脚本,功能可以同步到其它机器上面去,被控制机器不需要脚本。

    [root@m01 ~]# ansible web01 -m script -a '/root/a.sh'
    

    yum 模块

    # 查看帮助
    [root@m01 ~]# ansible-doc yum
    
    # 安装httpd
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=latest'
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=present'
    # 一般使用present
    
    # 卸载httpd
    absent
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=absent'
    
    # 指定网络的rpm包
    [root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.1-1.el7.x86_64.rpm state=present'
    
    # 类似于
    yum -y install https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/SRPMS/zabbix-4.4.1-1.el7.src.rpm 
    
    # 在db组安装nginx,前提是db组上的主机有本地安装的包。
    [root@m01 ~]# ansible db_group -m yum -a 'name=/root/nginx-1.18.0-1.el7.ngx.x86_64.rpm state=present'
    

    需要掌握的方法

    name:
    	指定安装的软件。
    	可以是本地的也可以是远程的链接包
    state:
    	prsent	正常安装,类似于yum -y install 
    	absent	删除、卸载
    	lastet	最新版本
    

    yum_repository 模块

    [root@m01 ~]# ansible-doc yum_repository
    
    [root@m01 ~]# ansible web_group -m yum_repository -a 'name=gong description=gong baseurl=zp.gong.com gpgcheck=0 enabled=no'
    
    # web01生成的文件
    [root@web01 ~]# cat /etc/yum.repos.d/gong.repo 
    [gong]
    baseurl = zp.gong.com
    enabled = 0
    gpgcheck = 0
    name = gong
    
    # file指的是yum仓库的文件名,gong是[ ] 里面的内容,des是name的内容
    [root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=gong description=gong baseurl=zp.gong.com gpgcheck=0 enabled=no'
    
    # web01生成的文件
    [root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
    [gong]
    baseurl = zp.gong.com
    enabled = 0
    gpgcheck = 0
    name = gong
    
    # 删除yum仓库文件,最好是把文件名和仓库名都加上,防止误删。
    [root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=gong state=absent'
    
    # 直接在文件里面添加仓库,file不变,其它参数改变就会加上
    [root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=liao description=liao baseurl=tencent.gong.com gpgcheck=0 enabled=yes'
    
    
    [root@web01 /etc/yum.repos.d]# cat nginx.repo 
    [xiao]
    baseurl = qq.gong.com
    enabled = 1
    gpgcheck = 0
    name = xiao
    
    [liao]
    baseurl = tencent.gong.com
    enabled = 1
    gpgcheck = 0
    name = liao
    
    
    name        #指定仓库名,如果没有file,则仓库名为文件名
    baseurl     #指定yum源
    description   # yum配置文件中的name
    gpgcheck    #指定检查秘钥,也可用数字
        no
        yes
    
    enabled     #是否启用仓库
        no
        yes
    

    文件管理模块

    copy 模块

    相当于scp

    [root@m01 ~]# ansible-doc copy
    
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/fenfa.sh dest=/root/fenfa_miyue.sh owner=root group=root mode=644'
    
    src		# 指定原文件
    dest	# 指定目标位置及文件名,可以改名
    owner	# 属主
    group	# 指定属组
    mode	# 指定权限
    
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/fenfa.sh dest=/root/fenfa_miyue.sh owner=root group=root mode=644 backup=yes'
    
    backup 	# 如果原文件变了去覆盖已经有的文件,会把目标机的文件备份一遍,默认不开启。
    
    [root@web03 ~]# ll
    total 16136
    -rw-------. 1 root root     1444 Apr 30 20:47 anaconda-ks.cfg
    -rw-r--r--. 1 root root      287 May  1 00:05 change_ip.sh
    -rw-r--r--  1 root root      149 Jun  9 09:51 fenfa_miyue.sh
    -rw-r--r--  1 root root      152 Jun  9 09:34 fenfa_miyue.sh.11536.2020-06-09@09:51:56~
    
    # 回滚
    [root@m01 ~]# ansible web_group -m copy -a 'src=/root/fenfa_miyue.sh.12398.2020-06-09@09:51:56~ dest=/root/fenfa_miyue.sh remote_src=yes'
    
    remote_src=yes    # 是否在远端主机操作
    
    [root@m01 ~]# ansible web_group -m copy -a 'content=gong:123 dest=/root/rsync.pass owner=root group=root mode=600'
    
    content=gong:123  # 把里面的内容写到,目标主机指定文件中。
    

    file 模块

    作用:

    • 创建目录
    • 创建文件
    • 创建软链接
    • 删除目录、文件,软链接
    # 创建目录
    [root@m01 ~]# ansible all -m file -a 'path=/opt/test owner=root group=root mode=755 state=directory'
    path		# 指定文件或者目录
    owner		# 指定属主
    group		# 指定属组
    mode		# 指定权限
    src			# 做软、硬链接的时候使用
    recurse		# 递归授权
    	yes
    	no
    state:	
    	absent		# 
    	touch		# 创建文件
    	directory	# 目录
    	link		# 软链接
    	hard		# 硬连接
    	file		# 配合
    # 递归授权
    [root@m01 ~]# ansible web_group -m file -a 'path=/website owner=www group=www recurse=yes'
    
    

    get_url 模块

    [root@m01 ~]# ansible web_group -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.0-1.el7.x86_64.rpm dest=/tmp mode=0644' -i ./hosts
    
    url             #指定下载地址
    dest            #指定下载的目录
    mode            #指定权限
    checksum        #校验加密算法
        md5
        sha256
    

    service 模块

    支持跨平台

    systemd

    仅支持centos7

    [root@m01 ~]# ansible web_group -m service -a 'name=crond state=started'
    
    name		# 服务名
    state
    	stopped		# 停止服务
    	started		# 开始服务
    enabled
    	yes
    	no
    

    user 模块

    [root@m01 ~]# ansible all -m user -a 'name=john uid=1024 group=root shell=/sbin/nologin create_home=false'
    
    name	# 用户名
    uid		# 指定uid
    group	# 指定属组
    shell	# 指定解释器
    create_home		# 是否创建家目录
    	yes
    	no
    comment		# 指定注释 -c
    groups		# 指定附加组,配合append,如果不加就会覆盖-G
    append		# 附加组 -a
    remove		# 删除用户的时候,是否同时删除家目录
    	true,yes	# 删除
    	flase,no	# 不删除
    state
    	present		# 创建
    	absent		# 删除
        
    generate_ssh_key: 是否创建密钥对
    	yes		# 创建
    	no		# 不创建
    ssh_key_bits	# 指定密钥的加密长度
    ssh_key_file	# 指定私钥文件的位置
    
    system			# 是否是系统用户 -r
    	yes			是
    	no			不是
    

    group 模块

    # 添加组
    [root@m01 ~]# ansible all -m group -a 'name=xxxx gid=897 state=present'
    

    cron 模块

    # 创建定时任务
    [root@m01 ~]# ansible all -m cron -a "name='sync time' minute=*/5 job='ntpdate ntp1.aliyun.com &> /dev/null'"
    
    # 效果
    [root@web03 ~]# crontab -l
    #Ansible: sync time
    */5 * * * * ntpdate ntp1.aliyun.com &> /dev/null
    
    # 删除定时任务
    [root@m01 ~]# ansible all -m cron -a "name='sync time' state=absent"
    
    name		# 定时任务的名字,备注,删除的时候是用它来注释的。
    state
    	present
    	absent
    minute	分0-59
    hour	时0-23
    day		日1-31
    month		月1-12
    weekday		周0-6
    

    mount 模块

    [root@m01 ~]# ansible web_group -m mount -a 'path=/website/wp/wp-content/uploads src=172.16.1.31:/data/wp_data fstype=nfs state=mounted'
    
    path		# 挂载到本地的路径
    src			# 挂载源
    fstype		# 挂载的文件系统
    	nfs
    	ext4
    	ext3
    state
    	present		# 只写文件,挂载的东西写到/etc/fstab
    		推荐使用  :mounted		# 即写入文件,又挂载
    		推荐使用  :	absent		# # 卸载设备,并清理开机自动挂载文件
    	unmounted		# 只会卸载,不会清除/etc/fstab
    

    slinux 模块

    # 关闭selinux
    [root@m01 ~]# ansible all -m selinux -a 'state=disabled'
    
    state
    	enforcing
    	permisive
    	disabled
    

    firewalld 模块

    # 指定服务的方式去开放
    [root@m01 ~]# ansible all -m firewalld -a 'service=httpd permanent=no state=enabled'
    [root@m01 ~]# ansible nfs -m firewalld -a 'port=111/tcp permanent=no state=enabled'
    
    service		# 需要控制的服务
    port		# 需要设定的端口
    permanent	# 是否临时生效
    state		# 让这个服务通行,开启或者关闭
    	enabled
    	disabled
    

    setup 模块

    主机系统的所有信息

    [root@m01 ~]# ansible web01 -m setup
    
  • 相关阅读:
    编译安装LEMP
    eAccelerator配置和使用指南
    /dev/null的用途
    分库分表
    JVM
    SOFA 数据透析
    HTTPS
    SOFA 通信
    分布式锁
    mysql 锁
  • 原文地址:https://www.cnblogs.com/gshelldon/p/13374967.html
Copyright © 2020-2023  润新知