• ansible


    简介

    ansible是基于ssh协议实现的,通过连接插件进行管理;在ansible的管理机上执行的ansible命令会在临时目录中生成对应的python文件,并且把管理机上的python文件所在的临时目录拷贝到被管理的机器上去执行,执行完成后删除临时目录。

    ansible基本命令:

    [root@centos7 ~]# ansible -h                       #查看帮助
    [root@centos7 ~]# ansible --version                #查看版本  
    [root@centos7 ~]# ansible all --list               #列出主机清单中所管理的所有主机  
    [root@centos7 ~]# ansible websrvs --list           #列出websrvs分组中的所有主机  
    [root@centos7 ~]# ansible-doc -l                   #列出ansible所有模块  
    [root@centos7 ~]# ansible-doc file                 #查看ansible关于file模块的详细帮助  
    [root@centos7 ~]# ansible-doc -s file              #查看ansible关于file模块的简单帮助
    [root@centos7 ansible]# ansible-galaxy list        #查看写好的角色列表
    [root@centos7 ansible]# ansible-galaxy remove dong.users        #删除角色
    

    使用ansible前需要做的准备:

    ansible是基于ssh协议进行连接管理的,为了方便进行管理,需要基于key验证  
    [root@centos7 ~]# ssh-keygen     #生成公钥私钥对儿  
    [root@centos7 ~]# ssh-copy-id 192.168.38.47        #把公钥发送给远程主机
    

    ansible的配置文件

    [root@centos7 ~]# rpm -ql ansible|grep /etc/  
    /etc/ansible  
    /etc/ansible/ansible.cfg   #ansible配置文件  
    /etc/ansible/hosts         #ansible主机清单  
    /etc/ansible/roles         #角色
    

    ansible配置文件所需要的修改:

    [root@centos7 ~]# vim /etc/ansible/ansible.cfg
    host_key_checking = False     #省略了key检查,相当于系统默认回复了yes,不需要第一次ssh连接回复yes/no  
    log_path = /var/log/ansible.log   #开启ansible日志  
    module_name = shell           #设置ansible的默认模块为shell模块
    

    ansible中常用的模块:

    ping模块
    [root@centos7 ~]# ansible websrvs -m ping     #测试所管理的websrvs分组中的主机的连接状态
    
    shell模块
    chdir: 切换目录
    [root@centos7 ~]# ansible websrvs -a '想要执行的linux命令'        #指定分组,后面指定想要执行的linux命令
    
    script模块
    [root@centos7 ansible]# ansible websrvs -m script -a '/data/ansible/test.sh'     #script模块是专门用于执行脚本的模块,脚本需要提前加好执行权限,指定脚本路径
    
    copy模块
    src: copy模块可以拷贝目录和文件;如果源是一个目录,以 "/" 结束,则拷贝的是目录里面的所有文件;不以 "/" 结束,则拷贝的是整个目录  
    mode: 指定权限  
    owner: 指定所有者  
    group: 指定所属组  
    backup: 做备份  
    content: 以内容方式生成一个新的文件(例:'content="[test]
    baseurl=111" dest=/data/test1' )
    [root@centos7 ansible]# ansible websrvs -m copy -a 'src=/etc/issue dest=/data mode=700 owner=dong group=bin backup=yes'
    
    fetch模块
    把远程主机的文件拷贝到ansible服务器上;只支持拷贝文件 
    src: 远程主机的源地址  
    dest: ansible服务器地址
    [root@centos7 ansible]# ansible websrvs -m fetch -a 'src=/var/log/messages dest=/data'
    
    file模块
    state: absent(删除;如果是目录,则递归删除)、directory(如果目录不存在,就创建文件夹)、link(创建软链接)、hard(创建硬链接)、touch(如果文件不存在,就创建文件)
    path:必须指定目标路径
    src: 创建软、硬链接才可以指定的源文件路径
    [root@centos7 ansible]# ansible websrvs -m file -a 'path=/data/test.txt state=touch'
    [root@centos7 ansible]# ansible websrvs -m file -a 'src=/data/test.txt path=/data/test.link state=link'     #path后面指定软链接文件名
    
    unarchive
    解包
    copy: yes(将ansible主机上的压缩包传到远程主机后解压缩至特定目录;默认是yes);no(将远程主机上的某个压缩包解压缩到指定路径下)
    src: 源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
    dest:远程主机上的目标路径
    mode:设置解压缩后的文件权限
    [root@centos7 ansible]# ansible websrvs -m unarchive -a 'src=boot.tar.gz dest=/data'    #ansible机器上的打包文件解压缩到远程主机上  
    [root@centos7 ansible]# ansible websrvs -m unarchive -a 'src=/tmp/boot.tar.gz dest=/opt/ copy=no'       #把远程主机上的包解压缩到远程主机的指定目录下
    
    archive模块
    打包
    path: 指定针对远程主机的目录
    dest: 打包到哪里
    format: 压缩格式
    [root@centos7 ansible]# ansible websrvs -m archive -a 'path=/etc/sysconfig dest=/data/sysconfig.tar.gz format=gz'
    
    cron模块
    minute: 设置分钟
    job: 所执行的任务;命令需要加路径
    name: 任务名称
    disabled: 禁用计划任务
    state: absent(删除)
    [root@centos7 ansible]# ansible websrvs -m cron -a 'minute=*/10 job="/usr/sbin/ntpdate 192.168.38.7" name=synctime disabled=yes'
    
    yum模块
    state: installed(安装;默认不加state就是installed)、absent(卸载)
    name: 需要安装的软件名称
    [root@centos7 ansible]# ansible websrvs -m yum -a 'name=httpd,mariadb-server state=absent'
    
    service模块
    name: 服务名称
    state: started(开启)、stopped(停止)、restarted(重启服务)
    enabled: 开机启动
    [root@centos7 ansible]# ansible websrvs -m service -a 'name=httpd enabled=yes state=started'
    
    user模块
    name: 用户名
    uid: 指定uid
    group: 指定所属组
    groups: 指定附加组
    system: yes(指定为系统用户)
    home: 指定家目录
    shell: 指定shell类型
    state: absent(删除用户)
    remove: yes(配合state=absent使用,删除与用户相关的家目录)
    password: 设置密码(密码必须是加密的,可以使用openssl passwd -1生成md5的加密密码)
    remove: yes(把用户账号和家目录都删除)
    create_home: no(不创建家目录)
    [root@centos7 ansible]# ansible websrvs -m user -a 'name=test uid=88 system=yes home=/data/app group=root groups=bin,dong shell=/sbin/nologin'
    
    group模块
    name: 指定组名
    gid: 指定gid
    state: absent(删除)
    [root@centos7 ansible]# ansible websrvs -m group -a 'name=test gid=1234'
    

    角色参考网站

    https://galaxy.ansible.com              #下载安装网站上写好的角色文件
  • 相关阅读:
    013.[转] 云原生
    012.[转] 云计算
    011.maven 继承与聚合
    Window常用且通用快捷键
    Hibernate Validator Engine的用法
    Mybatis书写
    Ajax错误
    apache commons lang架包介绍
    Mybatis使用Mybatis-generator插件及配置(数据库逆向工程)
    二分搜索树
  • 原文地址:https://www.cnblogs.com/dongzhanyi123/p/11899863.html
Copyright © 2020-2023  润新知