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

ansible-批量管理服务

作者: linux_龍 | 来源:发表于2019-07-30 22:49 被阅读0次

    1.ansible批量管理服务概述

    实现批量管理 --- 并行管理 ---部署简单方便/应用简单方便

    2.ansible批量管理服务安装

    yum repolist --查看是否有对应的epel源
    安装ansible 软件
    yum install -y ansible

    3.absible服务软件配置应用

    架构组成部分:
    host inventory:主机清单配置信息
    core modules:实现批量管理功能 命令信息 参数
    playbooks :剧本实现自动化功能 脚本文件

    4.ansible主机清单配置方法

    官方资料 :https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

    /etc/ansible
    /etc/ansible/ansible.cfg     --- ansible配置文件 
    /etc/ansible/hosts           --- 主机清单文件 /etc/ansible/roles           --- 角色目录(更加规范使用ansible)
    
    vim /etc/ansible/hosts       --- 定义管理的主机信息
    172.16.1.31
    测试是否可以管理31存储服务器
    ansible命令格式:
    ansible 管理主机信息  -m  模块名称 -a "操作动作" 
          01       02         03   04      05    06
    
    主机清单配置方法一:
     在文件中直接编写远程主机地址信息:
     vim /etc/ansible/hosts 
    172.16.1.31 
    172.16.1.41 
    
    主机清单配置方法二:
    编写服务内置变量信息
    ansible_port=xxx      --- 指定远程服务端口号
    ansible_password=xxx  --- 指定远程服务密码信息
    ansible_user=xxx      --- 指定以什么用户远程连接主机
    ansible_host=xxx      --- 指定远程主机地址信息 和 名称做映射
    vim /etc/ansible/hosts 
    nfs01 ansible_host=172.16.1.31
    172.16.1.41 ansible_user=root ansible_password=123456
    172.16.1.7  ansible_user=root ansible_password=654321 ansible_port=52113  
    
    主机清单配置方法三: 
    编写服务主机组信息
    vim /etc/ansible/hosts 
    [oldboy]
    nfs01 ansible_host=172.16.1.31
    172.16.1.41 ansible_user=root ansible_password=123456
    nfs01 ansible_host=172.16.1.31
    172.16.1.7  ansible_user=root ansible_password=654321 ansible_port=52113  
    
    主机清单配置方法四: 
    编写服务主机子组信息
    vim /etc/ansible/hosts
    [nfs:children]
    nfs_server
    nfs_client
    [nfs_server]
    nfs01 ansible_host=172.16.1.31
    [nfs_client]
    172.16.1.41 ansible_user=root
    ansible_password=123456
    172.16.1.7  ansible_user=root
    ansible_password=654321 ansible_port=52113
    
    主机清单配置方法五:  剧本 
    配置主机清单变量信息
    vim /etc/ansible/hosts
    [nfs:vars]
    pass=123456
    port=873
    [nfs]
    172.16.1.31 
    172.16.1.41
        
    [nfs_client]
    172.16.1.41
    [nfs_client:vars]
    ansible_user=root
    ansible_password=123456
    
    #5.absible服务模块应用方法
    第一个模块:commad(默认模块)
    command -Execute commands on targets
    在目标主机执行命令
    synopsis 模块详细说明
    01.模块多个参数要用空格分隔
    02.使用commad模块一些特殊符号信息不能使用,如果非要执行请使用shell模块
    parameters模块的参数
    chdir -- 在执行命令前,先要切换指定目录(远程主机)
    creates ---  在执行命令前,判断远程主机指定文件是否存在,如果存在,命令不执行 
    参数演示过程: 
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf touch /etc/rsyncd.conf"
     172.16.1.41 | SUCCESS | rc=0 >>
     skipped, since /etc/rsyncd.conf exists
     
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsync.conf touch /etc/rsync.conf"
    [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file 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 >>
    removes    --- 在执行命令前,判断远程主机指定文件是否存在,如果存在,命令会继续执行
    free_form  --- 在使用这个模块是, -a后面必须输入一个合法linux命令
        
        
    一个模块哪些是重要参数:
    01. 通过其他人博文
    02. 看官网网站文档中,有红色字体标记的
    03. 看官网网站文档中,举例配置中参数使用频次
    
    第二个模块: shell (万能模块)
    shell – Execute shell commands on targets
    在远程目录主机执行命令
    Parameters 模块的参数
    chdir   ---  在运行命令之前,先切换指定目录(远程主机)
    creates ---  在执行命令前,判断远程主机指定文件是否存在,如果存在,命令不执行 
    参数演示过程: 
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf touch /etc/rsyncd.conf"
    172.16.1.41 | SUCCESS | rc=0 >>
    skipped, since /etc/rsyncd.conf exists
        
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsync.conf touch /etc/rsync.conf"
     [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file 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 >>
    removes  --- 在执行命令前,判断远程主机指定文件是否存在,如果存在,命令会继续执行     
    
    第三个模块: script (脚本模块)
    script - 远程批量执行脚本信息
        
    shell 远程执行脚本:
    第一步: 编写脚本 
    vim yum.sh 
    #!/bin/bash
    yum install -y htop
    第二步: 传输脚本文件
    ansible 172.16.1.41 -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts"
    第三步: 批量执行脚本
    ansible 172.16.1.41 -m shell -a "sh /server/scripts/yum.sh"
        
    script 远程执行脚本:
    第一步: 编写脚本 
    vim yum.sh 
    #!/bin/bash
    yum install -y htop 
    第二步: 远程执行脚本 
    ansible 172.16.1.41 -m script -a "/server/scripts/yum.sh"
    
    第四个模块: copy  推送数据操作
    copy - 可以将本地数据批量拷贝到远程主机
    可以将远程主机数据进行移动操作    /tmp  /opt
    
    参数:
    src  - 指定本地要推送的源数据信息
    dest - 指定保存数据目录路径信息
    mode - 数据推送后修改数据权限 
    owner- 修改数据推送后的所属用户信息
    group- 修改数据推送后的所属组信息
    remote_src - 指定源为远程主机路径信息
    backup     - 将数据进行备份
    content    - 在指定远程主机生成有数据的文件
        
        
    ansible 172.16.1.41 -m copy -a "src=/oldboy/oldboy.txt dest=/backup mode=666 owner=oldboy group=oldboy"
    ansible 172.16.1.41 -m copy -a "src=/oldboy/ dest=/backup mode=666 owner=oldboy group=oldboy"
    说明: 复制目录是遵循rsync复制目录原理 目录后面有/ 和 没有/有区别
     ansible nfs -m copy -a "src=/oldboy/oldboy.txt dest=/backup backup=yes"
    实现数据备份 了解
    ansible nfs -m copy -a "src=/backup/oldboy.txt
    dest=/backup/oldboy.txt.bak remote_src=yes" -- 批量备份
    ansible nfs -m copy -a "src=/backup/oldboy.txt.bak dest=/backup/oldboy.txt remote_src=yes" -- 批量还原
    ansible nfs_client -m copy -a "content="oldboy123" dest=/etc/rsync.password mode=600"
    

    补充: 通过ansible命令执行结果颜色
    黄色: 对远程主机做出了改动
    绿色: 查询信息/对远程主机没有做改动
    红色: 远程执行命令出错了
    紫色: 警告信息(建议信息)
    蓝色: 命令执行的过程

    5.主机清单配置5种方法(主机信息 主机组信息 主机内置变量 主机子组 主机变量)

    配置文件信息
    become=True --- 开启用户切换功能
    become_method=sudo --- 使用什么方式切换用户权限 su / sudo
    become_user=root --- 默认切换成什么用户
    become_ask_pass=false --- 是否使用密码认证
    主机清单中的配置

    主机清单中配置:
     ansible_become
    Equivalent to ansible_sudo or ansible_su, allows to force privilege escalation
    开启用户sudo或su功能
    
    ansible_become_method
    Allows to set privilege escalation method
    选择获取权限的方式  su / sudo
    
    ansible_become_user
    Equivalent to ansible_sudo_user or ansible_su_user, allows to set the user you become through privilege escalation
    指定切换成什么用户操作
    
    ansible_become_password
    Equivalent to ansible_sudo_password or ansible_su_password, allows you to set the privilege escalation password
    (never store this variable in plain text; always use a vault. See Variables and Vaults)
    实现设置su或者sudo密码信息
    

    相关文章

      网友评论

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

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