ansible学习1,


一、常见的自动化运维工具

1.功能上分类

OS Provisioning:PXE,cobbler:参考链接 http://renjunjie622.blog.51cto.com/2913680/1782190

        物理机:PXE、Cobbler

        虚拟机:Image Templates(镜像模板)


OS Config:程序包管理、用户管理、配置文件、服务管理、cron任务等等;

        puppet (ruby) 

        saltstack (python)

        chef

        cfengine

Task Exec:

        fabric,func,saltstack


Deployment:

        fabric



2.从代理上分类:

        agent: puppet, func

        agentless: ansible, fabric(运行ssh服务)


二、ansible介绍

1.ansible特性:

    1.高度模块化,借助模块完成各种任务

    2.agentless,即无需在被控制端安装agent

    3.默认基于ssh协议向被控制端发送操作指令

       基于密钥认证

       在inventory文件中指定账号和密码       

    4.一系列任务执行可写成剧本(playbook)

    5.具有幂等性:不会重复执行相同操作,比如不会重复安装软件

    6.主从模式:master:ansible,ssh client  slave:ssh server


2.ansible的核心组件:

    ansible core(核心程序)

    host inventory(主机列表)

    core modules(核心模块)

    custom modules(自定义模块)

    playbook(yaml,jinjia2)(剧本)

    connection plugin(连接插件,负责和被控制端通信)

    plugin(其他插件:邮件发送、日志)


3.安装

ansible依赖于Python 2.6或更高的版本、paramiko、PyYAML及Jinja2。

1 2 3 4 5 6 7 8 9 10 11 12 1.编译安装 解决依赖关系 # yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto # tar xf ansible-1.5.4.tar.gz # cd ansible-1.5.4 # python setup.py build # python setup.py install # mkdir /etc/ansible # cp -r examples/* /etc/ansible   2.rpm包安装(配置epel yum源)   #建议采用 # yum install ansible

 3.ansible重要文件               

1 2 配置文件:/etc/ansible/ansible.cfg  主机列表文件:/etc/ansible/hosts


4.root基于密钥,免密登入

1 2 3 4 5 (1)yum -y install openssh-clients  (2)ssh-keygen -t rsa (3)ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.193.129   具体解释参见:http://nxyboy.blog.51cto.com/10511646/1944205


三、常用模块与命令

1.命令格式:

1 2 3 4 5 6 7 ansible命令基础: ansible <host-pattern> [-f forks] [-m module_name] [-a args]     -f forks: 启动的并发线程数     -m module_name:     -a args:        args:   key=value     -i 指定hosts文件,不使用默认


2.查看模块帮助:

1 2 3 ansible-doc  -l      #列出ansible所支持的模块 ansible-doc  -s  MODULE_NAME  #查看指定模块 ansible-doc  -s  yum


3.常用模块module_name介绍

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1.command  命令模块,默认模块,用于在远程执行命令         -a 'COMMAND' #ansible node2 -m command -a "date"     2.user  管理用户           -a 'name= state={present|absent} system=  uid='                 name=:指明用户名  password=加密串 #ansible node2 -m user -a 'name=mysql uid=306 system=yes group=mysql'     3.group             -a 'name= gid= state= system='              state:present,absent #ansible node2 -m group -a 'name=mysql gid=306 system=yes state=present '     4.cron  周期性任务计划模块       -a 'name= minute= hour= day= month= weekday= job= user= state=' #ansible node2 -m cron -a 'minute="*/10" job="/bin/echo hahaha" name="test" state="present" '               5.copy(复制文件)     -a 'dest= src= mode= owner= group=   content='                 src=:本地源文件路径(可以相对和绝对路径)         dest=:目标文件路径(绝对路径)         content=: 直接生成文件内容,取代src(不能与src同时存在)         force: 当设置为yes时,如果目标主机存在该文件,但内容不同,会强制覆盖。默认为yes                 backup: 在覆盖之前备份源文件,yes/no #ansible node2 -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640 ' #ansible node2 -m copy -a 'content="Hello\n  hello\n " dest=/tmp/hello.ansible '     6.file(设置文件属性)      -a 'path= mode= owner= group= state={directory|link|present|absent} src='         path=:指定文件路径,可以使用name或dest来替换         src=:被链接的源文件路径,只应用于 state=link 的情况             创建文件的链接:             src=:指定源文件   path=:指定链接文件             state=:后面接文件的各种状态,如directory, link, hard, file及absent(删除)                   directory: 如果目录不存在,则创建目录                   file: 即使文件不存在,也不会被创建                   absent: 删除目录、文件或链接文件                   touch: 如果文件不存在,则会创建一个新文件,如果存在,则更新其时间戳                   link: 创建软链接                   hard:硬链接   #ansible node2 -m file -a 'owner=mysql group=mysql mode=644 path="/tmp/fstab.ansible"' #ansible node2 -m file -a ' path="/tmp/fstab.link"  src="/tmp/fstab.ansible" state=link '          7.ping 测试远程主机能否能连接,没有参数 #ansible node2 -m ping     8.yum(安装或卸载程序包)     -a 'name= state={present|latest|absent}'         name=指明要安装的软件包,可以带上版本号 #ansible node2 -m yum -a 'name=zsh  state=present'                            9.service(指定程序运行状态)     -a 'name= state={started|stopped|restarted} enabled='         enabled=:是否开机自启,取值为truefalse         state=现在状态 #ansible node2 -m service -a 'enable=true name=httpd state=started '     10.shell (管道,变量 复杂命令)      -a 'COMMAND' #ansible node2 -m shell -a 'echo 123456 | passwd --stdin user1 '     11.script(本地脚本,复制到远程主机上并运行)         -a '/path/to/script'  #ansible node2 -m script -a '/tmp/test.sh'      12.setup(收集远程主机的facts) ansible-doc -s setup #ansible node2 -m setup   playbook运行时,会自动调用setup模块收集远程主机的相关信息(称为facts,如操作系统版本、ip地址、cpu数量等),这些信息保存于变量中,可在playbook中引用。   我们也可直接使用ansible命令直接获取这些变量信息: ansible all -m setup [-a 'filter=ansible_eth[0-2]']        filter:过滤器,表示只返回与指定shell风格通配符匹配的变量信息 ansible all -m setup --tree /tmp/facts        --tree:表示将收集的facs以树状的结构输入到指定文件中          [root@node1 ~]#  ansible node2 -m setup -a 'filter=ansible_fqdn' 192.168.193.129 | SUCCESS => {     "ansible_facts": {         "ansible_fqdn""node2"     },      "changed"false }                   13.template  template是使用了Jinjia2格式作为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。              举个例子:          vim /root/httpd.conf            ...            ServerName {{ ansible_fqdn }} #ansible node2 -m template -a 'src=/root/httpd.conf desc=/etc/httpd/conf/httpd.conf   /root/httpd.conf文件被复制到第一个主机时,ServerName的值被替换成第一个主机的ansible_fqdn的值node2,而被复制到第二个主机时,ServerName的值会被替换成node3     14.synchronize 该模块会调用rsync命令,用于将ansible机器的指定目录推送到客户机器 的指定目录下 #ansible node2 -m synchronize -a 'src=/usr/local/src/ dest=/usr/local/src/ delete=yes compress=yes'   15.get_url 常用模块,可以实现在远程主机上下载url到本地 #ansible node2 -m get_url -a 'url=http://xxx.com dest=/tmp'   更多模块,参考http://www.361way.com/ansible-modules/4415.html
本文转自   a8757906   51CTO博客,原文链接:http://blog.51cto.com/nxyboy/1952519

相关内容