ansible基础教程

ansible 是一个轻量级的IT自动化工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

特点

  • SSH by default
  • No agents: controlled hosts / devices need no agent sofware
  • No server: any linux machine can do Ansible activities via terminal commands
  • Modules in any languages: the modules can be developed in any languages
  • YAML, not code: using YAML language(标记语言,类XML) to write playbook
  • Strong multi-tier solution:可实现多级指挥

ansible 配置文件

  • ansible.cfg

    • 定义各种通用变量
    • 查找ansible.cfg文件的顺序
      • ANSIBLE_CONFIG环境变量所指定的文件
      • ./ansible.cfg
      • ~/.ansible.cfg
      • /etc/ansible/ansible.cfg
    • 配置举例:
    inventory = /etc/ansible/hosts  #指定inventory文件位置
    

Inventory

Ansible只能管理指定的服务器,在inventory文件中进行配置对应的主机/分组的数据,其格式如下:

--组名(对系统进行分组)
[webservers]
--主机名
foo.example.com
                             
--指定系统的别名 + ssh的用户
jumper ansible_ssh_host=192.168.1.50 ansible_ssh_user=appadmin
        
--01到50,一组相似的hostname
www[01:50].example.com

--给host设定变量,后续playbook中可以使用
host1 http_port=80 maxRequestsPerChild=808

--给group设定变量,应用于组内的所有host
[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com    

--组内组
[southeast:children]
atlanta
raleigh    

Ansible Ad-Hoc 命令

  • 临时执行的命令
ansible <pattern_goes_here[webservers, all, *]> -m <module_name> -a <arguments>
  • 不指定module的话,则默认执行command模块
  • ansible-doc: 获取模块列表,以及模块使用格式
    • ansible-doc [-l] [-s MODULE]
      • -l : 列出支持的核心模块
      • -s MODULE : 查看模块的用法

使用例子:ping主机

ansible -i hosts webservers -m ping --ask-pass -u user
ansible -i hosts all -m ping --ask-pass -u user

输出:

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

host2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Authentication failed.",
    "unreachable": true
}

参数解释

  • -m, --module-name: module name to execute(default=command)
    • -m ping : 执行ping module
  • -a, --args: module arguments
  • -i, --inventory-file: specify inventory host path(default=/etc/ansible/hosts) or comma separated host list.
  • -k, --ask-pass: ask for connection password
  • -u REMOTE_USER, --user=REMOTE_USER: connect as this user (default=None)
  • webservers 表示执行该命令的分组,all 表示inventory中配置的所有主机
  • -l, --limit=SUBSET: further limit selected hosts to an additional pattern,限定组或host来执行playbook
  • -c, --connect: connect type to use (default=smart)
  • --ask-vault-pass: ask for vault password(sudo 模式需要)
  • -b, --become: run operations with become (does not imply password prompting)(使用playbook制定的become_user进行操作)
  • -t TAGS, --tags=TAGS: only run plays and tasks tagged with these values
  • -C, --check: don't make any changes; instead, try to predict some of the changes that may occur

Ansible Playbook

  • Ad-Hoc命令只能执行一些临时性的、简单的命令
  • 实际企业应用需要经过多个步骤,且各个步骤之间存在依赖关系,Ad-Hoc命令无法满足使用需求
  • 使用playbook来定义步骤以及依赖
  • playbook 由yaml编写,让远程主机按照事先编排的机制执行task
---
- hosts: all    #执行tasks的主机,all表示所有
  become: yes   #使用特定用户执行tasks,该参数也可以配置在相应task中。
  become_user: root
  remote_user: username #the user log into machine.
  
  tasks:
    # 每个task都相当于在执行对应模块的功能
    # 每个task感觉都是单次的连接,执行完之后断掉,之前的环境变量设置不会在后续的task中生效
    # 描述task
    - name: copy local file to remote machine
      # 执行对应模块功能
      copy: 
        src: ~/test
        dest: ~/test
        owner: root 
        mode: 0600
      # 命令执行的结果存到变量中,方便后续使用
      register: rsa
      # 设置环境变量
      environment:
        JAVA_HOME: /usr/java/jre1.8.0_51
      # task有失败之后,相同host后续的task不会执行,该参数可在失败后继续执行。
      ignore_errors: yes
      # 给这部分task打上tags,可指定只执行相应tags的task  (命令中添加:-t deploy)
      tags: deploy
      # (call the tasks defined in handlers if module does some changes to the remote host)
      notify:
        - do something
            
    # defines a list of tasks
    handlers:
      - name: do something
        service: test

    - name: task 2
      debug: var={{ host_vars }} # 使用对应host的host_vars变量

  • 例:在几台机子中执行hostname命令,并获取返回值

    • 文件目录:
    test        # inventory文件,配置主机
    test.yml    # playbook
    
    • inventory 配置内容
    [server]
    host1 ansible_ssh_host=1.1.1.1 ansible_ssh_user=appadmin
    host2 ansible_ssh_host=1.1.1.2 ansible_ssh_user=appadmin
    
    • test.yml 内容
    ---
    - hosts: all
      tasks:
        - name: get hostname
          shell: hostname
          register: out
    
        - debug: var=out
    
    • 执行playbook:$ ansible-playbook -i test test.yml,返回内容:
PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [host1]
ok: [host2]

TASK [get hostname] ************************************************************
changed: [host1]
changed: [host2]

TASK [debug] *******************************************************************
ok: [host1] => {
    "out": {
        "changed": true,
        "cmd": "hostname",
        "delta": "0:00:00.003584",
        "end": "2017-02-09 16:05:04.043118",
        "rc": 0,
        "start": "2017-02-09 16:05:04.039534",
        "stderr": "",
        "stdout": "host1.com",
        "stdout_lines": [
            "host1.com"
        ],
        "warnings": []
    }
}
ok: [host2] => {
    "out": {
        "changed": true,
        "cmd": "hostname",
        "delta": "0:00:00.003584",
        "end": "2017-02-09 16:05:04.043118",
        "rc": 0,
        "start": "2017-02-09 16:05:04.039534",
        "stderr": "",
        "stdout": "host2.com",
        "stdout_lines": [
            "host1.com"
        ],
        "warnings": []
    }
}

PLAY RECAP *********************************************************************
# 以下是对应host的task执行情况,ok表示执行成功的task数量,charged表示对host产生修改的task数量。
host1                         : ok=3    changed=1    unreachable=0    failed=0
host2                         : ok=3    changed=1    unreachable=0    failed=0

role 使用

  • playbook 直接调用 task 问题
    • playbook 是需要处理的事情,task 是执行细节,playbook并不关心细节
    • playbook 直接调用task 使task无法复用
    • playbook会越来越长,难维护
  • 将一个或多个task抽象成一个role,隐藏细节,供playbook调用
  • role易于复用,可以从一个已知的文件结构中自动加载vars, tasks, handler。
  • 部分文件结构:
test
test.yml
roles/
    install/
        files/
        templates/
        tasks/
            main.yml  #应用 install 时,优先执行main.yml
        handlers/
        vars/
    deploy/
        files/
        templates/
        tasks/
            main.yml
        handlers/
        vars/
  • playbook内容
---
- hosts: webservers
  roles:
     - install
     - deploy

部分常用模块

  • file: 包含了文件、文件夹、超级链接类的创立、拷贝、移动、删除操作。
  • copy: copy a file on the local box to remote locations. (可以使用 remote_src,使src在远程机子上,2.0 以后的版本适用)
  • fetch: copy files from remote locations to the local box.
  • template: Templates a file out to a remote server.
  • command: Executes a command on a remote node(It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", ";" and "&" will not work)If you want to execute a command securely and predictably, it may be better to use the command module instead.
  • lineinfile: Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression.
  • pause : Pause playbook execution
  • ping : Try to connect to host, verify a usable python and return pong on success. no sense in playbook.
  • shell : Execute commands in nodes.(runs the command through a shell (/bin/sh) on the remote node.)If you want to execute a command securely and predictably, it may be better to use the command module instead.
  • debug : Print statements during execution
  • setup : Gathers facts about remote hosts(默认执行),支持filter。
  • apt : Manages apt-packages
  • service: Controls services on remote hosts
  • fail: Fail with custom message
  • subversion: Deploys a subversion repository.
  • group: Add or remove groups
  • user: Manage user accounts
  • get_url: Downloads files from HTTP, HTTPS, or FTP to node
  • wait_for: Waits for a condition before continuing.(port is open , file is present, and so on.)
  • script: Runs a local script on a remote node after transferring it

实际场景应用

参考:

an-intro-to-network-automation-3-ansible
an-ansible-tutorial
ansible-simple-tutorial

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • ansible 系统架构 ansible简介ansible是新出现的自动化运维工具,ansible是一个配置管理和...
    运维阿文阅读 9,504评论 1 52
  • ###### Ansible总结 ##### 运维工作: 系统安装(物理机、虚拟机)-->程序包安装、配置、服务启...
    二郎5阅读 1,957评论 0 4
  • 作为背锅侠运维工作的基本流程 运维工具的分类 : ansible的模块化: ansible密钥登陆 ansible...
    二郎5阅读 4,083评论 0 10
  • ansible介绍ansible常用模块使用playbooktemplates,模板条件测试和循环迭代roles,...
    哈喽别样阅读 1,370评论 0 3
  • 进手帐坑已经半年左右了,然而还是不怎么会拼贴。买来的胶带不会用,最后被我用来改装盒子 后来知道了bullet...
    木浅月阅读 1,173评论 6 25