ansible安装应用及playbook剧本的定义

   在配置集群时,每个节点上有很多相同的配置,如启动服务相同、安装程序、提供相同的配置文件,甚至定制相同的任务计划来定期执行某些操作等。如果你管理的只是三两个节点配置起来还是相当简单的,但是有三五十台,甚至上百台你还要自已一个一个手动来配置,那就太痛苦了。因些我们就需要能实现批量管理的一套组件,ansible就是实现这样管理的工具。

    ansible以它强大功能和简单上手的应用,在2012年获OSS(Open Source Software,开源软件 开放源代码软件)一项大奖,评为前十名,评为最有用的,最著名的,最受欢迎的开源项目,它柔和了很多强大的功能。在配置上柔和了cfengine(配置引擎)(是一种 UNIX 管理工具,其目的是使简单的管理的任务自动化,使困难的任务变得较容易。Cfengine 适用于管理各种环境,从一台主机到上万台主机的机群均可使用)chef、puppet等。。在批量任务执行上实现了Func的功能。能实现多层次,多级别的统一指挥。这从它下面的特性就能看的出来。

ansible的特性:

    Minimal learning curve,auditability

    NO bootstrapping  #不需要复杂的算法

    NO DAG ordering,Fails Fast

    NO agents(other than sshd)-0 resource consumption when not in use#不需要代理,不需要在被管控的主机上安装任意客户端

    NO server#没有服务器端,有需要启动任何服务

    NO additional PKI #不基于PKI工作

    Modules in any language #支持很多语言

    YAML,not code  #支持YAML语言实现据本定制

    SSH by default #默认基于ssh进行工作

    Strong multi-tier solution #可以实现多级指挥



前提准备四台虚拟机,有一台主机要扮演指挥员的角色,它们的地址分别为:

主控制端:

          control.edu.com 172.16.24.100

要控制的三台主机:

           node1.edu.com 172.16.24.6

           node2.edu.com 172.16.24.7

           node3.edu.com 172.16.24.8

所需要的安装包搜狐epel源中有提供,将yum源指向搜狐epel源。

http://mirrors.sohu.com/fedora-epel/6/x86_64/

ansible-1.5-1.el6.noarch.rpm

epel-release-6-8.noarch.rpm

一、ansible 安装

[root@control ~]# yum install ansible -y

#编译安装方式:

# 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

二、实现主机之间的互信

因为ansible连接主机要基于ssh,如果每次都要输密码是不是很痛苦呀,所以实现主机之间的互信就很有必要。

ssh-keygen -t rsa -P ''

ssh-copy-id -i .ssh/id_rsa.pub [email protected]

ssh-copy-id -i .ssh/id_rsa.pub [email protected]

ssh-copy-id -i .ssh/id_rsa.pub [email protected]

三、建立hosts文件
ansible的hosts默认在/etc/ansible/目录中,采用rpm安装的ansible会将该hosts作为范例,其中提示ansible是主机名和ip两种客户端命名格式的

[root@control ~]# vim /etc/ansible/hosts

在未行模式下输入.,$s/^\([^[:space:]#]\)/#/g把没有用的都注释掉,然后添加如下:

[webservers]

node1.edu.com

nose2.edu.com


[dbservers]

node2.edu.com

node3.edu.com

注:如果没有主机名的话这里可以使用ip地址,这里方括号里是自己定义的,只要自己容易看懂,容易记住,定义什么名无所谓。
四、测试一下主机之间是否能够通信:

[root@control ~]# ansible all -a 'date'

node1.edu.com | success | rc=0 >>

Wed Apr 23 14:41:02 CST 2014


node3.edu.com | success | rc=0 >>

Wed Apr 23 14:41:02 CST 2014


node2.edu.com | success | rc=0 >>

Wed Apr 23 14:41:02 CST 2014

五、ansible命令详析:

ansible <host-pattern> [-f forks] [-m module_name] [-a args]

<host-pattern>:指定对哪些主机做操作。

        [-f forks]:指定管理级别,就是一批要管理多少个主机。默认为一批5个。

        [-m module_name]:指定模块名称。

        [-a args]:指定前定所使的模块的参数。

六、ansible模块详析:

ansible <host-pattern> [-f forks] [-m module_name] [-a args]

       <host-pattern>  #指定对哪些主机做操作

       [-f forks]      #指定并行级别,就是一批要管理多少台主机

       [-m module_name]#指定模块名称

       [-a args]       #指定模块参数

1).command模块,这是ansible最常用的模块,默认可省。

例:查看一下所管理的主机web服务运行状态

[root@control ~]# ansible all -m command -a 'service httpd status'

node3.edu.com | FAILED | rc=3 >>

httpd is stopped


node2.edu.com | FAILED | rc=3 >>

httpd is stopped


node1.edu.com | FAILED | rc=3 >>

httpd is stopped

返回的结果是关闭状态,所以要想执行任何shall命令,直接 -m command

2)copy模块的使用

[root@control ~]# ansible-doc -s copy

- name: Copies files to remote locations.

 action: copy

     src= #源文件                  # Local path to a file to copy to the remote server; can be

     directory_mode=               # When doing a recursive copy set the mode for the director

     force=  #覆盖原来的文件              

     dest=  #复制到哪个位置                

     content=               # When used instead of 'src', sets the contents of a file d

     others=                # all arguments accepted by the [file] module also work her

     validate=              # The validation command to run before copying into place.

     backup=                # Create a backup file including the timestamp information

例:把根目录下的anaconda-ks.cfg文件复制到定义为dbservers的两台主机上:

[root@control ~]# ansible dbservers -m copy -a "src=/root/anaconda-ks.cfg dest=/tmp/"

node3.edu.com | success >> {

node2.edu.com | success >> {

查看一下有没有复制过去

[root@control ~]# ansible dbservers -a "ls /tmp/"

wKiom1Nkh6qjQnSOAACeIwMJ0aQ121.jpg

3)cron模块的使用

[root@control ~]# ansible-doc -s cron

- name: Manage cron.d and crontab entries.

 action: cron

     name=                  # Description of a crontab entry.

     hour=                  # Hour when the job should run ( 0-23, *, */2, etc )

     job=                   # The command to execute. Required if state=present.

     cron_file=             # If specified, uses this file in cron.d instead of an indi

     reboot=                # If the job should be run at reboot. This option is deprec

     month=                 # Month of the year the job should run ( 1-12, *, */2, etc

     state=                 # Whether to ensure the job is present or absent.

     special_time=          # Special time specification nickname.

     user=                  # The specific user who's crontab should be modified.

     backup=                # If set, create a backup of the crontab before it is modif

     day=                   # Day of the month the job should run ( 1-31, *, */2, etc )

     minute=                # Minute when the job should run ( 0-59, *, */2, etc )

     weekday=               # Day of the week that the job should run ( 0-7 for Sunday

例:让所有的主机每隔五分钟到时间服务器上同步一下时间

[root@control ~]# ansible all -m cron -a 'name="corn job" minute=*/5 hour=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.0.1"'

查看下一定制的结果

[root@control ~]# ansible all -a "crontab -l"

wKiom1NkiBvwQmnQAADsCgsEyAk426.jpg

4)group模块

[root@control ~]# ansible-doc -s group

- name: Add or remove groups

 action: group

     state=                 # Whether the group should be present or not on the remote host.

     gid=                   # Optional `GID' to set for the group.

     name=                  # Name of the group to manage.

     system=                # If `yes', indicates that the group created is a system group.

例:三台主机上都添加一个mysql组:

[root@control ~]# ansible all -m group -a "gid=306 system=yes name=mysql"


5)yum模块的使用

[root@control ~]# ansible-doc -s yum

action: yum

     state=       指定是安装(`present', `latest'), 还是卸载 remove (`absent')

     disablerepo= 如果有多个yum源可能禁用一个

     name=        要安装的包名(如果只写包名,就安装最新的包,如果想指定安装什么片本的就包名+版本号)

     enablerepo=  要启用的repo

     list=        列表(跟playbook相关)

     disable_gpg_check=  是否开启gpg_check

     conf_file=    可以用其它服务器上的配置文件,而不使用本地的配置文件

例:所有的主机都安装上corosync.

[root@control ~]# ansible all -m yum -a "state=present name=corosync"

查看一下是否安装上了

[root@control ~]# ansible all -a "rpm -q corosync"

wKioL1NkiEPgDX_lAACcCq9XSqs637.jpg

6)service模块

root@control ~]# ansible-doc -s service

- name: Manage services.

 action: service

     state=              现在是什么状态              

     name=               启动的服务名称

     runlevel=           指定在什么级别下启动

     pattern=            指定模式    

     enabled=             开机是否启动

例:设置web服务开机自动启动

[root@control ~]# ansible all -m service -a "state=started name=httpd enabled=yes"

查看一下是否启动了

[root@control ~]# ansible all -a "service httpd status"

wKiom1NkiMyRX-_aAACUxEg-Ppw062.jpg


七、ansible剧本playbook的定义:

  playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,

1)playbook基础组件

Hosts和Users

playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。如上面示例中的

-hosts: webnodes

remote_user: root


不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。


- hosts: webnodes

 remote_user: mageedu

 tasks:

   - name: test connection

     ping:

     remote_user: mageedu

     sudo: yes


2) 任务列表和action

  play的主体部分是task list。task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。


task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。


每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出。


定义task的可以使用“action: module options”或“module: options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也中使用在行首使用几个空白字符进行换行。

tasks:

 - name: make sure apache is running

   service: name=httpd state=running


在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:

tasks:

 - name: disable selinux

   command: /sbin/setenforce 0


如果命令或脚本的退出码不为零,可以使用如下方式替代:

tasks:

 - name: run this command and ignore the result

   shell: /usr/bin/somecommand || /bin/true


或者使用ignore_errors来忽略错误信息:

tasks:

 - name: run this command and ignore the result

   shell: /usr/bin/somecommand

   ignore_errors: True


3) handlers

用于当关注的资源发生变化时采取一定的操作。


“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。

- name: template configuration file

 template: src=template.j2 dest=/etc/foo.conf

 notify:

    - restart memcached

    - restart apache


handler是task列表,这些task与前述的task并没有本质上的不同。

handlers:

   - name: restart memcached

     service:  name=memcached state=restarted

   - name: restart apache

     service: name=apache state=restarted


定义一个剧本内容如下:

     1)确保三个主机上都安装了httpd

     2)把配置文件复制给三台主机

     3)配置文件一旦复制完成要重新启动httpd

-hosts:all

remote_user:root

tasks:

   -name:ensure apache latest version

    yum: state=latest name=httpd

   -name:apache configure file

    copy:src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes

    notify:

      -restart httpd

handlers:

   -name:restart httpd

    service:name=httpd state=restarted













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章