Ansible基本介绍

一、基础知识:

1. 简介

ansible基于python开发,集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。
        真正具有批量部署的是ansible运行的模块,ansible只是一个框架

(1) 连接插件connection plugins: 负责和被监控端实现通信;
        (2) host inventory: 指定操作的主机,是一个配置文件里面定义监控的主机
        (3) 各种模块核心模块、command模块、自定义模块;
        (4) 借助于插件完成记录日志邮件等功能;
        (5) playbook: 剧本执行多个任务时,非必须可以让节点一次性运行多个任务。

2、特性:
        (1) no agents: 不需要在被管理主机上安装任务agent
        (2) no server: 无服务器端,使用时,直接运行命令即可
        (3) modules in any languages: 基于模块工作,可使用任意语言开发模块
        (4) yaml not code:使用yaml语言定制剧本playbook
        (5) ssh by default:基于SSH工作
        (6) strong multi-tier solution: 可实现多级指挥

3、优点:
        (1) 轻量级,无需在客户端安装agent,更新时,只需要在操作机上进行一次更新即可;
        (2) 批量任务可以写成脚本,而且不用分发到远程就可以执行
        (3) 使用python编写,维护简单
        (4) 支持sudo

二、ansible安装

1.1 rpm包安装
        epel源:

[epel]
            name=Extra Packages for Enterprise Linux 6 - $basearch
            baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
            #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
            failovermethod=priority
            enabled=1
            gpgcheck=0
            gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

            [epel-debuginfo]
            name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
            baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug
            #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch
            failovermethod=priority
            enabled=0
            gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
            gpgcheck=0

            [epel-source]
            name=Extra Packages for Enterprise Linux 6 - $basearch - Source
            baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS
            #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch
            failovermethod=priority
            enabled=0
            gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
            gpgcheck=0

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

三、常用模块介绍

copy模块
        目的:把主控本地文件拷贝到远程节点上

[[email protected] ~]# ansible 192.168.118.14 -m copy -a "src=/root/bigfile dest=/tmp"
        192.168.118.14 | SUCCESS => {
            "changed": true,
            "checksum": "8c206a1a87599f532ce68675536f0b1546900d7a",
            "dest": "/tmp/bigfile",
            "gid": 0,
            "group": "root",
            "md5sum": "f1c9645dbc14efddc7d8a322685f26eb",
            "mode": "0644",
            "owner": "root",
            "size": 10485760,
            "src": "/root/.ansible/tmp/ansible-tmp-1467946691.02-193284383894106/source",
            "state": "file",
            "uid": 0
        }

file模块
        目的:更改指定节点上文件的权限、属主和属组

 [[email protected] ~]# ansible 192.168.118.14 -m file -a "dest=/tmp/bigfile mode=777 owner=root group=root"
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "gid": 0,
                "group": "root",
                "mode": "0777",
                "owner": "root",
                "path": "/tmp/bigfile",
                "size": 10485760,
                "state": "file",
                "uid": 0
            }

cron模块
        目的:在指定节点上定义一个计划任务,每三分钟执行一次。

 [[email protected] ~]# ansible all -m cron -a ‘name="Cron job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/bin/ntpdate tiger.sina.com.cn"‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "envs": [],
                "jobs": [
                    "Cron job"
                ]
            }
            192.168.118.13 | SUCCESS => {
                "changed": true,
                "envs": [],
                "jobs": [
                    "Cron job"
                ]
            } 

group模块
        目的:在远程节点上创建一个组名为ansible,gid为2016的组

 [[email protected] ~]# ansible 192.168.118.14 -m group -a "name=ansible gid=2016"
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "gid": 2016,
                "name": "ansible",
                "state": "present",
                "system": false
            }

user模块
        目的:在指定节点上创建一个用户名为ansible,组为ansible的用户

[[email protected] ~]# ansible 192.168.118.14 -m user -a "name=ansible uid=2016 group=ansible state=present"
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "comment": "",
                "createhome": true,
                "group": 2016,
                "home": "/home/ansible",
                "name": "ansible",
                "shell": "/bin/bash",
                "state": "present",
                "system": false,
                "uid": 2016
            }

删除远端节点用户,注意:删除远程用户,但是不会删除该用户的家目录

 [[email protected] ~]# ansible 192.168.118.14 -m user -a "name=ansible state=absent"
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "force": false,
                "name": "ansible",
                "remove": false,
                "state": "absent"
            }    

yum 模块
        目的:在远程节点安装vsftpd

  [[email protected] ~]# ansible 192.168.118.14 -m yum -a ‘name=vsftpd state=present‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "msg": "",
                "rc": 0,
                "results": [
                    "Loaded plugins: fastestmirror\nSetting up Install Process\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:2.2.2-14.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch             Version                  Repository     Size\n================================================================================\nInstalling:\n vsftpd           x86_64           2.2.2-14.el6             yum           152 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 152 k\nInstalled size: 332 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : vsftpd-2.2.2-14.el6.x86_64                                   1/1 \n\r  Verifying  : vsftpd-2.2.2-14.el6.x86_64                                   1/1 \n\nInstalled:\n  vsftpd.x86_64 0:2.2.2-14.el6                                                  \n\nComplete!\n"
                ]
            }

卸载写法:

  [[email protected] ~]# ansible 192.168.118.14 -m yum -a ‘name=vsftpd state=removed‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "msg": "",
                "rc": 0,
                "results": [
                    "Loaded plugins: fastestmirror\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:2.2.2-14.el6 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch             Version                 Repository      Size\n================================================================================\nRemoving:\n vsftpd           x86_64           2.2.2-14.el6            @yum           332 k\n\nTransaction Summary\n================================================================================\nRemove        1 Package(s)\n\nInstalled size: 332 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Erasing    : vsftpd-2.2.2-14.el6.x86_64                                   1/1 \n\r  Verifying  : vsftpd-2.2.2-14.el6.x86_64                                   1/1 \n\nRemoved:\n  vsftpd.x86_64 0:2.2.2-14.el6                                                  \n\nComplete!\n"
                ]
            }

service模块

    启动
            [[email protected] ~]# ansible 192.168.118.14 -m service -a ‘name=vsftpd state=started enabled=yes‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "enabled": true,
                "name": "vsftpd",
                "state": "started"
            }
        停止
            [[email protected] ~]# ansible 192.168.118.14 -m service -a ‘name=vsftpd state=stopped enabled=yes‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "enabled": true,
                "name": "vsftpd",
                "state": "stopped"
            }    

ping模块

            [[email protected] ~]# ansible 192.168.118.14 -m ping
            192.168.118.14 | SUCCESS => {
                "changed": false,
                "ping": "pong"
            }

command模块

            [[email protected] ~]# ansible 192.168.118.14 [-m command] -a ‘w‘    # -m command可以省略就表示使用命名模块
            192.168.118.14 | SUCCESS | rc=0 >>
             14:00:32 up  3:51,  2 users,  load average: 0.00, 0.00, 0.00
            USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT
            root     pts/0    192.168.118.69   18:09    3:29   0.12s  0.12s -bash
            root     pts/1    192.168.118.13   14:00    0.00s  0.04s  0.00s /bin/sh -c LANG

raw模块

主要的用途是在command中添加管道符号

            [[email protected] ~]# ansible 192.168.118.14 -m raw -a ‘hostname | tee‘
            192.168.118.14 | SUCCESS | rc=0 >>
            localhost.localdomain

get_url模块

目的:将http://192.168.118.14/1.png 下载到本地

[[email protected] ~]# ansible 192.168.118.14 -m get_url -a ‘url=http://192.168.118.14/1.png dest=/tmp‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "checksum_dest": null,
                "checksum_src": "ba5cb18463ecfa13cdc0b611c9c10875275d883e",
                "dest": "/tmp/1.png",
                "gid": 0,
                "group": "root",
                "md5sum": "8c0df0b008eb5735dc955171d6d9dd73",
                "mode": "0644",
                "msg": "OK (14987 bytes)",
                "owner": "root",
                "size": 14987,
                "src": "/tmp/tmpY2lqHF",
                "state": "file",
                "uid": 0,
                "url": "http://192.168.118.14/1.png"
            }    

synchronize模块

目的:将主空方目录推送到指定节点/tmp目录下

[[email protected] ~]# ansible 192.168.118.14 -m synchronize -a ‘src=/root/test dest=/tmp/ compress=yes‘
            192.168.118.14 | SUCCESS => {
                "changed": true,
                "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh  -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/test\" \"192.168.118.14:/tmp/\"",
                "msg": ".d..t...... test/\n<f+++++++++ test/abc\n",
                "rc": 0,
                "stdout_lines": [
                    ".d..t...... test/",
                    "<f+++++++++ test/abc"
                ]
            }

转载来源:https://www.cnblogs.com/hukey/p/5660538.html

原文地址:https://www.cnblogs.com/taich-flute/p/ansible.html

时间: 2024-08-24 14:55:56

Ansible基本介绍的相关文章

Ansible基础介绍

Ansible是一种基于python编写的自动化批量部署工具.主要应用于批量部署应用和结合Git.Jenkins进行自动化配置管理. Ansible结构: Ansible(核心程序) 就是ansible的心脏大脑,进行各部分的协调调用. Host Invertory(主机群组) 定义了被管理的Client,例如Client的IP.域名或ssh端口等信息. Playbooks(剧本) 用于给Client执行的任务配置文件 Modules(模块) ansible的内置功能模块,例如yum模块,she

Ansible Playbooks 介绍 和 使用 二

目录 handlers playbook 案例 2 handlers vars 变量 setup facts 变量使用 案例 inventory 中定义变量 案例 条件测试 when 语句 案例 handlers 接上一篇文章 Ansible Playbooks 介绍 和 使用 一 继续说明 用于当关注的资源发生变化时采取一定的操作. notify这个 action可用于在每个play的最后被处罚,这样可以避免多次有改变时每次都执行指定的操作,取而代之,尽在所有的变化发生完成后一次性执行指定的操

自动化运维工具ansible详细介绍

在学习批量管理软件时,首先要明确的知道自己需要什么,网上大神很多,他们都研究到源码上了,写了很多介绍绚丽功能的文档,但其实那些功能基本上我们都用不到,经常被各种文档弄得头脑发晕,此文就是为了简单直白的告诉大家ansible的功能,满足大家的基本需求. 首先确认批量管理我们需要什么:无外乎主机分组管理.实时批量执行命令或脚本.实时批量分发文件或目录.定时同步文件等. 目录 1.      ansible与saltstack对比... 2.      ansible安装... 3.      ans

自动化运维工具ansible简单介绍

一.Ansible介绍Ansible 简单的说是一个配置管理系统(configuration management system).你只需要可以使用 ssh 访问你的服务器或设备就行.它也不同于其他工具,因为它使用推送的方式,而不是像 puppet 等 那样使用拉取安装agent的方式.ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作.? 模块化:调用特定的模块,完成特定任务? 有Paramiko,PyYAML,Jinja2(模板语言)三个关键模块? 支持自定义模块?

ansible模块介绍之ios_facts

一.模块简介 收集运行IOS系统的(此处指思科的ios)的远端设备信息 二.模块参数 auth_pass #特权密码,如果参数authorize=no,则不会检索此密码,如果任务task不指定,则默认使用环境变量ANSIBLE_NET_AUTH_PASS代替 authorize #是否进入特权模式,yes是;no不是.如果任务task不指定,则默认使用环境变量ANSIBLE_NET_AUTHORIZE 代替 gather_subset #限定取的子集范围,可跟范围:all(所有设备信息),con

(3)ansible文件介绍

ansible安装建议使用rpm安装,简单便捷容易上手,作为初学者来说是再好不过的安装方法了,安装了ansible,让我们来了解下他的目录结构,以及配置文件结构. 一. ansible目录结构介绍 shell> rpm -ql ansible | more 目录主要是以下几项/etc/ansible #配置文件目录,主要功能为inventory主机信息配置.ansible工具功能配置:/etc/ansible/ansible.cfg/etc/ansible/hosts/etc/ansible/r

Ansible命令介绍之ansible

前言 官方介绍:针对一组主机定义并运行单个任务“剧本”工具:个人理解ansible 是Ansible-hoc功能的程序入口,即简单临时命令: 命令格式 ansible [group|host] [options] # group:组名,可以使用all来表示所有组与主机 # host:主机名或者主机地址,多主机用逗号隔开 # options:ansible程序选项 选项 常用实例 指定模块来进行操作远程主机 [[email protected] ~]# ansible 192.168.10.170

ansible配置文件介绍及命令介绍

1.ansible的配置文件 [[email protected] ansible]#  rpm -ql ansible |grep etc /etc/ansible /etc/ansible/ansible.cfg /etc/ansible/hosts /etc/ansible/roles ansible.cfg 文件可以设定一些默认值,这样我们就不需要对同样的内容输入很多遍. 我应该把ansible.cfg 文件放到哪里呢 Ansible 按照如下位置和顺序来查找ansible.cfg 文件

ansible模块介绍

命令模块: 1:command模块在远程节点上执行命令: command模块后面紧跟要执行的命令,命令的参数以空格隔开.指定的命令会在所选的节点上执行.命令并不是通过shell执行的,所以并不能使用$HOME等环境变量和一些操作符(<,>,|,&).shell模块可以使用. 1>chdir 在运行命令之前,先切换到指定的目录. [[email protected] ansible]# ansible testhosts -m command -a "ls -l chdi