zoukankan      html  css  js  c++  java
  • Ansible集群自动化运维操作

    1、//配置分组主机

    vim /etc/ansible/hosts
    

     

    内容:
    # 方法一 主机+端口+密码

    [webservers]
    192.168.16.1 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"

    # 方法二 主机+端口+密码

    [webservers]
    192.168.16.[2:4] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
    192.168.16.1[2:4] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
    

     

    # 方法二 主机+端口+密码

    [webservers]
    192.168.1.3[1:3]
    [webserver:vars]
    ansible_ssh_pass="123456"
    

     

    2、执行命令

    ansible <pattern_goes_here> -m <module_name> -a <arguments>
    
    ansible 192.168.17.128 -m ping
    
    ansible 192.168.17.128 -m command -a 'ls /usr/local'
    
    ansible webservers -m command -a 'chdir=/data/ ls'	#先切换到/data/ 目录,再执行“ls”命令
    

      

    3、传输文件到远程机

    ansible webservers -m copy -a "src=/home/elk/metricbeat.yml dest=/etc/metricbeat/" > /home/elk/scp-metric-yml.log
    

      

    #覆盖备份,设置权限666

    ansible webservers -m copy -a 'content="I am keerya
    " backup=yes dest=/data/name mode=666'
    

      

    4、运行shell执行命令

    ansible webservers -m shell -a 'cat /etc/passwd |grep "keer"'
    

     

    5、文件操作
    ①创建目录

    ansible webservers -m file -a 'path=/data/app state=directory'

    ②创建链接

    ansible webservers -m file -a 'path=/data/bbb.jpg src=aaa.jpg state=link'
    

     

    ③删除文件

    ansible webservers -m file -a 'path=/data/a state=absent'
    

      

    6、从远程机拉取文件到本地(file,不是目录)

    ansible webservers -m fetch -a 'src=/data/hello.txt dest=/data' 
    

      

    7、cron定时任务模块
    ①添加名为ntphh的定时任务

    ansible webservers -m cron -a 'name="ntphh" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'

    ②删除定时任务

    ansible webservers -m cron -a 'name="ntphh" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null" state=absent'
    

    8、yum安装程序

    ansible webservers -m yum -a 'name=htop state=present'
    

      

    9、启动service
    ①开启服务并设置自启动

    ansible webservers -m service -a 'name=nginx state=started enabled=true' 
    

      

    ②关闭服务

    ansible webservers -m service -a 'name=nginx state=stopped'
    

      


  • 相关阅读:
    悬崖边上的舞者,记7.2生产数据库灾难事件
    MySQL数据库辅助类
    C# 自定义控件VS用户控件
    WinForm创建自定义控件
    mvc导出excel
    aspose导出excel文件
    oracle 定义带参数的视图
    extjs 分组函数自定义统计
    向第三次世界大战中的勇士致敬
    Office2007打开文件提示“您尝试打开的文件xxx.xls的格式与文件扩展名指定的格式不一致”的解决办法
  • 原文地址:https://www.cnblogs.com/hyh123/p/14243373.html
Copyright © 2011-2022 走看看