ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Ansible批量管理服务(常用模块的用法)

2021-07-06 16:04:47  阅读:187  来源: 互联网

标签:oldboy 批量 ansible --- Ansible 模块 172.16 1.110


01. ansible批量管理服务介绍
  ansible批量管理服务意义
  01. 提高工作的效率
  02. 提高工作准确度
  03. 减少维护的成本
  04. 减少重复性工作
  ansible批量管理服务功能
  01. 可以实现批量系统操作配置
  02. 可以实现批量软件服务部署
  03. 可以实现批量文件数据分发
  04. 可以实现批量系统信息收集

02. ansible批量管理服务部署
  管理端服务器
  第一个历程: 安装部署软件
  yum install -y ansible --- 需要依赖epel的yum源
  /etc/ansible/ansible.cfg --- ansible服务配置文件
  /etc/ansible/hosts --- 主机清单文件 定义可以管理的主机信息
  /etc/ansible/roles --- 角色目录

  第二个历程: 需要编写主机清单文件
  vim /etc/ansible/hosts
  #可以管理的主机列表
  10.0.0.100
  10.0.0.110

  第三个历程: 测试是否可以管理多个主机(基于秘钥的连接)
  脚本 hostname
  ansible all -a "hostname"

03. ansible软件模块应用
  中文版:https://ansible-tran.readthedocs.io/en/latest/
  ansible官方网站: https://docs.ansible.com/
  模块的应用语法格式:
  ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "执行什么动作"

命令类型模块:
掌握第一个模块: command (默认模块)

command – Executes a command on a remote node
在一个远程主机上执行一个命令
简单用法:
[root@m01 scripts]# ansible 172.16.1.110 -m command -a "hostname"

扩展应用:
1) chdir Change into this directory before running the command.
在执行命令之前对目录进行切换
ansible 172.16.1.110 -m command -a "chdir=/tmp touch oldboy.txt"

2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
ansible 172.16.1.110 -m command -a "creates=/tmp/hosts touch oldboy.txt"

3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
ansible 172.16.1.110 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"

使用command模块的时候,-a参数后面必须写上一个合法linux命令信息
注意事项:
有些符号信息无法识别: <", ">", "|", ";" and "&"

掌握第二个模块: shell (万能模块)

    shell   – Execute commands in nodes
              在节点上执行操作
    简单用法:和command用法一样
    [root@m01 scripts]# ansible 172.16.1.110 -m shell -a "hostname"
    扩展应用:
    1) chdir      Change into this directory before running the command.
              在执行命令之前对目录进行切换
       ansible 172.16.1.110 -m shell -a "chdir=/tmp touch oldboy.txt"
    2) creates    If it already exists, this step won't be run.
                如果文件存在了,不执行命令操作
       ansible 172.16.1.110 -m shell -a "creates=/tmp/hosts touch oldboy.txt"      
    3) removes    If it already exists, this step will be run.
                如果文件存在了,    这个步骤将执行   
 ansible 172.16.1.31 -m shell -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
    实践应用: 利用shell执行脚本  
    第一个步骤: 编写一个脚本
    第二个步骤: 将脚本发送到远程主机
    第三个步骤: 将脚本权限进行修改(添加执行权限)
    第四个步骤: 运行ansible命令执行脚本
    ansible 172.16.1.110 -m shell -a "/server/scripts/yum.sh" 在.110执行被管理端的脚本

  掌握第三个模块: script (万能模块)

    第一个步骤: 编写一个脚本
    第二个步骤: 运行ansible命令执行脚本
    ansible 172.16.1.110 -m script -a "/server/scripts/yum.sh" 在.110上执行管理端的脚本
    PS: scripts模块参数功能和command模块类似

文件类型模块 copy

    copy – Copies files to remote locations
           将数据信息进行批量分发    
    扩展用法:
    01. 在传输文件时修改文件的属主和属组信息
    ansible 172.16.1.110 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
    02. 在传输文件时修改文件的权限信息
    ansible 172.16.1.110 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"

    03. 在传输数据文件信息时对远程主机源文件进行备份 
    ansible 172.16.1.110 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
    04. 创建一个文件并直接编辑文件的信息
    ansible 172.16.1.110 -m copy -a "content='oldboy123' dest=/etc/rsync.password"

文件类型模块 file

    基本用法:
    ansible 172.16.1.110 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"    
    
    扩展用法:
    1. 可以利用模块创建数据信息 (文件 目录 链接文件)
    state  参数
    =absent    --- 缺席/删除数据信息
    =directory --- 创建一个目录信息
    =file      --- 检查创建的数据信息是否存在 绿色存在 红色不存在
    =hard      --- 创建一个硬链接文件
    =link      --- 创建一个软链接文件
    =touch     --- 创建一个文件信息
    
    创建目录信息:
    ansible 172.16.1.110 -m file -a "dest=/oldboy/ state=directory"
    ansible 172.16.1.110 -m file -a "dest=/oldboy/oldboy01/oldboy02/ state=directory"
    创建文件信息:
    ansible 172.16.1.110 -m file -a "dest=/oldboy/oldboy.txt state=touch"
    创建链接文件信息:
    ansible 172.16.1.110 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_hard.txt state=hard"
    ansible 172.16.1.110 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_link.txt state=link"

    2. 可以利用模块删除数据信息
    ansible 172.16.1.110 -m file -a "dest=/oldboy/oldboy.txt state=absent"
    ansible 172.16.1.110 -m file -a "dest=/oldboy/  state=absent"

    自行研究: recurse=yes    相当于 chmod  -R  chown -R 递归授权

补充模块

yum模块
    name  --- 指定安装软件名称
    state --- 指定是否安装软件
              installed   --- 安装软件
              present
              latest
              absent      --- 卸载软件
              removed
    ansible 172.16.1.110 -m yum -a "name=iotop state=installed"    
    
service模块: 管理服务器的运行状态 停止 开启 重启 name: --- 指定管理的服务名称 state: --- 指定服务状态 started 启动 restarted 重启 stopped 停止 enabled --- 指定服务是否开机自启动 ansible 172.16.1.110 -m service -a "name=nfs state=started enabled=yes"
cron模块: 批量设置多个主机的定时任务信息 crontab -e * * * * * 定时任务动作 分 时 日 月 周 minute: # Minute when the job should run ( 0-59, *, */2, etc ) 设置分钟信息 hour: ansi # Hour when the job should run ( 0-23, *, */2, etc ) 设置小时信息 day: # Day of the month the job should run ( 1-31, *, */2, etc ) 设置日期信息 month: # Month of the year the job should run ( 1-12, *, */2, etc ) 设置月份信息 weekday: # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc ) 设置周信息 job 用于定义定时任务需要干的事情 基本用法: ansible 172.16.1.110 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 扩展用法: 01. 给定时任务设置注释信息 ansible 172.16.1.110 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 02. 如何删除指定定时任务 ansible 172.16.1.110 -m cron -a "name='time sync01' state=absent" PS: ansible可以删除的定时任务,只能是ansible设置好的定时任务 03. 如何批量注释定时任务 ansible 172.16.1.110 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes" mount模块: 批量进行挂载操作 src: 需要挂载的存储设备或文件信息 path: 指定目标挂载点目录 fstype: 指定挂载时的文件系统类型 state present/mounted --- 进行挂载 present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载 mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载 ***** ansible 172.16.1.110 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted" absent/unmounted --- 进行卸载 absent: 会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载 unmounted: 会实现立即卸载, 但是不会会删除fstab文件信息 *****
user模块: 实现批量创建用户 基本用法: ansible 172.16.1.110 -m user -a "name=oldboy01" 扩展用法: 1) 指定用户uid信息 ansible 172.16.1.110 -m user -a "name=oldboy02 uid=6666" 2) 指定用户组信息 ansible 172.16.1.110 -m user -a "name=oldboy03 group=oldboy02" ansible 172.16.1.110 -m user -a "name=oldboy04 groups=oldboy02" 3) 批量创建虚拟用户 ansible 172.16.1.110 -m user -a "name=rsync create_home=no shell=/sbin/nologin" 4) 给指定用户创建密码 PS: 利用ansible程序user模块设置用户密码信息,需要将密码明文信息转换为密文信息进行设置 生成密文密码信息方法: 方法一: ansible all -i localhost, -m debug -a "msg={{ '密码信息123456' | password_hash('sha512', 'oldboy') }}" [root@m01 tmp]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy') }}" localhost | SUCCESS => { "msg": "$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1" }

补充说明: ansible软件输出颜色说明:
  01. 绿色信息: 查看主机信息/对主机未做改动
  02. 黄色信息: 对主机数据信息做了修改
  03. 红色信息: 命令执行出错了
  04. 粉色信息: 忠告信息
  05. 蓝色信息: 显示ansible命令执行的过程

补充: ansible学习帮助手册如何查看
  ansible-doc -l --- 列出模块使用简介
  ansible-doc -s fetch --- 指定一个模块详细说明
  ansible-doc fetch --- 查询模块在剧本中应用方法

知识回顾

  command 模块: 在远程主机上执行命令操作 默认模块
  shell 模块: 在远程主机上执行命令操作 万能模块
  PS: 有时剧本不能反复执行!!!
  script 模块: 批量执行本地脚本
  copy 模块: 用于批量分发传输数据信息 ()管理端 ---> 多个被管理端)
  fetch 模块: 用于将远程主机数据进行拉取到本地管理主机 ()管理端 <--- 多个被管理端)

   ansible 172.16.1.110 -m fetch -a "src=/tmp/oldboy.txt dest=/tmp"    和copy相反

  file 模块: 修改数据属性信息/创建数据信息
  yum 模块: 用于安装和卸载软件包
  service 模块: 用于管理服务的运行状态
  user 模块: 用于批量创建用户并设置密码信息
  mount 模块: 用于批量挂载操作
  cron 模块: 批量部署定时任务信息
  ping 模块: 远程管理测试模块

 

标签:oldboy,批量,ansible,---,Ansible,模块,172.16,1.110
来源: https://www.cnblogs.com/z-macro/p/14977320.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有