ansible 安装部署文档

一、简介
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优势,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工做的,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、链接插件connection plugins:负责和被监控端实现通讯;
(2)、host inventory:指定操做的主机,是一个配置文件里面定义监控的主机;
(3)、各类模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可让节点一次性运行多个任务。
二、整体架构
 

 

三、特性
(1)、no agents:不须要在被管控主机上安装任何客户端;
(2)、no server:无服务器端,使用时直接运行命令便可;
(3)、modules in any languages:基于模块工做,可以使用任意语言开发模块;
(4)、yaml,not code:使用yaml语言定制剧本playbook;
(5)、ssh by default:基于SSH工做;
(6)、strong multi-tier solution:可实现多级指挥。
四、优势
(1)、轻量级,无需在客户端安装agent,更新时,只需在操做机上进行一次更新便可;
(2)、批量任务执行能够写成脚本,并且不用分发到远程就能够执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。

五、准备工做css

centos7.5 ansible-managenode

centos7.5 masterpython

centos7.5 node1linux

centos7.5 node2web

关闭防火墙 :systemctl stop firewalld && systemctl disable firewalldshell

禁用selinux :setenforce 0 临时关闭vim

                    sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinuxcentos

下载epel扩展源:yum install -y epel-releaseruby

 


 

六、安装ansible:服务器

yum install ansible -y 

这里默认安装的是ansible2.7

修改配置文件 vim /etc/ansible/hosts 添加主机组

七、ssh免秘钥登陆配置

生成密钥文件:ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ""

与各主机免密登录:ssh-copy-id -i ~/.ssh/id_dsa.pub root@192.168.175.144等

八、测试ansible

ping各主机状态:ansible test -m ping

ansible 经常使用模块介绍

一、Command 模块

    做用:在远程主机运行命令

         示例:ansible web -a "ls /root" #省略模块不写默认为command模块

    注意:该模块不支持一些特殊符号如`"<"', `">"', `"|"',`";"' and `"&"等如需利用这些符号需使用shell模块。

二、shell模块

    做用:在远程主机在shell进程下运行命令,支持shell特性,如管道等。

         示例:ansible web -m shell -a "echo 123456|passwd --stdin wang"

三、copy模块

    做用:在远程主机执行赋值操做文件

         示例:

    (1)        src=  dest=

    (2)        content=   dest= #content生成字符到目标文件

    其余 owner,group, mode (mode#修改权限)

        [root@centos7_1~]# ansible web -m copy -a "src=/etc/fstab dest=/apps/test"

         [root@centos7_1~]# ansible web -m copy -a"content='hello world1' dest=/apps/test"

         [root@centos7_1 ~]# ansible web -m copy-a "content='hello world1' mode=777 dest=/apps/test1"

        

四、cron模块

         做用:主要是用来对定时任务进行调度

         参数:

    name=               任务的描述

    minute=           分

    day=                   天

    weekday=          周

    hour=                 时

    Month                月

    job=                    须要执行的命令,必须状态为present

    state=                状态

            present:建立

            absent:删除       

 

五、fetch模块

    做用:fetches a file from remotenodes         (获取远程节点文件)

 

六、file模块

    做用:sets attributes of files( 设置文件的属性)

    用法:

    (1)    建立连接文件:*path=  src= state=link

    (2)    修改属性 : path=  owner= mode= grup=

    (3)    建立目录:path= state=directory

 

 七、filesystem模块

    做用:Makes file system on block device   可以在块设备上建立文件系统(慎用)

八、hostname模块

    做用:管理主机名称

九、pip模块

     做用:Manages Python librarydependencies.  #管理Python库依赖项。

十、yum模块

    做用:Manages packages with the`yum' package manager  #使用`yum'软件包管理器管理软件包

         参数:

        name=    程序包名称,能够带版本号

       state=        状态

                present,latest   目前/最新版本

                installed  安装

       conf_file:指定yum配置文件

          示例:[root@centos7_1 ~]# ansibleweb -m yum -a "name=httpd state=latest"

 

十一、service 模块

    做用:管理服务

         参数

       name=       服务名称

       state=        状态

                started       启动

                stopped      中止

                            restarted    重启

                            enabled=    [yes|no] 是否随系统启动

                            runlevel=             运行级别

    示例:ansible web -m service -a"name=httpd state=started enabled=yes runlevel=5" #记得针对Centos7就

    不要使用这个模块了。

 

十二、user 模块

    做用:管理用户账号

    参数:

                  name=                 用户名

                  state=                  状态

                  system=   [yes|no]    是否为系统用户

                  shell=  默认shell类型       指定shell

                  uid=                     指定UID

                  home=                 指定家目录

group=                 指定属组

groups=               指定附加组

comment=          描述信息

1三、script模块

执行脚本 (执行的是本地的脚本将其复制到远程在执行)

                   -a “/PATH/TO/SCRIPT_FILE” 会在远程自动给予权限并运行

1四、Template模块

    基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式做为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)下一章节由详细应用介绍。

                   backup=     备份

                   src=            源文件

                   dest=          目标路径

                   owner=      属主

                   group=       主组

                   mode=       权限

下一章节准备 介绍ansible-playbook的使用  敬请期待!!!

相关文章
相关标签/搜索