ansible学习笔记

简介:

 在日常服务器维护中,从系统安装到程序部署再到发布应用,在大规模的生产环境中,如果需要手动的每台服务器进行安装配置将会给运维人员带来许多繁琐而又重复的工作。这就促使了在每个运维层次中出现了不同的自动化运维工具。

常见的自动化运维工具分类有以下几类:

 系统安装运维工具(OS Provisioning):

   常见的有:PXE,Cobbler,Red Hat Satelite(redhat)系统专用等

 操作系统的配置运维工具(OS Config):

   常见的有:cfengine,puppet,saltsack,chef等 

 应用程序部署工具(Application Service Orchestration):

   常见的有:Func,Fabric,ControITier,Capistrano等

根据工作模式不同上面的运维工具有分为以下两类:

   agent:基于ssl协议实现,agent工作在被监控端,例如:puppet

   agentless: 基于ssh key实现,例如:ansible

ansible介绍:

 ansible是一款轻量级自动化运维工具,由Python语言开发,结合了多种自动化运维工具的特性,实现了批量系统配置、批量程序部署、批量命令执行等功能;ansible是基于模块化实现批量操作的。

各模块之间的工作联系如下图所示:

ansible架构图.jpgansible的特点:

 模块化、部署简单、工作于agentless模式、默认使用ssh协议、支持自定义模块、支持Palybook等

一、ansible安装以及常用的模块介绍

1、安装ansible

[root@node1 ~]# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto ansible

2、配置ansible的主机文件,编辑/etc/ansible/hosts,添加管理节点主机:

[test]  \\主机组名,可以任意命名
172.16.2.13 \\管理节点主机,也可以是主机名

3、ansible常用的模块有:

command模块:默认模块,用于在各被管理节点运行指定的命令;

例:[root@node1 ~]# ansible all  -m command -a 'ifconfig eth0'
172.16.2.13 | success | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F8:D4:88  
          inet addr:172.16.2.13  Bcast:172.16.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fef8:d488/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8046 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2165 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1392192 (1.3 MiB)  TX bytes:201703 (196.9 KiB)

user模块:用户模块,用于在各被管理节点管理用户所使用;

例:[root@node1 ~]# ansible all  -m user -a 'name=test'
172.16.2.13 | success >> {  
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 500, 
    "home": "/home/test", 
    "name": "test", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 500
}
[root@node2 ~]# tail  -1 /etc/passwd
test:x:500:500::/home/test:/bin/bash

group模块:用户组模块,用于在各被管理节点管理用户组所使用;

例:[root@node1 ~]# ansible all  -m group  -a 'name=mylinux gid=1000'
172.16.2.13 | success >> {
    "changed": true, 
    "gid": 1000, 
    "name": "mylinux", 
    "state": "present", 
    "system": false
}

[root@node2 ~]# tail -1 /etc/gshadow
mylinux:!::

cron模块:计划任务模块,用于在各被管理节点管理计划任务;

例:[root@node1 ~]# ansible all -m cron -a  "name=time  minute='*/2' job='/usr/sbin/ntpdate 172.16.12'"
172.16.2.13 | success >> {
    "changed": true, 
    "jobs": [
        "time"
    ]
}
[root@node2 ~]# crontab  -l  \\在管理节点查看cron任务
#Ansible: time
*/2 * * * * /usr/sbin/ntpdate 172.16.12

copy模块:复制模块,复制文件至各管理节点;

例:[root@node1 ~]# ansible all -m copy -a 'src=/root/test dest=/tmp mode=600'
172.16.2.13 | success >> {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0600", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1439189042.77-131108212586927/source", 
    "state": "file", 
    "uid": 0
}
[root@node2 ~]# ls -l /tmp/test
-rw------- 1 root root 0 Aug 10 14:44 /tmp/test

file模块:文件模块,修改各个节点指定的文件属性;

例:[root@node1 ~]# ansible all -m file -a 'path=/tmp/test mode=644 owner=test'
172.16.2.13 | success >> {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "test", 
    "path": "/tmp/test", 
    "size": 0, 
    "state": "file", 
    "uid": 500
}
[root@node2 ~]# ls -l /tmp/test
-rw-r--r-- 1 test root 0 Aug 10 14:44 /tmp/test

ping模块:测试模块,测试各个被管理节点是否在线;

例:[root@node1 ~]# ansible all  -m ping
172.16.2.13 | success >> {
    "changed": false, 
    "ping": "pong"
}

service模块:管理各个节点的服务

例:[root@node1 ~]# ansible all -m service -a 'name=ntpd  enabled=true'
172.16.2.13 | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "ntpd"
}

shell模块:与command模块功能相同,但比command的模块功能强大

例:[root@node1 ~]# ansible all -m shell -a 'cat /etc/passwd | grep root'
172.16.2.13 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

script模块:自动复制脚本至远程节点,并运行之

例:[root@node1 ~]# cat ansible.sh 
#!/bin/bash
echo  "hello word" >> /tmp/test
[root@node1 ~]# ansible all -m script -a '/root/ansible.sh'
172.16.2.13 | success >> {
    "changed": true, 
    "rc": 0, 
    "stderr": "OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug1: mux_client_request_session: master session id: 2\r\nShared connection to 172.16.2.13 closed.\r\n", 
    "stdout": ""
}
[root@node2 ~]# cat /tmp/test
hello word

setup模块:收集ansible的facters

例:[root@node1 ~]# ansible all -m setup
172.16.2.13 | success >> {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.16.2.13"
        ], 
       ...............

yum模块:用于在各个管理节点安装软件所使用

例:[root@node1 ~]# ansible all -m yum -a 'name=httpd state=present'
172.16.2.13 | success >> {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base: mirrors.yun-idc.com\n * extras: mirrors.yun-idc.com\n * updates: mirrors.yun-idc.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-45.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-45.el6.centos for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-45.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package             Arch         Version                      Repository  Size\n================================================================================\nInstalling:\n httpd               x86_64       2.2.15-45.el6.centos         base       829 k\nInstalling for dependencies:\n apr-util-ldap       x86_64       1.3.9-3.el6_0.1              base        15 k\n httpd-tools         x86_64       2.2.15-45.el6.centos         base        77 k\n mailcap             noarch       2.1.31-2.el6                 base        27 k\n\nTransaction Summary\n================================================================================\nInstall       4 Package(s)\n\nTotal download size: 947 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           977 kB/s | 947 kB     00:00     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : httpd-tools-2.2.15-45.el6.centos.x86_64                      1/4 \n\r  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/4 \n\r  Installing : mailcap-2.1.31-2.el6.noarch                                  3/4 \n\r  Installing : httpd-2.2.15-45.el6.centos.x86_64                            4/4 \n\r  Verifying  : mailcap-2.1.31-2.el6.noarch                                  1/4 \n\r  Verifying  : httpd-2.2.15-45.el6.centos.x86_64                            2/4 \n\r  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/4 \n\r  Verifying  : httpd-tools-2.2.15-45.el6.centos.x86_64                      4/4 \n\nInstalled:\n  httpd.x86_64 0:2.2.15-45.el6.centos                                           \n\nDependency Installed:\n  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        \n  httpd-tools.x86_64 0:2.2.15-45.el6.centos                                     \n  mailcap.noarch 0:2.1.31-2.el6                                                 \n\nComplete!\n"
    ]
}
[root@node2 ~]# rpm -q httpd
httpd-2.2.15-45.el6.centos.x86_64

4、ansible使用帮助

ansbile-doc -l \\列出ansible的所有模块
ansible-doc -s module_name \\查看模块的属性信息
例:查看service模块的属性信息;
[root@node1 ~]# ansible-doc -s service
less 436
Copyright (C) 1984-2009 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: M a n a g e   s e r v i c e s .
  action: service
      arguments              # Additional arguments provided on the command line
      enabled                # Whether the service should start on boot. *At least one of state and enabled are required.*
      name=                  # Name of the service.
      pattern                # If the service does not respond to the status command, name a substring to look for as would be found in the output of the `ps' command 
      runlevel               # For OpenRC init scripts (ex: Gentoo) only.  The runlevel that this service belongs to.
      sleep                  # If the service is being `restarted' then sleep this many seconds between the stop and start command. This helps to workaround badly beha
      state                  # `started'/`stopped' are idempotent actions that will not run commands unless necessary.  `restarted' will always bounce the service.  `r

原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/6941

(0)
马行空马行空
上一篇 2015-08-17
下一篇 2015-08-17

相关推荐

  • lvs初探

    LVS 简述 HA基础知识 lvs配置 简述 什么是lvs? lvs是linux virtual server linux虚拟服务的缩写,通过一台调度服务器来调度收到的请求并分发给后端的real server。 lvs的功能是什么? lvs能够实现在大并发的情况下,将前端调度器收到的请求分发给后端服务器处理,实现了负载均衡集群的作用。 lb基础知识 lb集群…

    Linux干货 2016-05-31
  • 马哥教育20期面授2班第一周课程练习1

    计算机基础   1,计算机系统           硬件系统            主机部分:中央处理器CPU(运算器ALU、控制器CU);  &nb…

    Linux干货 2016-07-29
  • Mozart的剑(文本处理工具)——壹剑(文本查看cat、more、less)

    壹剑(文本查看cat、more、less) 博客之前突然想起两句话,很有意思的话,在此贴出上句,希望可以与大家分享,不可问度娘,有兴趣的可以试试。 上句:烟锁池塘柳    下句: 上句:因荷而得藕    下句: 1>cat命令是Linux系统下的一个文本输出命令,一般是用于观看某个文件的内容,特点:一次性显示整个…

    2017-07-29
  • 初识Linux

    一,计算机的组成和功能 计算机主要由四个部分组成,内存、缓存、CPU和寄存器。其中缓存及其重要,为CPU和内存之间的缓存,寄存器存放指令,CPU负责执行指令。内存是与CPU进行沟通的桥梁。计算机中所有程序的运行都是在内存中进行的,因此内存的性能对计算机的影响非常大。 二,Linux的主流发行版本 Linux的发行版有数百种之多,其中主流的三种为: Debia…

    Linux干货 2017-12-04
  • 内核编译流程和自动化安装

    内核编译         在特定的情况我们机器上面有些硬件特性需要利用起来,但是我们现成的这个内核没有开启这个功能,那就需要重新编译,把这个功能模块加进来,或者打到vmlinux中的核心文件里面。比如ntfs功能,默认系统没有启用此功能,但是系统是带这个功能的,编译的时候时候可以…

    Linux干货 2016-09-18
  • N22-第六周作业

    1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; :%s@[[:space:]]\+@#&@g    2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符; :%s#…

    Linux干货 2016-09-26