美文网首页
综合架构 ansible模块补充

综合架构 ansible模块补充

作者: 一只打不死的小强 | 来源:发表于2019-07-24 21:37 被阅读0次

ansible服务模块

1) file  --- 修改远程主机数据属性信息   
             创建和删除远程主机数据信息
ansible

参数信息

path --- 指定远程主机上已有的一个文件数据
mode --- 修改数据权限数值
owner --- 修改属主
group --- 修改属组
state --- directory touch link hard absent
recurse=yes 修改目录权限时, 进行递归修改

ansible 172.16.1.41 -m file -a "path=/backup/oldboy.txt mode=666 owner=oldboy group=oldboy"  --- 修改文件权限
ansible 172.16.1.41 -m file -a "path=/backup/oldboy state=directory"  --- 创建目录
ansible 172.16.1.41 -m file -a "path=/backup/oldboy/oldboy01/oldboy02/ state=directory"                --- 创建多级目录
ansible 172.16.1.41 -m file -a "path=/backup/oldboy.txt state=touch"  --- 创建文件
ansible 172.16.1.41 -m file -a "src=/backup/oldboy.txt  path=/backup/oldboy_soft_link.txt state=link"  --- 创建软链接
ansible 172.16.1.41 -m file -a "src=/backup/oldboy.txt  path=/backup/oldboy_hard_link.txt state=hard"  --- 创建硬链接
ansible 172.16.1.41 -m file -a "path=/backup/oldboy_hard_link.txt state=absent"                        --- 删除数据操作 
ansible 172.16.1.41 -m file -a "path=/backup/oldboy02.txt state=file"                                  --- 检查文件是否存

yum --- 批量安装软件模块

  • name --- 指定要安装的软件名称
  • state --- 安装软件 或是 卸载软件
  • present --- 安装软件 installed
  • latest --- 更新软件
  • removed --- 移除卸载软件 absent

ansible 172.16.1.41 -m yum -a "name=htop state=absent"    --- 卸载软件 
ansible 172.16.1.41 -m yum -a "name=htop state=installed" --- 安装软件
ansible 172.16.1.41 -m yum -a "name=htop state=removed"   --- 卸载软件

service --- 批量管理服务启动状态

name --- 管理哪个服务名称
state --- 指定服务运行状态
reloaded
restarted
started
stopped


       ansible 172.16.1.41 -m service -a "name=crond state=stopped"
       ansible 172.16.1.41 -m service -a "name=crond state=started"
       ansible 172.16.1.41 -m service -a "name=crond state=restarted"
       enabled --- 设置服务是否开机运行
       ansible 172.16.1.41 -m service -a "name=crond enabled=no"   --- 开机不运行
       ansible 172.16.1.41 -m service -a "name=crond enabled=yes"  --- 开机自动运行服务

corn --- 批量部署定时任务

name --- 定义定时任务注释信息
state --- absent删除定时任务
disabled --- 给定时任务添加注释

示例:minute hour day month weekday job="ntpdate ntp1.aliyun.com"。

ansible定时任务设置命令
       01. 要求: 每隔五分钟,进行时间同步
       ansible 172.16.1.41 -m cron -a "minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null'"
       02. 要求: 每周五,晚上10:30, 需要去大保健 
       ansible 172.16.1.41 -m cron -a "name='大保健' minute=30 hour=22 weekday=5 job='make shufu &>/dev/null'"
       说明: 定时任务配置时.需要添加name注释信息,否则会出现反复创建相同定时任务
    
       ansible 172.16.1.41 -m cron -a "name='大保健03' state=absent"   --- 删除指定定时任务
       ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=yes"  --- 给定时任务添加注释
       ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=no"   --- 取消定时任务注释

补充: ansible帮助信息查看方法
01. 查看ansible所有模块
ansible-doc -l
02. 查看ansible模块参数信息
ansible-doc -s cron
03. 查看ansible模块详细信息
ansible-doc cron


user --- 批量创建创建用户

name --- 创建的用户名称
password --- 设置用户密码信息 必须设置为密文
create_home --- yes 表示创建家目录 no 不创建家目录
shell --- 指定用户登录方式 shell=/sbin/nologin
group --- 指定用户属于哪个组 主要组
groups --- 指定用户属于哪个组 附属组
uid --- 指定用户uid数值
state --- absent删除用户
remove -- yes 删除家目录

ansible 172.16.1.41 -m user -a "name=alex create_home=no shell=/sbin/nologin"   --- 批量创建虚拟用户
       ansible 172.16.1.41 -m user -a "name=alex group=oldboy groups=oldgirl"          --- 指定用户所属组
       ansible 172.16.1.41 -m user -a "name=alex uid=2000"                             --- 指定用户uid
       ansible 172.16.1.41 -m user -a "name=alex state=absent"                         --- 删除用户
    
       ansible 172.16.1.41 -m user -a 'name=oldbaby password="$6$oldgirl$kAUTXVC2z1agr1HlmpFe9abFhWKwJ1fNyg64F95U3rVumwQfqOuhV3YkyZU9.H79TChzIKn5epl5M18B199qV1"'
       --- 给用户设置密码

ps: 密码密文生成方式:

    https://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module
    方法一: 
    ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"
    mypassword      --- 明文密码信息
    sha512          --- 明文转换为密文加密方法
    mysecretsalt    --- 用什么做算法依据生成密文信息
    ansible all -i localhost, -m debug -a "msg={{ 'oldboy123' | password_hash('sha512', 'oldgirl') }}   
    $6$oldgirl$kAUTXVC2z1agr1HlmpFe9abFhWKwJ1fNyg64F95U3rVumwQfqOuhV3YkyZU9.H79TChzIKn5epl5M18B199qV1
    
    实践操作:
    [root@m01 ~]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy123') }}"
    localhost | SUCCESS => {
        "msg": "$6$oldboy123$W3jkmkkVTr.9UStm4S50RT2uIEjB/4GEtaAeVCSZ..uWVN1YGxHvluss9JVfAPV0gSJoGn1qAfxGyttIsTjcz0"
    }

    方法二:  在centos7中无法使用
    mkpasswd --method=sha-512
            
    方法三:  利用python模块功能
    yum install python-pip
    pip install passlib 
    优化pip源
    ~/.pip/pip.conf
    中添加或修改: 
    [global]
    index-url = https://mirrors.aliyun.com/pypi/simple/
    
    [install]
    trusted-host=mirrors.aliyun.com

    python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"

mount --- 批量挂载模块

src --- 需要挂载的存储设备
path --- 挂载点信息
fstype --- 挂载文件系统类型 nfs
state --- 挂载和卸载设置参数
mounted : 实现立即挂载 和 开机自动挂载 (推荐)
present : 只能实现开机自动挂载
unmounted : 立即卸载 但是 不会永久卸载
absent : 立即卸载 永久卸载 错误提示

ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted"
ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=unmounted"

利用管理端ansible程序部署rsync

  • 第一个历程: 检查环境
  • 第二个历程: 配置ansible主机清单.
vim /etc/ansible/hosts
    [rsync:children]
    rsync_server
    rsync_client
    [rsync_server]
    172.16.1.41 ansible_user=root ansible_password=123456
    [rsync_client]
    172.16.1.31 
    172.16.1.7  ansible_user=root ansible_password=654321 ansible_port=52113

测试ansible能否管理远程主机

    [root@m01 172.16.1.41]# ansible rsync -m ping
    172.16.1.31 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    172.16.1.41 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    172.16.1.7 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    
  • 第三个历程: 利用模块功能部署rsync服务
    服务端配置:
    01. 安装软件程序
    ansible rsync -m yum -a "name=rsync state=installed"
    02. 编写配置文件
    在管理端准备好服务配置文件
    ansible rsync_server -m copy -a "src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/"
    03. 创建虚拟用户
    ansible rsync_server -m user -a "name=rsync create_home=no shell=/sbin/nologin"
    04. 创建密码文件 (授权600)
    ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"
    05. 创建备份目录 (授权 属主 属组)
    ansible rsync_server -m file -a "path=/backup state=directory owner=rsync group=rsync"
    06. 启动程序服务
    ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"

客户端配置:

    01. 创建密钥文件 (授权600)
    ansible rsync_client -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"
    02. 批量测试传输文件
    ansible rsync_client -m shell -a "rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password"

相关文章

网友评论

      本文标题:综合架构 ansible模块补充

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