Ansible命令模块(mount模块 selinux模块 firewalld模块 )

 

1.磁盘挂载mount模块

1)帮助语法

EXAMPLES:
# Before 2.3, option 'name' was used instead of 'path'
- name: Mount DVD read-only
  mount:
    path: /mnt/dvd            #挂载的目录(nfs客户端)
    src: /dev/sr0            #远端被挂载的目录 (nfs服务端)
    fstype: nfs                #挂在类型
    opts: ro,noauto            #自动挂载的参数
    state: 
        present                #写入自动挂载,但实际没有挂咋,需要重启服务器
        unmounted            #取消临时挂载,但是没有清理自动挂载
        mounted                #写入自动挂载,并且直接挂载了(常用)
        absent                #取消临时挂载,并且清理自动挂载(常用)

 

2)准备挂载的服务端

1.安装nfs
[root@m01 ~]# ansible nfs -m yum -a 'name=nfs-utils state=present'
2.配置nfs
[root@m01 ~]# ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'
3.创建用户
[root@m01 ~]# ansible nfs -m group -a 'name=www gid=666'
[root@m01 ~]# ansible nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no'
4.创建目录并授权
[root@m01 ~]# ansible nfs -m file -a 'path=/data state=directory owner=www group=www'
5.启动
[root@m01 ~]# ansible nfs -m systemd -a 'name=nfs state=started'

 

3)客户端使用模块挂载

#挂载目录,并加入开机自动挂载
[root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=mounted'

#取消挂载,并取消开机自动挂载
[root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=absent'

 

 

 

2.selinux模块

1)帮助语法

EXAMPLES:
- name: Enable SELinux
  selinux:
    policy: targeted        
    state: 
        enforcing            #开启
        disabled            #关闭

 

2)关闭selinux

[root@m01 ~]# ansible web01 -m selinux -a 'state=disabled'

 

 

 

3.firewalld模块

1)帮助语法

EXAMPLES:
- firewalld:
    service: https                    #防火墙开启的服务
    permanent:                      #永久生效
        yes                            
        no                            
    state:                          #临时生效
        enabled                        #开启
        disable                        #关闭
    port: 8081/tcp  161-162/udp          #防火墙配置的端口
    zone: dmz                        #指定配置空间
    rich_rule:                        #富规则
    source: 192.0.2.0/24             #防火墙配置的源ip
    masquerade: 
        yes                             #开启ip伪装
        no                            #关闭ip伪装
    interface: eth2                     #绑定网卡

 

2)防火墙配置实践

#1.允许访问http,永久生效
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'

#2.允许80端口被访问,临时生效
[root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'

#3.允许10.0.0.0/24网段访问22端口
[root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 service name=ssh accept" state=enabled'

#4.允许10.0.0.0/24网段访问所有服务
[root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'

 

posted @ 2020-10-07 14:22  六月OvO  阅读(2221)  评论(0编辑  收藏  举报