美文网首页
2020-03-18 Ansible模块详解

2020-03-18 Ansible模块详解

作者: 阿丧小威 | 来源:发表于2020-03-18 17:22 被阅读0次

    1. ansible命令和脚本类型模块介绍与实践

    1.1 command模块功能说明

    功能说明:在远程节点上执行一个命令(用于执行一个命令)。

    command模块功能参数

    管理实践:利用command模块实现批量管理。
    示例1:获取所有机器的负载信息。

    [root@m01 ~]# ansible oldgirl -m command -a "uptime"
    # 说明:command模块为基本命令模块,可以省略不用必须指定。
    # -m指定使用的模块
    # -a指定使用模块中相应的命令参数
    192.168.9.7 | SUCCESS | rc=0 >>
     11:07:50 up 1 day,  4:22,  2 users,  load average: 0.00, 0.01, 0.05
    
    192.168.9.9 | SUCCESS | rc=0 >>
     11:00:34 up 1 day,  1:36,  2 users,  load average: 0.00, 0.01, 0.05
    # 提示:uptime可以换为任意的系统命令,注意不能带管道及重定向符号。
    # 特殊:不支持的东西,如 > < | &等 $HOME,替代方案用shell模块
    ansible oldboy -m shell -a "ps -ef | grep ssh"
    ansible oldboy -m shell -a "echo oldboy > /tmp/a.log"
    

    示例2:切换到/etc/目录。
    切换到/etc/目录,然后输出当前目录。

    [root@m01 ~]# ansible oldboy -m command -a "pwd chdir=/etc"
    192.168.9.5 | SUCCESS | rc=0 >>
    /etc
    192.168.9.6 | SUCCESS | rc=0 >>
    /etc
    [root@m01 ~]# ansible oldboy -m command -a "pwd"
    192.168.9.6 | SUCCESS | rc=0 >>
    /root
    192.168.9.5 | SUCCESS | rc=0 >>
    /root
    提示:command的参数chdir=/etc配置相当于cd /etc。
    

    示例3:command的参数creates实践。
    参数:pwd creates=/etc相当于Shell的条件测试 [ -e /etc ] || pwd,即存在/etc目录就不执行对应的命令操作,如果不存在就执行相应命令操作,和下面的removes相反。

    [root@m01 ~]# ansible oldboy -m command -a "pwd creates=/oldgirl"
    ---如果不存在/oldgirl目录或文件,就执行pwd,反之不执行。
    192.168.9.6 | SUCCESS | rc=0 >>    ---因为/oldgirl不存在,所以执行了pwd,打印了/root目录
    /root
    192.168.9.5 | SUCCESS | rc=0 >>
    /root
    
    [root@m01 ~]# ansible oldboy -m command -a "pwd creates=/etc"
    192.168.9.5 | SUCCESS | rc=0 >>    ---因为/etc存在,所以没有执行pwd,无正确输出,只提示/etc/存在。
    skipped, since /etc exists
    192.168.9.6 | SUCCESS | rc=0 >>
    skipped, since /etc exists
    

    示例4:command的参数removes实践。
    参数:ls /opt removes=/opt相当于条件测试[ -e /opt ] && ls /opt,即存在/opt目录就执行对应的命令操作,如果不存在就不执行相应的命令操作,和前文的creates参数作用相反。

    [root@m01 ~]# ansible oldboy -m command -a "ls /opt removes=/opt"
    192.168.9.6 | SUCCESS | rc=0 >>
    a
    b
    c
    d
    etc
    hosts
    192.168.9.5 | SUCCESS | rc=0 >>
    null
    [root@m01 ~]# ansible oldboy -m command -a "ls /old removes=/old"
    192.168.9.5 | SUCCESS | rc=0 >>
    skipped, since /old does not exist
    192.168.9.6 | SUCCESS | rc=0 >>
    skipped, since /old does not exist
    

    示例5:command的参数warn实践。
    参数:warn=False忽略警告的意思。

    [root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts"
     [WARNING]: Consider using file module with mode rather than running chmod    ---警告信息
    192.168.9.5 | SUCCESS | rc=0 >>
    192.168.9.6 | SUCCESS | rc=0 >>
    [root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"    ---忽略警告
    192.168.9.6 | SUCCESS | rc=0 >>
    192.168.9.5 | SUCCESS | rc=0 >>
    

    1.2 shell模块功能说明

    功能说明:在远程节点上执行命令(可以是多个命令)。
    示例6:批量执行ps -ef | grep sshd | grep -v grep命令。

    [root@m01 ~]# ansible oldboy -m shell -a "ps -ef | grep sshd | grep -v grep"
    192.168.9.5 | SUCCESS | rc=0 >>
    root       6738      1  0 2月29 ?       00:00:00 /usr/sbin/sshd -D
    root      16241   6738  0 09:53 ?        00:00:00 sshd: root@pts/0
    192.168.9.6 | SUCCESS | rc=0 >>
    root       6791      1  0 3月11 ?       00:00:00 /usr/sbin/sshd -D
    root      10454   6791  0 11:55 ?        00:00:00 sshd: root@pts/0
    # command模块不支持管道等特殊操作
    [root@m01 ~]# ansible oldboy -m command -a "ps -ef | grep sshd | grep -v grep"
    192.168.9.5 | FAILED | rc=1 >>    ---输出报错
    error: garbage option
    Usage:
     ps [options]
     Try 'ps --help <simple|list|output|threads|misc|all>'
      or 'ps --help <s|l|o|t|m|a>'
     for additional help text.
    For more details see ps(1).non-zero return code
    192.168.9.6 | FAILED | rc=1 >>
    error: garbage option
    Usage:
     ps [options]
     Try 'ps --help <simple|list|output|threads|misc|all>'
      or 'ps --help <s|l|o|t|m|a>'
     for additional help text.
    For more details see ps(1).non-zero return code
    

    示例7:批量执行远程脚本。
    注意:脚本必须事先存在于本地节点,否则无法执行。

    # 被管理节点Web01编写远程执行脚本。
    [root@web01 ~]# echo pwd > /server/scripts/pwd.sh
    [root@web01 ~]# cat /server/scripts/pwd.sh 
    pwd
    [root@web01 ~]# chmod +x /server/scripts/pwd.sh
    [root@m01 ~]# ansible oldgirl -m shell -a "/server/scripts/pwd.sh"
    192.168.9.7 | SUCCESS | rc=0 >>
    /root
    192.168.9.9 | FAILED | rc=127 >>
    /bin/sh: /server/scripts/pwd.sh: 没有那个文件或目录non-zero return code
    说明:利用shell模块实现批量执行远程主机脚本时,脚本必须在远程主机上存在,并且授权为执行权限。
    

    1.3 script模块功能说明

    功能说明:把本地脚本传输到远程节点上并运行脚本。
    和shell模块比,script模块更强大,本地有一份脚本就可以在所有机器上执行。

    script模块功能参数说明

    示例8:批量执行远程脚本。

    # 管理节点m01编写脚本
    [root@m01 ~]# echo -e "pwd\nhostname" >/server/scripts/pwd.sh
    [root@m01 ~]# cat /server/scripts/pwd.sh 
    pwd
    hostname
    [root@m01 ~]# chmod +x /server/scripts/pwd.sh
    # 批量运行脚本文件
    [root@m01 ~]# ansible oldgirl -m script -a "/server/scripts/pwd.sh"
    192.168.9.7 | SUCCESS => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.9.7 closed.\r\n", 
        "stdout": "/root\r\nweb01\r\n", 
        "stdout_lines": [
            "/root", 
            "web01"
        ]
    }
    192.168.9.9 | SUCCESS => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.9.9 closed.\r\n", 
        "stdout": "/root\r\nweb02\r\n", 
        "stdout_lines": [
            "/root", 
            "web02"
        ]
    }
    说明:利用script模块实现批量执行远程主机脚本时,脚本不用在远程主机上存在和授权。
    

    2. Ansible文件类型模块介绍与实践

    2.1 copy模块功能说明

    功能说明:复制文件到远程主机。

    copy模块功能参数1 copy模块功能参数2

    示例9:批量远程复制文件
    批量远程复制并设置用户、用户组、权限属性。

    # 执行批量复制文件到远程主机命令。
    [root@m01 ~]# ansible oldboy -m copy -a "src=/etc/passwd dest=/tmp/oldgirl.txt owner=oldboy group=oldboy mode=0755"
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "checksum": "f46e74616780e28c950837c16571665b058c3233", 
        "dest": "/tmp/oldgirl.txt", 
        "gid": 1000, 
        "group": "oldboy", 
        "md5sum": "388824fe0e2029fba5ef752f0e0fab2c", 
        "mode": "0755", 
        "owner": "oldboy", 
        "size": 798, 
        "src": "/root/.ansible/tmp/ansible-tmp-1584426871.96-177865870784265/source", 
        "state": "file", 
        "uid": 1000
    }
    192.168.9.6 | SUCCESS => {
        "changed": true, 
        "checksum": "f46e74616780e28c950837c16571665b058c3233", 
        "dest": "/tmp/oldgirl.txt", 
        "gid": 1000, 
        "group": "oldboy", 
        "md5sum": "388824fe0e2029fba5ef752f0e0fab2c", 
        "mode": "0755", 
        "owner": "oldboy", 
        "size": 798, 
        "src": "/root/.ansible/tmp/ansible-tmp-1584426871.96-226674117663169/source", 
        "state": "file", 
        "uid": 1000
    }
    说明:copy模块为数据推送模块,可以将数据推送到远程节点主机中,并且可以对文件进行属性权限修改。
    
    # 登录到远程主机上检查批量操作结果。
    [root@m01 ~]# ansible oldboy -m command -a "ls -l /tmp/oldgirl.txt"
    192.168.9.6 | SUCCESS | rc=0 >>
    -rwxr-xr-x 1 oldboy oldboy 798 3月  12 15:10 /tmp/oldgirl.txt
    192.168.9.5 | SUCCESS | rc=0 >>
    -rwxr-xr-x 1 oldboy oldboy 798 3月   4 13:08 /tmp/oldgirl.txt
    

    示例10
    远程批量复制文件前进行备份,并写入指定内容到文件。

    # 执行批量复制文件到远程主机命令。
    [root@m01 ~]# ansible oldboy -m copy -a "content='I am oldboy' dest=/tmp/oldgirl.txt backup=yes"
    192.168.9.5 | SUCCESS => {
        "backup_file": "/tmp/oldgirl.txt.16864.2020-03-04@13:13:50~", 
        "changed": true, 
        "checksum": "e5fcfd07f21f35adb159418b01b1f61274fb9134", 
        "dest": "/tmp/oldgirl.txt", 
        "gid": 1000, 
        "group": "oldboy", 
        "md5sum": "ba56bdae36d0506a311e3098b8e97882", 
        "mode": "0755", 
        "owner": "oldboy", 
        "size": 11, 
        "src": "/root/.ansible/tmp/ansible-tmp-1584427221.9-51998958880411/source", 
        "state": "file", 
        "uid": 1000
    }
    192.168.9.6 | SUCCESS => {
    ---与上面相同,省略若干行---
    }
    ---检查内容是否修改
    [root@m01 ~]# ansible oldboy -m command -a "cat /tmp/oldgirl.txt"
    192.168.9.6 | SUCCESS | rc=0 >>
    I am oldboy
    192.168.9.5 | SUCCESS | rc=0 >>
    I am oldboy
    ---检查是否备份,command模块无法使用通配符
    [root@m01 ~]# ansible oldboy -m command -a "ls /tmp/oldgirl.txt*"
    192.168.9.5 | FAILED | rc=2 >>
    ls: 无法访问/tmp/oldgirl.txt*: 没有那个文件或目录non-zero return code
    192.168.9.6 | FAILED | rc=2 >>
    ls: 无法访问/tmp/oldgirl.txt*: 没有那个文件或目录non-zero return code
    ---检查是否备份,shell模块可以使用通配符
    [root@m01 ~]# ansible oldboy -m shell -a "ls /tmp/oldgirl.txt*"
    192.168.9.5 | SUCCESS | rc=0 >>
    /tmp/oldgirl.txt
    /tmp/oldgirl.txt.16864.2020-03-04@13:13:50~
    192.168.9.6 | SUCCESS | rc=0 >>
    /tmp/oldgirl.txt
    /tmp/oldgirl.txt.11076.2020-03-12@15:15:55~
    

    2.2 file模块功能说明

    功能说明:创建及设置文件(目录)属性。

    file模块参数表

    示例11:创建数据文件(普通文件、目录、软链接文件)

    # 远程创建目录信息。
    [root@m01 ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0755", 
        "owner": "root", 
        "path": "/tmp/oldboy_dir", 
        "size": 6, 
        "state": "directory", 
        "uid": 0
    }
    192.168.9.6 | SUCCESS => {
    ---与上面相同,省略若干行---
    }
    # 远程创建文件信息
    [root@m01 ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file owner=ftp group=oldboy mode=777 state=touch"
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "dest": "/tmp/oldboy_file", 
        "gid": 1000, 
        "group": "oldboy", 
        "mode": "0777", 
        "owner": "ftp", 
        "size": 0, 
        "state": "file", 
        "uid": 14
    }
    192.168.9.6 | SUCCESS => {
    ---与上面相同,省略若干行---
    }
    [root@m01 ~]# ansible oldboy -m shell -a "ls -l /tmp/oldboy_file"
    192.168.9.5 | SUCCESS | rc=0 >>
    -rwxrwxrwx 1 ftp oldboy 0 3月   4 13:38 /tmp/oldboy_file
    192.168.9.6 | SUCCESS | rc=0 >>
    -rwxrwxrwx 1 ftp oldboy 0 3月  12 15:40 /tmp/oldboy_file
    # 远程创建链接文件
    [root@m01 ~]# ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "dest": "/tmp/link_file", 
        "gid": 0, 
        "group": "root", 
        "mode": "0777", 
        "owner": "root", 
        "size": 10, 
        "src": "/etc/hosts", 
        "state": "link", 
        "uid": 0
    }
    192.168.9.6 | SUCCESS => {
    ---与上面相同,省略若干行---
    }
    # 登录到远程节点主机进行检查
    [root@m01 ~]# ansible oldboy -m shell -a "ls -l /tmp/"
    192.168.9.6 | SUCCESS | rc=0 >>
    总用量 12
    drwx------ 2 root   root    65 3月  12 15:43 ansible_2JCbDT
    lrwxrwxrwx 1 root   root    10 3月  12 15:42 link_file -> /etc/hosts    ---新建的链接目录
    drwxr-xr-x 2 root   root     6 3月  12 15:37 oldboy_dir    ---新建的目录
    -rwxrwxrwx 1 ftp    oldboy   0 3月  12 15:40 oldboy_file    ---新建的文件
    -rwxr-xr-x 1 oldboy oldboy  11 3月  12 15:15 oldgirl.txt
    -rwxr-xr-x 1 oldboy oldboy 798 3月  12 15:10 oldgirl.txt.11076.2020-03-12@15:15:55~
    
    192.168.9.5 | SUCCESS | rc=0 >>
    总用量 20
    ---与上面相同,省略若干行---
    说明:file模块可以实现数据信息创建,也可以修改数据属性信息。
    

    3. Ansible软件类型模块介绍与实践

    yum模块功能说明
    功能说明:yum包管理模块。

    yum模块功能参数

    示例12:批量安装nmap软件包。

    # 执行批量安装软件命令。
    [root@m01 ~]# ansible oldboy -m shell -a "rpm -qa nmap warn=false"
    192.168.9.5 | SUCCESS | rc=0 >>
    192.168.9.6 | SUCCESS | rc=0 >>
    [root@m01 ~]# ansible oldboy -m yum -a "name=nmap state=installed"
    192.168.9.5 | SUCCESS => {
        "changed": false, 
        "msg": "", 
        "rc": 0, 
        "results": [
            "2:nmap-6.40-19.el7.x86_64 providing nmap is already installed"
        ]
    }
    192.168.9.6 | SUCCESS => {
        "changed": false, 
        "msg": "", 
        "rc": 0, 
        "results": [
            "2:nmap-6.40-19.el7.x86_64 providing nmap is already installed"
        ]
    }
    # 执行结果验证
    [root@m01 ~]# ansible oldboy -m shell -a "rpm -qa nmap warn=false"
    192.168.9.5 | SUCCESS | rc=0 >>
    nmap-6.40-19.el7.x86_64
    192.168.9.6 | SUCCESS | rc=0 >>
    nmap-6.40-19.el7.x86_64
    

    Ansible yum模块背后的原理:/usr/bin/python /usr/bin/yum -y install nmap。

    4. Ansible网络服务类型模块介绍与实践

    4.1 service/systemd模块功能说明

    service/systemd模块功能参数说明

    示例13:管理crond定时任务
    service/systemd模块管理crond定时任务服务(重启、停止、开启)。

    # 先检查crond状态
    [root@m01 ~]# ansible oldboy -m shell -a "systemctl status crond"
    192.168.9.6 | SUCCESS | rc=0 >>
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since 三 2020-03-11 10:30:11 CST; 1 day 7h ago
    ---省略若干行---
    192.168.9.5 | SUCCESS | rc=0 >>
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since 六 2020-02-29 16:52:22 CST; 3 days ago
    ---省略若干行---
    [root@m01 ~]# ansible oldboy -m service -a "name=crond state=stopped enabled=no"
    说明:此处的name是服务名,表示将crond停止,并且取消开机自启动。
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "enabled": false, 
        "name": "crond", 
        "state": "stopped", 
        "status": {
    ---省略若干行---
        }
    }
    192.168.9.6 | SUCCESS => {
    ---省略若干行---
        }
    }
    [root@m01 ~]# ansible oldboy -m shell -a "systemctl status crond"
    192.168.9.6 | FAILED | rc=3 >>
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
    ---省略若干行---
    192.168.9.5 | FAILED | rc=3 >>
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
    ---省略若干行---
    [root@m01 ~]# ansible oldboy -m systemd -a "name=crond enabled=yes state=started"
    ---省略若干行---
    [root@m01 ~]# ansible oldboy -m shell -a "systemctl status crond"
    192.168.9.6 | SUCCESS | rc=0 >>
    ● crond.service - Command Scheduler
       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
       Active: active (running) since 四 2020-03-12 18:22:10 CST; 35s ago
    ---省略若干行---
    192.168.9.5 | SUCCESS | rc=0 >>
    ---省略若干行---
    

    提示:service模块对CentOS7依然有效,但是CentOS7的启动方式已经改成了systemd。

    4.2 cron模块功能说明

    功能说明:管理定时任务条目信息模块。

    cron模块功能参数1 cron模块功能参数2

    编写定时任务利用ansible软件,其实和直接编写定时任务文件的思路是一样的,只不过有些参数信息发生了变化。根据下表信息,表示定时任务传统编写和ansible软件编写对比说明,便于初学者更好地理解ansible的定时任务模块:系统定时任务编写格式与ansible定时任务对比表。

    系统定时任务与ansible定时任务对比

    示例14:设置时间同步定时任务

    [root@m01 ~]# ansible oldboy -m cron -a "name='oldboy' job='/usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1' minute=*/5"
    192.168.9.6 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "oldboy"
        ]
    }
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "oldboy"
        ]
    }
    # 检查验证
    [root@m01 ~]# ansible oldboy -m shell -a "crontab -l | tail -2"
    192.168.9.5 | SUCCESS | rc=0 >>
    #Ansible: oldboy
    */5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1
    192.168.9.6 | SUCCESS | rc=0 >>
    #Ansible: oldboy
    */5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1
    # 删除定时任务:
    ansible oldboy -m cron -a "name='oldboy' state=absent"
    

    4.3 mount模块功能说明

    功能说明:挂载及卸载文件系统(本地或者远程)

    mount模块功能参数

    示例15:批量挂载NFS到本地Web服务。

    # 配置所有主机NFS客户端环境。
    [root@m01 ~]# ansible oldgirl -m shell -a "yum install nfs-utils rpcbind -y warn=false"
    [root@m01 ~]# ansible oldgirl -m shell -a "systemctl start rpcbind"
    192.168.9.7 | SUCCESS | rc=0 >>
    192.168.9.9 | SUCCESS | rc=0 >>
    [root@m01 ~]# ansible oldgirl -m shell -a "systemctl status rpcbind"
    [root@m01 ~]# ansible oldgirl -m shell -a "showmount -e 192.168.9.6"
    192.168.9.9 | SUCCESS | rc=0 >>
    Export list for 192.168.9.6:
    /oldboy 192.168.9.0/24
    /data   192.168.9.0/24
    
    192.168.9.7 | SUCCESS | rc=0 >>
    Export list for 192.168.9.6:
    /oldboy 192.168.9.0/24
    /data   192.168.9.0/24
    # 检查NFS客户端环境。
    [root@m01 ~]# ansible oldgirl -m shell -a "grep data /etc/fstab"
    192.168.9.9 | SUCCESS | rc=0 >>
    192.168.9.6:/data       /data                   nfs     defaults,soft   0 0
    192.168.9.7 | SUCCESS | rc=0 >>
    192.168.9.6:/data   /data           nfs defaults,soft   0 0
    
    [root@m01 ~]# ansible oldgirl -m shell -a "df -h | grep data"
    192.168.9.9 | FAILED | rc=1 >>
    non-zero return code
    
    192.168.9.7 | SUCCESS | rc=0 >>
    192.168.9.6:/data         17G  1.3G   16G    8% /data
    # 执行挂载命令state=present,只挂载配置写入/etc/fstab。
    [root@m01 ~]# ansible oldgirl -m mount -a "src=192.168.9.6:/data path=/data fstype=nfs opts=defaults state=present"
    192.168.9.9 | SUCCESS => {
        "changed": true, 
        "dump": "0", 
        "fstab": "/etc/fstab", 
        "fstype": "nfs", 
        "name": "/data", 
        "opts": "defaults", 
        "passno": "0", 
        "src": "192.168.9.6:/data"
    }
    192.168.9.7 | SUCCESS => {
    ---省略若干行---
    }
    # 验证确实写入了/etc/fstab。
    [root@m01 ~]# ansible oldgirl -m shell -a "grep data /etc/fstab"
    192.168.9.9 | SUCCESS | rc=0 >>
    192.168.9.6:/data /data nfs defaults 0 0
    
    192.168.9.7 | SUCCESS | rc=0 >>
    192.168.9.6:/data /data nfs defaults 0 0
    # 验证但是没有实际挂载设备。
    [root@m01 ~]# ansible oldgirl -m shell -a "df -h | grep data"
    192.168.9.7 | SUCCESS | rc=0 >>
    192.168.9.6:/data         17G  1.3G   16G    8% /data
    
    192.168.9.9 | FAILED | rc=1 >>
    non-zero return code
    # 挂载设备,并写入/etc/fstab。
    [root@m01 ~]# ansible oldgirl -m mount -a "src=192.168.9.6:/data path=/data fstype=nfs opts=defaults state=mounted"
    192.168.9.7 | SUCCESS => {
        "changed": true, 
        "dump": "0", 
        "fstab": "/etc/fstab", 
        "fstype": "nfs", 
        "name": "/data", 
        "opts": "defaults", 
        "passno": "0", 
        "src": "192.168.9.6:/data"
    }
    192.168.9.9 | SUCCESS => {
    ---省略若干行---
    }
    # 验证确实真正挂载了
    [root@m01 ~]# ansible oldgirl -m shell -a "df -h | grep data"
    192.168.9.9 | SUCCESS | rc=0 >>
    192.168.9.6:/data         17G  1.3G   16G    8% /mnt
    192.168.9.7 | SUCCESS | rc=0 >>
    192.168.9.6:/data         17G  1.3G   16G    8% /data
    # 卸载设备。
    [root@m01 ~]# ansible oldgirl -m mount -a "src=192.168.9.6:/data path=/data fstype=nfs opts=defaults state=unmounted"
    192.168.9.9 | SUCCESS => {
        "changed": true, 
        "dump": "0", 
        "fstab": "/etc/fstab", 
        "fstype": "nfs", 
        "name": "/data", 
        "opts": "defaults", 
        "passno": "0", 
        "src": "192.168.9.6:/data"
    }
    192.168.9.7 | SUCCESS => {
    ---省略若干行---
    }
    # 验证确实下载了。
    [root@m01 ~]# ansible oldgirl -m shell -a "df -h | grep data"
    192.168.9.9 | FAILED | rc=1 >>
    non-zero return code
    192.168.9.7 | FAILED | rc=1 >>
    non-zero return code
    # 卸载设备并清理/etc/fstab。
    [root@m01 ~]# ansible oldgirl -m mount -a "src=192.168.9.6:/data path=/data fstype=nfs opts=defaults state=absent"
    192.168.9.9 | SUCCESS => {
        "changed": true, 
        "dump": "0", 
        "fstab": "/etc/fstab", 
        "fstype": "nfs", 
        "name": "/data", 
        "opts": "defaults", 
        "passno": "0", 
        "src": "192.168.9.6:/data"
    }
    192.168.9.7 | SUCCESS => {
    ---省略若干---
    }
    # 验证/etc/fstab是否清理,已经清理。
    [root@m01 ~]# ansible oldgirl -m shell -a "grep mnt /etc/fstab"
    192.168.9.9 | FAILED | rc=1 >>
    non-zero return code
    
    192.168.9.7 | FAILED | rc=1 >>
    non-zero return code
    

    5. Ansible用户和组类型模块介绍与实践

    5.1 user模块功能说明

    功能说明:管理系统用户。

    user模块功能参数

    示例16:创建test用户
    UID设置为8888,不能登录不创建家目录。

    [root@m01 ~]# ansible oldboy -m user -a "name=test uid=8888 shell=/sbin/nologin create_home=no"
    [root@m01 ~]# ansible oldboy -m shell -a "tail -1 /etc/passwd"
    

    5.2 group模块功能说明

    功能说明:管理系统用户。

    group模块功能参数
    [root@m01 ~]# ansible oldboy -m group -a "name=sa gid=9999"
    ---创建sa组,GID为9999
    192.168.9.5 | SUCCESS => {
        "changed": true, 
        "gid": 9999, 
        "name": "sa", 
        "state": "present", 
        "system": false
    }
    192.168.9.6 | SUCCESS => {
        "changed": true, 
        "gid": 9999, 
        "name": "sa", 
        "state": "present", 
        "system": false
    }
    [root@m01 ~]# ansible oldboy -m shell -a "tail -1 /etc/group"
    192.168.9.6 | SUCCESS | rc=0 >>
    sa:x:9999:
    
    192.168.9.5 | SUCCESS | rc=0 >>
    sa:x:9999:
    

    6. Ansible模块功能说明总结

    Ansible常用模块总结

    相关文章

      网友评论

          本文标题:2020-03-18 Ansible模块详解

          本文链接:https://www.haomeiwen.com/subject/eqpzehtx.html