• Ansible的安装及常用模块


    简介

    Ansible 基于 Python 语言实现,由 Paramiko 和 PyYAML 两个关键模块构建。

    Ansible 特点:

    1、部署简单,只需在主控端部署 Ansible 环境,被控端无需做任何操作。
    2、默认使用 SSH(Secure Shell)协议对设备进行管理。
    3、主从集中化管理。
    4、配置简单、功能强大、扩展性强。
    5、支持 API 及自定义模块,可通过 Python 轻松扩展。
    6、通过 Playbooks 来定制强大的配置、状态管理。
    7、对云计算平台、大数据都有很好的支持。
    8、提供一个功能强大、操作性强的 Web 管理界面和 REST API 接口 —- AWX 平台。

    Ansible 与 SaltStack:

    1、最大的区别是 Ansible 无需在被监控主机部署任何客户端代理,默认通过 SSH 通道进行远程命令执行或下发配置。
    2、相同点是都具备功能强大、灵活的系统管理、状态配置,都使用 YAML 格式来描述配置,两者都提供丰富的模板及 API,对云计算平台、大数据都有很好的支持。

    安装ansible

    yum安装

    yum -y install ansible
    

    配置ansible

    tree /etc/ansible/
    
    /etc/ansible/
    
    ├── ansible.cfg  # ansible.cfg 是 Ansible 工具的配置文件;
    
    ├── hosts  # ansible的主仓库 用来存储需要管理的远程主机的相关信息
    
    └── roles   # roles 是一个目录,playbook 将使用它
    

    SSH秘钥认证

    ssh-keygen -t rsa
    ssh-copy-id root@agent_host_ip
    

    添加被管理主机

    vim /etc/ansible/hosts
    
    [Client]
    angent_host_ip_1
    angent_host_ip_2
    

    测试ansible

    shell > ansible Client -m ping     # 操作 Client 组 ( all 为操作 hosts 文件中所有主机 ),-m 指定执行 ping 模块,下面是返回结果
    192.168.12.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    
    # -i          指定 hosts 文件位置
    # -u username 指定 SSH 连接的用户名
    # -k          指定远程用户密码
    # -f          指定并发数
    # -s          如需要 root 权限执行时使用 ( 连接用户不是 root 时 )
    # -K          -s 时,-K 输入 root 密码
    

    hosts主机文件

    shell > vim /etc/ansible/hosts
    
    www.abc.com     # 定义域名
    
    192.168.1.100   # 定义 IP
    
    192.168.1.150:37268   # 指定端口号
    
    [WebServer]           # 定义分组
    
    192.168.1.10
    192.168.1.20
    192.168.1.30
    
    [DBServer]            # 定义多个分组
    
    192.168.1.50
    192.168.1.60
    
    Monitor ansible_ssh_port=12378 ansible_ssh_host=192.168.1.200   # 定义别名
    
    # ansible_ssh_host 连接目标主机的地址
    
    # ansible_ssh_port 连接目标主机的端口,默认 22 时无需指定
    
    # ansible_ssh_user 连接目标主机默认用户
    
    # ansible_ssh_pass 连接目标主机默认用户密码
    
    # ansible_ssh_connection 目标主机连接类型,可以是 local 、ssh 或 paramiko
    
    # ansible_ssh_private_key_file 连接目标主机的 ssh 私钥
    
    # ansible_*_interpreter 指定采用非 Python 的其他脚本语言,如 Ruby 、Perl 或其他类似 ansible_python_interpreter 解释器
    
    [webservers]         # 主机名支持正则描述
    
    www[01:50].example.com
    
    [dbservers]
    
    db-[a:f].example.com
    

    ansible常用模块

    shell > ansible-doc -l    # 列出 Ansible 支持的模块
    
    shell > ansible-doc ping  # 查看该模块帮助信息
    

    远程命令模块(command / script / shell)

    command

    command 作为 Ansible 的默认模块,可以运行远程权限范围所有的 shell 命令,不支持管道符。
    shell > ansible Client -m command -a "free -m"               # 查看 Client 分组主机内存使用情况
    

    script

    script 的功能是在远程主机执行主控端存储的 shell 脚本文件,相当于 scp + shell 组合。
    
    shell > ansible Client -m script -a "/home/test.sh 12 34"    # 远程执行本地脚本
    

    shell

    shell模块基本和command相同,但是shell支持管道符
    
    shell > ansible Client -m shell -a "/home/test.sh"           # 执行远程脚本
    

    copy模块

    实现主控端向目标主机拷贝文件,类似于 scp 功能
    
    shell > ansible Client -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"   # 向 Client 组中主机拷贝 test.sh 到 /tmp 下,属主、组为 root ,权限为 0755
    

    stat模块

    获取远程文件状态信息,atime/ctime/mtime/md5/uid/gid 等信息
    
    shell > ansible Client -m stat -a "path=/etc/syctl.conf"
    

    get_url

    实现在远程主机下载指定 URL 到本地,支持 sha256sum 文件校验
    
    shell > ansible Client -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
    

    yum

    软件包管理
    
    shell > ansible Client -m yum -a "name=curl state=latest"
    

    corn

    远程主机 crontab 配置
    
    shell > ansible Client -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'"
    
    效果:
    * 5,2 * * * ls -alh > /dev/null
    

    mount

    远程主机分区挂载
    
    shell > ansible Client -m mount -a "name=/mnt/data src=/dev/sd0 fstype=ext4 opts=ro state=present"
    

    service

    远程主机系统服务管理
    
    shell > ansible Client -m service -a "name=nginx state=stoped"
    shell > ansible Client -m service -a "name=nginx state=restarted"
    shell > ansible Client -m service -a "name=nginx state=reloaded"
    

    user

    远程主机用户管理
    
    shell > ansible Client -m user -a "name=wang comment='user wang'"
    
    shell > ansible Client -m user -a "name=wang state=absent remove=yes"    # 添加删除用户
  • 相关阅读:
    转 SpringCloud服务注册中心比较:Consul vs Zookeeper vs Etcd vs Eureka
    转 微服务的4个设计原则和19个解决方案
    骑士问题
    种树(洛谷P1250)
    你的飞碟在这儿(洛谷P1200)
    Hello world
    [zt][cocos2dxwin32] 安装部署流程整理
    (ZT)关于IAP防止破解的几点
    (ZT)UIImage应用与内存管理
    在Mac上发布QT的程序
  • 原文地址:https://www.cnblogs.com/zhaohuhu/p/10837716.html
Copyright © 2020-2023  润新知