Ansible 常用模块Group、User、File、Mount、Script(二)

Ansible获取模块

    [root@manager ~]# ansible-doc yum -s
    ansible-doc 模块名
    ansible-doc 模块名 -s 列出该模块的所有选项
    ansible-doc -l 列出所有模块

Group模块

    - gid    设置组id
    = name   需要管理的组名
    - state  执行状态,  absent   删除 
                        present  创建(默认)
例1:创建组名为www,并设置gid 666
    [root@manager ~]# ansible all -m group -a "name=www gid=666 "
    10.10.10.14 | SUCCESS => {
        "changed": true, 
        "gid": 666, 
        "name": "www", 
        "state": "present", 
        "system": false
    }
    10.10.10.12 | SUCCESS => {
        "changed": true, 
        "gid": 666, 
        "name": "www", 
        "state": "present", 
        "system": false
    }
    10.10.10.13 | SUCCESS => {
        "changed": true, 
        "gid": 666, 
        "name": "www", 
        "state": "present", 
        "system": false
    }
例2:修改www组的gid为888
    [root@manager ~]# ansible all -m group -a "name=www gid=888"
    10.10.10.14 | SUCCESS => {
        "changed": true, 
        "gid": 888, 
        "name": "www", 
        "state": "present", 
        "system": false
    }
    10.10.10.12 | SUCCESS => {
        "changed": true, 
        "gid": 888, 
        "name": "www", 
        "state": "present", 
        "system": false
    }
    10.10.10.13 | SUCCESS => {
        "changed": true, 
        "gid": 888, 
        "name": "www", 
        "state": "present", 
        "system": false
    }
例3:删除wwww组
    [root@manager ~]# ansible all -m group -a "name=www gid=888 state=absent"
    10.10.10.14 | SUCCESS => {
        "changed": true, 
        "name": "www", 
        "state": "absent"
    }
    10.10.10.12 | SUCCESS => {
        "changed": true, 
        "name": "www", 
        "state": "absent"
    }
    10.10.10.13 | SUCCESS => {
        "changed": true, 
        "name": "www", 
        "state": "absent"
    }

Uesr模块

= name         用户名
- uid          uid
- group        gid或groupname
- state        执行状态, absent 删除 
                         present  创建(默认)
- shell        登录shell,/bin/bash(默认),/sbin/nologin
- create_home  创建用户时,是否创建家目录,create_home=no
- password     用户密码,不能使用明文,需用使用openssl加密后的密码
例1:创建一个用户zqf,指定uid 60000,gid 666,并设置密码为123
    [root@manager ~]# ansible backup -m user -a "name=zqf uid=60000 group=666 password='123'"
    #此时你发现登录不上去。注意:password不能使用明文,需要加密,password的加密值需要用双引号
     #使用MD5进行加密 -stdin 非交互式
    [root@manager ~]# echo "123" |openssl passwd -1 -stdin
    $1$aQ8AU6.N$l4VDDfDxZtUhrLpCksekH1
    #注意单双引号 否则密码登录会错误
    [root@manager ~]# ansible backup -m user -a 'name=zqf uid=60000 group=666 password="$1$aQ8AU6.N$l4VDDfDxZtUhrLpCksekH1"'
    10.10.10.12 | SUCCESS => {
        "append": false, 
        "changed": true, 
        "comment": "", 
        "group": 666, 
        "home": "/home/zqf", 
        "move_home": false, 
        "name": "zqf", 
        "password": "NOT_LOGGING_PASSWORD", 
        "shell": "/bin/bash", 
        "state": "present", 
        "uid": 60000
    }
例2:创建一个程序用户www,指定uid 666,gid 666 不让登录 不创建家目录

[root@manager ~]# ansible all -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"

file模块

    = path        路径 copy模块的dest 其他模块的name 
    - src        源文件路径
    - owner        属主
    - group        属组
    - mode        权限
    - state        absent 删除 
                    directory 创建目录
                    file 修改文件属性(默认)
                    touch 创建文件
                    link hard 连接
    - recurse        递归
                    recurse=yes
例1:创建目录/datah和/backup,属主666,属组666,递归

[root@manager ~]# ansible all -m file -a "path=/data owner=666 group=666 recurse=yes state=directory"

例2:创建文件/etc/rsync.password,权限600

[root@manager ~]# ansible all -m file -a "path=/etc/rsync.password state=touch mode=600"

例3:对/etc/hosts做个软连接,到/tmp/hosts

[root@manager ~]# ansible all -m file -a "src=/etc/hosts path=/tmp/hosts state=link"

总结:file模块仅适合创建目录,修改所属和权限,创建连接,除开这些操作的其他文件管理都通过copy模块实现

mount模块

= path        挂载点
- src        需要挂载的设备
- fstype    挂载设备的文件系统
                    iso9660 光驱
                    ext4
                    xfs
                    nfs
                    cifs samba的共享文件系统
                    ntfs windows磁盘文件系统
- opts        挂载属性
                    noatime
                    noexec
                    nosuid
- state        挂载动作
                    present  #开机挂载,仅将挂载配置写入/etc/fstab
                    mounted  #挂载设备,并将配置写入/etc/fstab
                    unmounted #卸载设备,不会清楚/etc/fstab写入的配置
                    absent   #卸载设备,会清理/etc/fstab写入的配置
例1:通过nfs实现网络文件共享

1)安装nfs
[root@manager ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"
2)启动服务
[root@manager ~]# ansible nfs -m service -a "name=rpcbind state=started"
3)修改配置文件
方法一:在manager上创建配置文件,推给nfs(建议使用)
[root@manager ~]# mkdir -p /server/conf
[root@manager ~]# echo '/data 10.10.10.0/24(rw,sync,all_squash,anonid=666,anongid=666) > server/conf/exports
[root@manager ~]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/"
方法二:在manager上直接使用copy模块,推送文件内容
[root@manager ~]# ansible nfs -m copy -a "content='/data 10.10.10.0/24(rw,sync,all_squash,anonid=666,anongid=666)' dest=/etc/exports"
4)创建目录,用户、并修改所属
通过user group file模块来实现,参考上面的例子
5)重载配置文件
[root@manager ~]# ansible nfs -m service -a "name=nfs state=restarted"
6)在web01上挂载nfs的共享目录

[root@manager ~]# ansible web01 -m shell -a "showmount -e 10.10.10.13"
10.10.10.14 | SUCCESS | rc=0 >>
Export list for 10.10.10.13:
/data 10.10.10.0/24
[root@manager ~]# ansible web01 -m mount -a "src=10.10.10.13:/data path=/var/www/html fstype=nfs state=present"  #只是写入/etc/fstab文件并未挂载
[root@manager ~]# ansible web01 -m shell -a "df -h"
10.10.10.14 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.7G   15G  20% /
tmpfs           498M     0  498M   0% /dev/shm
/dev/sr0        3.7G  3.7G     0 100% /mnt/cdrom   
#并没有挂载成功,只是写入了/etc/fstab文件
[root@manager ~]# ansible web01 -m shell -a "tail -3 /etc/fstab"
10.10.10.14 | SUCCESS | rc=0 >>
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
10.10.10.13:/data /var/www/html nfs defaults 0 0
#卸载设备
[root@manager ~]# ansible web01 -m mount -a "src=10.10.10.13:/data path=/var/www/html fstype=nfs state=unmounted" 
#删除设备
[root@manager ~]# ansible web01 -m mount -a "src=10.10.10.13:/data path=/var/www/html fstype=nfs state=absent"
#挂载并写入/etc/fstab
[root@manager ~]# ansible web01 -m mount -a "src=10.10.10.13:/data path=/var/www/html fstype=nfs state=mounted"  

script模块

1)写个脚本,创建文件
[root@manager server]# cat 1.sh 
#!/bin/bash
touch /root/zqfbeautiful
2)运行脚本
[root@manager server]# ansible all -m script -a "/server/conf/1.sh"
posted @ 2020-08-17 17:11  梦想如花般盛开  阅读(238)  评论(0)    收藏  举报