美文网首页
ansible批量管理服务

ansible批量管理服务

作者: 酷酷的伟 | 来源:发表于2019-05-29 00:39 被阅读0次

    ansible批量管理

    ansible优势

    1.ansible无需单独安装客户端,也不需要启动任何服务
    2.ansible是python中的一套完整的自动化执行任务模块
    3.ansible playbook(剧本),采用yaml配置,对于自动化任务执行一目了然
    4.ansible模块较多,对于自动化的场景比较丰富

    image.png

    管理机安装ansible

    yum install -y ansible
    

    1、查看ansible下的配置文件

    [ root@m01 ~]# rpm -ql ansible|grep -v /usr/
    /etc/ansible
    /etc/ansible/ansible.cfg
    /etc/ansible/hosts
    /etc/ansible/roles
    

    2、编辑主机配置清单

    vim /etc/ansible/hosts
    最后一行
    创建一个主机清单,名称为oldboy
    [oldboy]
    172.16.1.31
    172.16.1.41
    172.16.1.7
    

    3、使用ping检查

    ansible oldboy -m ping
    oldboy ---是主机清单的名称
    -m ---表示使用什么模块

    [root@m01 ~]# ansible oldboy -m ping
    172.16.1.41 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"      \\pong表示通了
    }
    172.16.1.31 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"      \\pong表示通了
    }
    172.16.1.7 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"      \\pong表示通了
    }
    

    ansible模块

    格式:ansible +主机清单+指定模块的参数+模块名称+执行命令的参数+命令
    ansible oldboy -m command hostname -I
    -m ---指定模块的参数
    -a ---模块中的命令或参数

    命令行中查询模块的参数

    ansible-doc -s +模块名称

    官网查询模块的应用

    https://docs.ansible.com
    网站中主要的单词
    src source源
    dest 目标
    content 文件内容
    state 状态

    1、command模块

    执行命令的模块
    默认的模块:只能执行简单的命令。不支持特殊符号

    2、shell模块

    万能模块 支持特殊符号及正则表达式
    可以执行命名或脚本

    3、copy模块

    推送文件的模块
    src= 源 推送数据的全路径
    dest= 目标 推送数据的目标路径
    owner= 指定推送文件的所有者信息
    group= 指定推送文件的用户组信息
    mode= 指定推送文件的权限信息
    backup= 对传送过去的数据进行备份
    content= 批量在服务端文件内添加内容 先清空再增加 与src二选一
    force=yes 强制覆盖 (默认为yes)
    force=no 对端不存在该文件时才覆盖

    4、script模块

    先把脚本传送到远端再执行

    5、yum模块

    安装模块
    name 指定要安装的软件包名称
    state 要执行的yum动作 installed&present 安装软件包 lastet更新软件包

    6、file模块

    文件模块
    path 指定远程主机目录或文件信息
    recurse 递归授权
    state directory 在远端创建目录
    state touch 在远端创建文件
    state absent 删除文件或目录
    state link&hard 创建软连接或硬链接文件
    mode 设置文件或目录权限
    owner 设置文件或目录属主信息
    group 设置文件或目录属组信息

    6、service模块

    开启 关闭 重启服务模块
    name 指定服务名称
    state started 启动服务
    state stoped 停止服务
    state restarte 重启服务
    state reload 平滑重启
    enabled:yes 让服务开机自启 no 默认disable

    7、group模块

    组模块
    name 指定创建的组名
    gid 指定组的gid
    state absent 移除远端主机的组
    state present 创建远端的组

    8、user模块

    用户模块
    name 创建的用户名
    uid 指定创建用户的uid
    gid 指定创建用户的gid
    group 指定用户组名称
    groups 指定附加组名称
    password 给用户添加密码
    shell 指定用户登录shell
    create_home 是否创建家目录

    9、mount模块

    挂载模块
    src 目标路径
    path 挂载路径
    fstype 挂载类型
    opts 挂载方式
    state present 仅将挂载信息写入fstab
    state mounted 挂载并将信息写入fstab
    state umount 取消挂载
    state absent 取消挂载并清除fstab内的挂载

    10、cron模块

    定时任务模块
    minute=* hoour=* day=* month=* weekday=*
    job 要执行的动作
    minute=* 分
    hour=* 时
    day=* 日
    month=* 月
    weekday=* 周

    ansible各模块使用案例

    模块案例

    推送 ansible all -m copy -a 'src=/etc/hostname dest=/tmp/'
    查看 ansible all -a 'cat /tmp/hostname'

    copy模块

    推送文件模块

    [root@m01 ~]# ansible all -m copy -a 'src=/etc/hostname dest=/tmp/'
    172.16.1.31 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum": "f434396716e2c9aed47cfde87c491cce5a2c08fa", 
        "dest": "/tmp/hostname", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "318d7defb693a2eb0d4f1a7a96575a57", 
        "mode": "0644", 
        "owner": "root", 
        "size": 4, 
        "src": "/root/.ansible/tmp/ansible-tmp-1559017854.64-224769717508792/source", 
        "state": "file", 
        "uid": 0
    }
    ...省略
    
    [root@m01 ~]# ansible all -a 'cat /tmp/hostname'
    172.16.1.31 | CHANGED | rc=0 >>
    m01
    
    172.16.1.41 | CHANGED | rc=0 >>
    m01
    
    172.16.1.7 | CHANGED | rc=0 >>
    m01
    
    [root@m01 ~]# ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hostname backup=yes' 
    172.16.1.31 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "backup_file": "/tmp/hostname.8691.2019-05-28@12:41:13~", 
        "changed": true, 
        "checksum": "d2c63329a65fa8c2a390e468cf037e28e6796f0f", 
        "dest": "/tmp/hostname", 
        "gid": 0, 
    

    script 模块

    [root@m01 ~]# #ansible all  -m script  -a "/server/scripts/yum.sh"
    [root@m01 ~]# ansible all -a 'rpm -qa ipvsadm'
     [WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because
    yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
    ansible.cfg to get rid of this message.
    
    172.16.1.41 | CHANGED | rc=0 >>
    ipvsadm-1.27-7.el7.x86_64
    
    172.16.1.7 | CHANGED | rc=0 >>
    ipvsadm-1.27-7.el7.x86_64
    

    yum模块

    ansible all   -m yum  -a 'name=sl state=present'
    

    使用yum模块的格式为:‘name=命令 state=present或者install(表示动作)’

    file模块

    [root@m01 ~]# #ansible all -m file  -a 'path=/tmp/a/b/c/d/e/f/g   state=directory '
    [root@m01 ~]# #ansible all -m file  -a 'path=/tmp/a/b/c/d/e/f/g/oldboy.txt   state=touch '
    [root@m01 ~]# ansible all  -a 'tree  /tmp/ '
    

    user模块

    caiav 创建用户指定uid和gid 1111,不创建家目录也不允许登陆

    groupadd -g 1111  caiav 
    useradd -u 1111 -g caiav    -s /sbin/nologin  -M  caiav 
    
    ansible all -m group  -a 'name=caiav gid=1111 state=present'
    ansible all -m user  -a  'name=caiav uid=1111 group=caiav  shell=/sbin/nologin create_home=no '
    

    cron模块

    [root@m01 /etc/ansible]# vim cron.yml 
    ---
      - hosts: all
        tasks:
        - name: chuangjian dingshirenwu
          cron:
            name: sync time V2
            minute: "*/10"
            job: /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
            state: present
    

    - hosts: all ---指定主机清单
    注:开头缩进两个空格

    tasks: ---指定工作内容
    注:比- hosts还缩进两个空格

    - name: ---指定要做的事儿的名字
    注:比tasks还缩进两个空格

    cron: ---模块名称
    注:比- name还缩进两个空格

    命令行执行定时任务

    1.检查是否安装ntpdate

    [09:32 root@m01 ~]# ansible all -a 'rpm -qa ntpdate'
    

    2.每5分钟同步一次时间:

    [09:34 root@m01 ~]# ansible all -m cron  -a 'name="sync time" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
    

    3.再添加一个定时任务

    [09:55 root@m01 ~]# ansible all -m cron  -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
    

    4.把此定时任务删除:

    [09:55 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" state=absent'
    

    查看一下

    [09:56 root@m01 ~]# ansible all -a 'crontab -l'
    

    5.再添加一个定时任务

    [09:57 root@m01 ~]# ansible all -m cron  -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
    

    6.给定时任务添加注释:

    [10:00 root@m01 ~]# ansible all -m cron  -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=yes'
    

    查看一下

    [10:00 root@m01 ~]# ansible all -a 'crontab -l'
    172.16.1.31 | CHANGED | rc=0 >>
    #Ansible: guoav-date
    #*/30 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
    

    7.取消注释:

    [10:02 root@m01 ~]# ansible all -m cron  -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=no'
    

    查看一下

    [10:02 root@m01 ~]# ansible all -a 'crontab -l'
    172.16.1.7 | CHANGED | rc=0 >>
    #Ansible: guoav-date
    */30 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
    

    mount挂载模块

    1.首先保证nfs01客户端的nfs服务开启
    [10:12 root@nfs01 ~]# systemctl is-active rpcbind
    active
    [10:13 root@nfs01 ~]# systemctl is-active nfs
    active
    
    2.将/nfs共享目录挂载到web01服务端上
    [10:09 root@web01 ~]# showmount -e 172.16.1.31
    Export list for 172.16.1.31:
    /nfs    172.16.1.0/24
    /upload 172.16.1.0/24
    [10:09 root@web01 ~]# mount -t nfs 172.16.1.31:/nfs/ /mnt/
    [10:09 root@web01 ~]# df -h
    Filesystem        Size  Used Avail Use% Mounted on
    /dev/sda3          19G  1.7G   18G   9% /
    devtmpfs          476M     0  476M   0% /dev
    tmpfs             487M     0  487M   0% /dev/shm
    tmpfs             487M  7.7M  479M   2% /run
    tmpfs             487M     0  487M   0% /sys/fs/cgroup
    /dev/sda1         197M  105M   93M  54% /boot
    tmpfs              98M     0   98M   0% /run/user/0
    172.16.1.31:/nfs   19G  1.7G   18G   9% /mnt
    
    测试完把/mnt卸载掉:
    umount /mnt
    
    3.用ansible将/nfs挂载到web01上的/upload

    1.查看挂载信息
    2.挂载 state=mounted
    3.查看

    [10:27 root@m01 ~]# ansible 172.16.1.7 -a 'showmount -e 172.16.1.31'
    172.16.1.7 | CHANGED | rc=0 >>
    Export list for 172.16.1.31:
    /nfs    172.16.1.0/24
    /upload 172.16.1.0/24
    
    [10:26 root@m01 ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=mounted'
    172.16.1.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dump": "0", 
        "fstab": "/etc/fstab", 
        "fstype": "nfs", 
        "name": "/upload", 
        "opts": "defaults", 
        "passno": "0", 
        "src": "172.16.1.31:/nfs"
    }
    
    [10:26 root@m01 ~]# ansible 172.16.1.7 -a 'df -h'
    172.16.1.7 | CHANGED | rc=0 >>
    Filesystem        Size  Used Avail Use% Mounted on
    ....
    172.16.1.31:/nfs   19G  1.7G   18G   9% /upload
    
    4.不挂载,只添加到fstab文件

    state=present

    [10:46 root@m01 ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/date path=/tmp state=present'
    
    5.卸载,会删除/etc/fstab

    state=absent

    [11:04 root@m01 ~]# ansible 172.16.1.7 -m mount -a ' src=172.16.1.31:/nfs path=/upload state=absent'
    

    查看命令行的返回值是否执行成功

    asnible的变量

    一、实现变量调用的方法:

    1.在nfs01 backup 创建/backup/lidao的目录 ,把 /etc/目录打包压缩到/backup/lidao目录下面 etc.tar.gz ,目录名存放在变量中

    image
    [09:55 root@m01 /etc/ansible]# vim vars.yml 
    ---
      - hosts: all
        vars:
          dir1: /backup/lidao
        tasks:
        - name: mkdir
          file:
            path: "{{ dir1 }}"
            state: directory
        - name: tar /etc
          archive:
            path: /etc
            dest: "{{ dir1 }}/etc.tar.gz"
    
    
    image image

    二、变量注册

    1.注册变量信息

    [11:31 root@m01 /etc/ansible]# vim vars1.yml 
    ---
      - hosts: all
        tasks:
          - name: ip
            shell: hostname -I|awk '{print $NF}'
            register: ipaddr
          - name: print ip
            shell: echo {{ ipaddr.stdout }} >>/tmp/ip.txt
    
    

    2.追加到文件中

    image

    3.查看追加过去的内容:

    最后的一行是stdout_lines: [u172.16.1.31] 是我们要的内容

    image

    4.ansible的调试

    需要用到叫register的内容
    把/etc/打包备份到/backup/ip地址命名目录/etc-时间.tar.gz
    (因为时间关系这里就打包/etc下的profile和hosts了)
    ip是每台机器的内网ip
    时间是当天的日期:年-月-日

    image
    [12:10 root@m01 /etc/ansible]# vim vars.yml 
    ---
      - hosts: all
        tasks:
        - name: ip
          shell: hostname -I|awk '{print $NF}'
          register: ipaddr
        - name: date
          shell: date +%F
          register: time
    
        - name: mkdir dir
          file:
            path: /backup/{{ ipaddr.stdout }}
            state: directory
        - name: tar /etc/profie && /etc/hosts
          archive:
            path: /etc
            dest: /backup/{{ ipaddr.stdout }}/etc-{{ time.stdout }}.tar.gz
        - name: mkdir xf-dir
          file:
            path: /backup/{{ ipaddr.stdout }}/tc-{{ time.stdout }}
            state: directory
        - name: tar xf
          unarchive:
            src: /backup/{{ ipaddr.stdout }}/etc-{{ time.stdout }}.tar.gz
            copy: no
            dest: /backup/{{ ipaddr.stdout }}/
    
    

    三、内置变量

    image
    查看主机名:
    [12:35 root@m01 /etc/ansible]# vim vars2.yml
    ---
      - hosts: all
        tasks:
          - name: 内置变量
            debug: msg={{ ansible_hostname }}
    
    

    四、查看所有内置变量

    用ansible_facts 内容太多可以筛选

    [12:35 root@m01 /etc/ansible]# vim vars2.yml
    ---
      - hosts: all
        tasks:
          - name: 内置变量
            debug: msg={{ ansible_facts }}
    
    

    循环的使用

    ---
    - hosts: all
      remote_user: root
      tasks:
        - name: Installed Pkg
          yum: name={{ item }} state=present
          with_items:
            - wget
            - tree
            - lrzsz
    
    

    判断的使用

    [root@m01 ~]# vim 05.var.yaml
    ---
       - hosts: all
         tasks:
           - name: yum
             yum:
              name: tree
              state: installed
             when: (ansible_hostname == 'nfs01')
    
    

    相关文章

      网友评论

          本文标题:ansible批量管理服务

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