综合架构 ansible模块补充

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"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容