Linux架构24 ansible之get_url模块, 服务管理模块, 用户管理模块, 定时任务模块, 挂载模块, 防火墙模块, 解压模块

3.get_url模块

- name: Download foo.conf
  get_url:
      url: http://example.com/path/file.conf
      dest: /etc/foo.conf
      mode: '0440'
      checksum: md5:b5bb9...    #公司内部库,验证文件是否为要求的文件
    checksum: sha256:b5bb9...    #另一种验证方式

#下载文件
[root@m01 ~]# ansible 'web_group' -m get_url -a 'url=https://www.mumusir.com/download/1.txt dest=/tmp/ mode=777'

#验证文件再下载(验证失败不下载)    (checksum指定加密方式,这里指定为md5,后面为文件的md5值)
[root@m01 ~]# ansible 'web_group' -m get_url -a 'url=https://www.mumusir.com/download/1.txt dest=/tmp/ mode=777 checksum=md5:f447b20a7fcbf53a...'
#查看文件的md5值
[root@m01 ~]# md5sum 1.txt

 

Ansible服务管理模块

1.service/systemd

- name: Start service httpd, if not started
  service:
    name: httpd    #服务
    state:         #状态
           started    #启动
           stopped    #停止
           restarted    #重启
           reloaded    #重载
       enable:        #是否加入开机自启
           yes
           no
   
#启动服务
[root@m01 ~]# ansible 'web_group' -m service -a 'name=httpd state=started'

#重启服务
[root@m01 ~]# ansible 'web_group' -m service -a 'name=httpd state=restarted'

 

Ansible用户管理模块

1.group模块

- name: Ensure group "somegroup" exists
  group:
    name: somegroup    #组名
    state:             #状态
        present        #创建用户组
        absent        #删除用户组
    gid: 666        #指定gid

#创建用户组(如果用户组已存在,会修改gid)
[root@m01 ~]# ansible 'web_group' -m group -a 'name=www gid=666 state=present'
#修改(gid改成77)
[root@m01 ~]# ansible 'web_group' -m group -a 'name=www gid=77 state=present'
#查看组
[root@web01 ~]# vim /etc/group

#删除用户组
[root@m01 ~]# ansible 'web_group' -m group -a 'name=www gid=666 state=absent'

 

2.user模块

  user:
    name: johnd                #用户名
    comment: John Doe        #用户备注
    uid: 1040                #指定uid
    group: admin            #指定用户组(这里是组名,不是组id)
    shell: /bin/bash        #指定用户登录脚本
    generate_ssh_key:         #是否创建密钥对
        yes
        no
    ssh_key_bits: 2048        #秘钥长度
    ssh_key_file: .ssh/id_rsa    #秘钥文件位置
    state: 
        absent                #删除用户
        present                #创建用户
    remove: 
        yes                    #删除用户的家目录
    expires:                 #用户过期时间
    
#单机创建用户命令    (-s指定用户登录脚本(这里不用用户登录),-M不创建家目录)
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M    

#创建用户,不需要登录,有家目录(state默认为present,可不写)
[root@m01 ~]# ansible 'web_group' -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present'

#只删除用户(当组的名字和用户名相同时,组也被删除)
[root@m01 ~]# ansible 'web_group' -m user -a 'name=www state=absent'

#删除用户和家目录
[root@m01 ~]# ansible 'web_group' -m user -a 'name=www state=absent remove=yes'

注意:
    1.当组的名字和用户名相同是,组也被删除
    2.当组的下面有多个用户,即使组的名字和用户名相同也不会删除

 

Ansible定时任务模块

1.cron模块

- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
  cron:
    name: "check dirs"    #脚本的备注
    minute: "0"            #分钟
    hour: "5,2"            #小时
    day:                #
    month:                #
    weekday:            #
    job: "ls -alh > /dev/null"    #执行的内容
    disabled:
        yes                #注释定时任务
        no                #解除注释
    
#添加定时任务(有名字,再次执行就是更新)
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=test minute=* hour=* day=* month=* weekday=* job="/bin/bash /tmp/touch.sh"'
[root@web01 ~]# crontab -l
#Ansible: test
* * * * * /bin/bash /tmp/touch.sh
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=test minute=3,5 hour=8-11 day=*/2 job="/bin/bash /tmp/touch.sh"'
#添加定时任务(没名字,再次执行继续添加,远端查看定时任务注释名称为None)
[root@m01 ~]# ansible 'web_group' -m cron -a 'minute=* hour=* day=* month=* weekday=* job="/bin/bash /root/mkdir.sh"'

#不配置时间常数,默认是*
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=test job="/bin/bash /tmp/touch.sh"'
[root@web01 ~]# crontab -l
#Ansible: test
* * * * * /bin/bash /tmp/touch.sh

#删除定时任务(只能根据名字删除,必须指定name参数)
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=test state=absent'
#删除定时任务(添加任务未给名字,名字写None)
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=None state=absent'

#注释定时任务(job得添加,不然会报错)    一般用不到
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=test job="/bin/bash /tmp/touch.sh" disabled=yes'
[root@web01 ~]# crontab -l
#Ansible: test
#* * * * * /bin/bash /tmp/touch.sh
#解除注释
[root@m01 ~]# ansible 'web_group' -m cron -a 'name=test job="/bin/bash /tmp/touch.sh" disabled=no'

 

Ansible磁盘挂载模块

1.mount模块

1)准备挂载的服务器

#1.安装nfs模块
[root@m01 ~]# ansible 'nfs' -m yum -a 'name=nfs-utils state=present'

#2.配置nfs(配置用no_all_squash)
[root@m01 ~]# ansible 'nfs' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,no_all_squash)" dest=/etc/exports'
#3.创建目录
[root@m01 ~]# ansible 'nfs' -m file -a 'name=/data state=directory'

#2.配置nfs(配置用all_squash)
[root@m01 ~]# ansible 'nfs' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash)" 
#3.创建目录
[root@m01 ~]# ansible 'nfs' -m file -a 'name=/data state=directory owner=nfsnobody group=nfsnobody'

#4.启动服务(加入开机自启)
[root@m01 ~]# ansible 'nfs' -m service -a 'name=/data state=started enabled=yes'

2)mount模块挂载

- name: Mount DVD read-only
  mount:
    path: /mnt/dvd        #本地要挂载的目录
    src: /dev/sr0        #远端挂载点目录
    fstype: iso9660        #挂载类型
    opts: ro,noauto        #/etc/fstab参数(默认会写上,不用管他)
    state:
        present            #开机才挂载,将配置写到自动挂载
        mounted            #直接挂载,并且配置写到自动挂载            (常用)
        unmounted        #取消挂载,但是不清除/etc/fstab自动挂载配置
        absent            #取消挂载,并清除/etc/fstab自动挂载配置     (常用)
        
#挂载(被控端没有挂上,而是写到开机启动的文件/etc/fstab中,重启或mount -a就会挂上)
[root@m01 ~]# ansible 'web_group' -m mount -a 'src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=present'

#挂载(被控端挂上,并写到开机启动的文件/etc/fstab中)
[root@m01 ~]# ansible 'web_group' -m mount -a 'src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=mounted'

#取消挂载(清除开机自启/etc/fstab)
[root@m01 ~]# ansible 'web_group' -m mount -a 'src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=absent'

#取消挂载(不清除开机自启/etc/fstab)
[root@m01 ~]# ansible 'web_group' -m mount -a 'src=172.16.1.31:/data path=/tmp fstype=nfs opts=defaults state=unmounted'

 

Ansible防火墙模块

1.selinux模块

- name: Enable SELinux
  selinux:
    policy: targeted
    state: disabled
    
#关闭selinux
[root@m01 ~]# ansible 'all' -m selinux -a 'state=disabled'

2.firewalld模块

- firewalld:
    service: https                    #指定服务
    permanent:                        #是否永久生效
        yes                            #永久生效
        no                        #临时生效(默认)
    state: enabled                    #状态
        enabled                        #允许
        disabled                    #禁止
    port:                             #端口
        8081/tcp                       #指定端口
        161-162/udp                    #端口范围
    rich_rule:                        #富规则
    source: 192.0.2.0/24            #指定网段或IP
    zone: trusted                    #指定空间

# 允许访问http服务(如果防火墙没启动会报错)    permanent为永久生效,被控端当前需要重新加载才生效
[root@m01 ~]# ansible 'web_group' -m firewalld -a 'service=http permanent=yes state=enabled'
# 启动防火墙
[root@m01 ~]# ansible 'web_group' -m systemd -a 'name=firewalld state=started'

# 被控端防火墙重新加载
[root@web01 ~]# firewall-cmd --reload
# 被控端查看防火墙设定
[root@web01 ~]# firewall-cmd --list-all

# 允许访问80端口
[root@m01 ~]# ansible 'web_group' -m firewalld -a 'port=80/tcp permanent=yes state=enabled'
# 临时生效,允许访问80端口(远端马上生效,重启就没了)
[root@m01 ~]# ansible 'web_group' -m firewalld -a 'port=80/tcp permanent=no state=enabled'

#允许10.0.0.0/24网段访问22端口  (富规则)
[root@m01 ~]# ansible 'web_group' -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.1 service name=http accept" state=enabled'

#配置网段白名单(必须要带permanent参数)
[root@m01 ~]# ansible 'web_group' -m firewalld -a 'source=10.0.0.1/24 zone=trusted state=enabled permanent=no'
#查看空间规则
[root@web01 ~]# firewall-cmd --list-all --zone=trusted

 

Ansible解压缩模块

1.unarchive模块

- name: Extract foo.tgz into /var/lib/foo
  unarchive:
    src: foo.tgz            #压缩包的路径及文件
    dest: /var/lib/foo        #压缩到远端服务器的位置
    remote_src: 
        yes                    #压缩包在被控端服务器
        no                    #压缩包在控制端

#控制端解压到远端服务器上
[root@m01 ~]# ansible 'web_group' -m unarchive -a 'src=/root/php.tar.gz dest=/tmp/'

#控制端把php.tar.gz拷贝到远端服务器
[root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/php.tar.gz dest=/root/'

#把远端服务器的/root/php.tar.gz解压到远端的/tmp/下
[root@m01 ~]# ansible 'web_group' -m unarchive -a 'src=/root/php.tar.gz dest=/tmp/ remote_src=yes'

2.archive模块

- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
  archive:
    path: /path/to/foo            #要打包的内容
    dest: /path/to/foo.tgz        #包位置与名字

#打包被控端的文件到被控端
[root@m01 ~]# ansible 'web_group' -m archive -a 'path=/data dest=/tmp/data.tar.gz'

 

posted @ 2024-03-08 17:19  战斗小人  阅读(9)  评论(0编辑  收藏  举报