zoukankan      html  css  js  c++  java
  • ansible批量部署模块(二)

    回顾:
    Ansible:
    无需客户端程序
    只要有SSH
    模块化

    ansible帮助工具
    ansible-doc 模块名
    ansible-doc 模块名 -s 列出该模块的所有选项
    ansible-doc -l 列出所有模块

    6.group组模块

    - gid
             设置组id
    = name
             需要管理的组名
    - state
             执行状态,absent 删除  
                      present 创建(默认)
    
    例1:创建组名www,并设置gid为666
    [root@m01 ~]# ansible oldboy -m group -a "name=www gid=666"
    
    例2:修改ww组的gid为888
    [root@m01 ~]# ansible oldboy -m group -a "name=www gid=888"
     
    例3:删除www组
    [root@m01 ~]# ansible oldboy -m group -a "name=www gid=888 state=absent"

    7.user用户模块

    = name
             用户名
    - uid
             uid
    - group
             gid或groupname
    - state
             执行状态,absent 删除  
                      present 创建(默认)
    - shell
             登录shell,/bin/bash(默认),/sbin/nologin
    - create_home
             创建用户时,是否创建家目录         
    - password
             用户密码,不能使用明文,需要使用openssl加密后的密码
    例1:创建一个用户oldboy,指定uid 6000,gid 666,并设置密码为123
    注意:password不能使用明文,需要加密,password的加密值需使用双引号
    # -1使用MD5进行加密 -stdin 非交互式
    [root@m01 ~]# echo "123" | openssl passwd -1 -stdin
    $1$ByoqasKh$lGSK41WgqAYJ/Ha9K3Yd00
    [root@m01 ~]# ansible oldboy -m user -a 'name=oldboy uid=6000 group=666 password="$1$ByoqasKh$lGSK41WgqAYJ/Ha9K3Yd00"'
    
    例2:创建一个程序用户www,指定uid666 gid666 不让登录 不创建家目录
    [root@m01 ~]# ansible oldboy -m user -a "name=www uid=666 group=www create_home=no shell=/sbin/nologin

    8.file模块

    = path
            目标文件路径
            copy模块的dest
            其他模块的name
    - src
            源文件路径
    - owner
            属主
    - group
            属组
    - mode
            权限
    - state
            absent 删除
            directory 创建目录
            file 修改文件属性(默认)
            touch  创建文件
            link hard 链接
    - recurse 
            递归
            recurse=yes
    例1:创建目录/data和/backup,属主666,属组666
    [root@m01 ~]# ansible oldboy -m file -a "path=/data owner=666 group=666 recurse=yes state=directory"
    [root@m01 ~]# ansible oldboy -m file -a "path=/backup owner=666 group=666 recurse=yes state=directory"
    
    例2:创建文件/etc/rsync.passwd,权限600
    [root@m01 ~]# ansible oldboy -m file -a "path=/etc/rsync.passwd mode=600 state=touch"
    
    例3:对/etc/hosts做个软链接到/tmp/hosts
    [root@m01 ~]#  ansible oldboy -m file -a "src=/etc/hosts path=/tmp/hosts state=link"
    
    总结:file模块仅适合创建目录,修改所属和权限,创建链接;除开这些操作的其他文件管理都通过copy模块实现

    9.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
    ansible nfs -m yum -a "name=nfs-utils state=installed"
    2)启动服务
    ansible nfs -m service -a "name=rpcbind state=started"
    ansible nfs -m service -a "name=nfs state=started" 
    3)修改配置文件
    方法一:在m01上创建配置文件,然后通过copy模块推送给nfs(建议使用)
    mkdir -p /server/conf
    echo '/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' > /server/conf/exports
    ansible nfs -m copy -a 'src=/server/conf/exports dest=/etc/'
    方法二:在m01上直接使用copy模块,推送文件内容
    ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' 
    4)创建目录用户,并修改所属
    ansible nfs -m group -a "name=www gid=666"
    ansible nfs -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin"
    ansible nfs -m file -a "path=/data state=directory owner=666 group=666 recurse=yes"
    5)重载配置文件
    ansible nfs -m shell -a "exportfs -arv" 或 ansible nfs -m service -a "name=nfs state=restarted"
    ansible nfs -m shell -a "showmount -e"
    6)在web上挂载nfs的共享目录
    ansible web -m yum -a "name=httpd state=installed"
    ansible web -m service -a "name=httpd state=started"
    ansible web -m mount -a "src=172.16.1.41:/data path=/var/www/html state=mounted fstype=nfs"
    ansible nfs -m copy -a "content='xiao ming is your father' dest=/data/index.html"

    10.script

    1).写个脚本,创建用户,并配置密码
    [root@m01 scripts]# cat 1.sh 
    #!/bin/bash
    useradd oldgirl
    echo 123 | passwd --stdin oldgirl &> /dev/null && echo 'create passwd success'
    2).运行脚本
    ansible all -m script -a "/server/scripts/1.sh"

    11.cron

    - name
            描述,必须要写,如果不写,默认为None,会导致无法指定删除某条计划任务
    - job
            任务,命令
    - state
            执行状态,absent 删除  
                     present 创建(默认)
    - minute
            Minute when the job should run ( 0-59, *, */2, etc )
            [Default: *]
    - hour
            Hour when the job should run ( 0-23, *, */2, etc )
            [Default: *]
    - day
            Day of the month the job should run ( 1-31, *, */2, etc )
            (Aliases: dom)[Default: *]
    - weekday
            Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc
            )(Aliases: dow)[Default: *]
    - month
            Month of the year the job should run ( 1-12, *, */2, etc )
            [Default: *]
    例子:每天的凌晨1点执行rsync_backup.sh
    [root@m01 ~]# ansible all -m cron -a "name='Rsync backup' hour=1 minute=0 job='/bin/sh /server/scripts/rsync_backup.sh &> /dev/null'"
  • 相关阅读:
    题解 P2296 【寻找道路】
    题解 CF534C 【Polycarpus' Dice】
    题解 CF294B 【Shaass and Bookshelf】
    题解 SP4354 【TWINSNOW Snowflakes】
    题解 UVA10294 【Arif in Dhaka (First Love Part 2)】
    FLV文件格式解析部分代码
    关于“无法定位程序输入点getaddrinfo于动态链接库WS32_32.dll上”的问题
    MinGW介绍与使用
    FFMPEG: 0.4.9
    可以处理UTF8编码的md5函数
  • 原文地址:https://www.cnblogs.com/xmtxh/p/12233678.html
Copyright © 2011-2022 走看看