zoukankan      html  css  js  c++  java
  • Ansible常用模块及API

    Ansible安装

    安装EPEL作为安装Ansible的yum源(CentOS6.4):

    rpm -Uvh http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm

    安装Ansible:

    yum install ansible -y

    配置文件:

    路径:/etc/ansible/hosts
    配置说明:webservers为组名,下面的ip或域名则是属于该组的主机。

    [webservers]
    192.168.1.111
    192.168.1.112
    192.168.1.113

    测试:

    ansible webservers -m ping -k   #对webservers组进行ping操作

    由于主控端与被控主机未配置SSH证书信任,需要在执行ansible命令时添加-k参数,要求提供root(默认)账号密码。

    配置SSH证书信任:
    主控端:
    生成密钥对:

    ssh-keygen -t rsa    #一路回车,在/root/.ssh/目录下会生成id_rsa(私钥)、id_rsa.pub(公钥)密钥对

    将公钥发送的被控主机:

    ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.111

    常用模块

    命令行调用模块格式:

    ansible 操作目标 -m 模块名(默认模块为command) -a 模块参数
    
    #例:
    ansible webservers -m ping

    帮助命令:

    ansible-doc 模块名
    
    #例:
    ansible-doc ping

    1、远程命令模块

    command:执行远程shell命令
    script:在被控端执行主控端上存放的脚本,相当于scp+shell
    shell:执行存放在被控端上的脚本
    例:

    ansible webservers -m command -a 'df -h' #在被控端执行df -h命令
    ansible webservers -m script -a '/root/test.sh' #在被控端执行test.sh脚本(test.sh脚本在主控端)
    ansible webservers -m shell -a '/root/test.sh' #在被控端执行test.sh脚本(test.sh脚本在被控端)

    2、copy模块

    copy:从主控端向被控端拷贝文件,类似于scp功能
    例:

    ansible webservers -m copy -a 'src=/root/test.py dest=/tmp owner=root group=root mode=0755'
    #将主控端的test.py文件拷贝到被控端的/tmp目录下,并且指定文件的属主和权限

    3、stat模块

    stat:获取远程文件状态信息,包括atime、ctime、mtime、md5、uid、gid等
    例:

    ansible webservers -m stat -a 'path=/etc/sysctl.conf' #获取被控端/etc/sysctl.conf文件状态信息

    4、get_url模块

    get_url:实现被控端下载指定URL,支持sha256sum校验
    例:

    ansible 192.168.1.111 -m get_url -a 'url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes'
    #被控端下载百度首页到/tmp/index.html中

    5、软件操作模块(yum、apt)

    yum/apt:linux平台软件包管理操作
    例:

    ansible 192.168.1.111 -m yum -a 'name=curl state=latest' #被控端使用yum安装最新的curl
    ansible 192.168.1.111 -m apt -a 'pkg=curl state=latest' #被控端使用apt安装最新的curl

    6、cron模块

    cron:被控端cron配置
    例:

    ansible 192.168.1.111 -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'" 
    
    #被控端cron结果:
    
    #Ansible: check dirs
    * 5,2 * * * ls -alh > /dev/null

    7、mount模块

    mount:被控端分区挂载
    例:

    ansible 192.168.1.111 -m mount -a 'name=/mnt/data src=/dev/sd0 fstype=ext3 opts=ro state=present'
    #将/dev/sd0挂载到/mnt/data,权限为ro

    8、service模块

    service:被控端系统服务管理
    例:

    ansible webservers -m service -a 'name=httpd state=stopped' #关闭httpd服务
    ansible webservers -m service -a 'name=httpd state=restarted' #重启httpd服务 
    ansible webservers -m service -a 'name=httpd state=started' #启动httpd服务

    9、sysctl包管理模块

    sysctl:被控端sysctl配置
    例:

    ansible 192.168.1.111 -m sysctl -a 'name="net.ipv4.ip_forward" value=1 sysctl_set=yes state=present reload=yes'
    #设置路由转发并生效

    10、user服务模块

    user:被控端系统用户管理
    例:

    ansible 192.168.1.111 -m user -a "name=johnd comment=Hohn Doe" #添加用户john
    ansible 192.168.1.111 -m user -a "name=johnd state=absent remove=yes" #删除用户john

    API调用方式

    https://ansible-tran.readthedocs.io/en/latest/docs/developing_api.html

    安装使用Ansible可能会出现的问题

    http://blog.csdn.net/longxibendi/article/details/46989735


    参考资料:

    刘天斯《Python自动化运维技术与最佳实践》

  • 相关阅读:
    [原创]手把手教你如何把二维码插件zxing加入到android和ios项目中
    解决通过Intent调用系统拍照程序,返回图片太小的问题[android] 【转】
    SVN Command
    取得ie 里面 自定义函数或者属性的集合 使用RuntimeObject()
    scrum 开发模型
    javascript AOP 实现,ajax回调函数使用比较方便
    印度英语的特点
    AspectJS
    java 打jar包 转
    XP 开发模式
  • 原文地址:https://www.cnblogs.com/MacoLee/p/5691217.html
Copyright © 2011-2022 走看看