快速上手 Ansible

Ansible 系列:html

快速上手 Ansible

Ansible 是 Paramiko 开发的一种自动化运维工具,基于模块化工做,集合了众多运维工具的优势,实现了批量系统配置,批量程序部署、批量运行命令等功能。python

Ansible 它自己没有批量部署的能力,真正执行这些操做的是 Ansible 的模块,它只是提供了一种框架。直到目前为止,Ansible 已有 800 多个模块可使用。ios

Ansible 的另外一个优点在于,它不须要在远程主机上安装任何东西,由于它是基于 SSH 来和远程主机进行通讯的。git

安装 Ansible

在 Ansible 里,有两种角色 Control Machine 与 Managed Node,它们经过 SSH 和 Python 进行沟通:github

  • **Control Machine(主控端):**操做 Ansible 的机器,用于操纵 Managed Node
  • **Managed Node(被控端):**被 Ansible 操纵的机器

在通常状况下,咱们只需在 Control Machine 里安装 Ansible 便可,由于 Linux 和 macOS 系统早已预载了 Python 2.5 以上的版本,且支持 SSH 链接。shell

而使用 Ansible 来管理 Window 的话,则须要较多的设置(此处不介绍,可自寻谷歌/百度)。vim

macOS 安装 Ansible

# 请先安装 homebrew,已安装者请略过
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# 安装 Ansible
brew install ansible
复制代码

Linux 安装 Ansible

CentOS(Yum)ruby

# 新增 epel-release 第三方套件来源。
sudo yum install -y epel-release
# 安装 Ansible
sudo yum install -y ansible
复制代码

Ubuntu(Apt)bash

# 安装 add-apt-repository 必要套件
sudo apt-get install -y python-software-properties software-properties-common
# 使用 Ansible 官方的 PPA 套件来源
sudo add-apt-repository -y ppa:ansible/ansible; sudo apt-get update
# 安装 Ansible
sudo apt-get install -y ansible
复制代码

使用 Pip 安装 Ansible

# 一、首先安装 pip
# Debian, Ubuntu
$ sudo apt-get install -y python-pip
# CentOS
$ sudo yum install -y python-pip
# macOS
$ sudo easy_install pip
 # 二、升级 pip
sudo pip install -U pip
# 三、安装 Ansible
sudo pip install ansible
复制代码

Ansible 文件

在安装 Ansible 以后,你能够访问如下 Ansible 文件:框架

  • /usr/bin/ansible :Ansible 命令行工具
  • /usr/bin/ansible-doc :Ansible 帮助文档工具
  • /usr/bin/ansible-playbook :Ansible 剧本执行工具
  • /etc/ansible/ansible.cfg :主配置文件
  • /etc/ansible/hosts :管理的主机清单
  • /etc/ansible/roles :角色存放处

Ansible 操做之 Ad-Hoc Command 和 Playbook

Ad-Hoc Commands

第一种方式是经过向 Ansible 发送 Ad-Hoc Commands(指令)来操做 Managed Node。

以常见的 pingecho 操做为例:

  • ping

    ansible all -m ping
    server1 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    复制代码
  • echo

    ansible all -m command -a "echo Hello World"
    server1 | SUCCESS | rc=0 >>
    Hello World
    复制代码

Playbook

另一种方式即经过 Playbook (剧本)让各个 Managed Node 进行指定的动做(Plays)和任务(Tasks),你能够理解为 Shell Script。

Playbook 支持两种写法 :

  • **YAML:**简单易读
  • **Jinja2:**模板化文件,支持变量、条件、循环语法

在一份 Playbook 中,能够有多个 Play、Task 与 Module:

  • **Play:**目的
  • **Task:**要执行的 Play 这个目的所需作的步骤
  • **Module:**Ansible 所提供的模块来执行各类操做

下面是一个 Hello World 的 Playbook 剧本,剧本后缀名应为 .yml:

- name: say 'hello world'
 hosts: all
 tasks:

 - name: echo 'hello world'
 command: echo 'hello world'
 register: result

 - name: print stdout
 debug:
 msg: ""
复制代码

执行剧本

ansible-playbook hello_world.yml
复制代码

查看 Ansible 的 Modules

模块是 Ansible 的核心,也是操做真正的执行者,只要掌握了如何使用模块就能够快速上手 Ansible,其他都只是延伸使用罢了。

例如咱们常常使用的 Command Modules 模块,你能够进入 Ansible 的 Module 帮助文档中找到它的使用文档。

automate_with_ansible_basic-20.jpg

文档中 Options 选项表会列出模块参数,参数的预设值等信息。

automate_with_ansible_basic-22.jpg

Setup 模块

在使用 Playbook 时,Ansible 会自动执行 Setup Modules 以收集各个 Managed Node 的 facts(系统信息),如 IP、做业系统、CPU 信息等。

咱们也能够经过 Ad-Hoc Commands 指令模式使用 setup,例如:

ansible all -m setup | less

--- 结果以下
server1 | SUCCESS => {
   "ansible_facts": {
       "ansible_all_ipv4_addresses": [
           "172.19.0.2"
       ],
       "ansible_all_ipv6_addresses": [
           "fe80::42:acff:fe13:2"
       ],
       "ansible_architecture": "x86_64",
       "ansible_bios_date": "03/14/2014",
:
复制代码

经过 filter 参数还能够过滤结果

ansible all -m setup -a "filter=ansible_distribution*"

--- 结果以下
server1 | SUCCESS => {
   "ansible_facts": {
       "ansible_distribution": "Ubuntu",
       "ansible_distribution_major_version": "14",
       "ansible_distribution_release": "trusty",
       "ansible_distribution_version": "14.04"
   },
   "changed": false
}
复制代码

一般在 Playbook 中咱们会将主机信息结合条件判断 when 使用,例以下面针对 Debian, Ubuntu, CentOS 安装 Vim 的 playbook:

- name: Setup the vim 
 hosts: all
 become: true
 tasks:

 # Debian, Ubuntu.
   - name: install apt packages
     apt: name=vim state=present
     when: ansible_pkg_mgr == "apt"

 # CentOS.
   - name: install yum packages
     yum: name=vim-minimal state=present
     when: ansible_pkg_mgr == "yum"
复制代码
相关文章
相关标签/搜索