【Ansible】Playbooks的简单实例编写

2,342 阅读4分钟

1 部署工具比较

Ansible 是一个开源部署工具,使用 Python 语言开发,支持 SSH 协议通信,兼容 Linux 和 Windows 平台,不需要编译,可以模块化部署管理。可以推送 Playbook 进行远程节点的快速部署。适用于中小规模快速部署。

Chef 是使用 Ruby 语言编写的自动化部署工具,使用了 C/S 架构,需要在目标机器上安装客户端,依赖 Git 实现版本控制,Recipe 脚本编写需要编程经验。

Saltstack 使用 Python 语言编写,使用 C/S 架构,可以支持模块化配置管理,需要遵循 YAML 脚本编写规范,适合大规模集群部署。

Ansible 的优势在于它的轻量级,无客户端,调用 SSH 连接工具实现通信。学习成本低,可以快速上手,使用 Playbook 作为核心配置架构,统一的脚本格式批量化部署。它具有完善的模块化扩展,可以支持目前主流的开发场景,具有强大的稳定性和兼容性。

2 Ansible 安装

2.1 centos下两种安装模式对比

  1. Yum 包管理安装:yum -y install ansible,可能和 Python 安装模块产生冲突
  2. Git 源代码安装(推荐):git clone https://github.com/ansible/ansible.git

2.2 Ansible2.5 + Python3.6 安装步骤

  1. 预先安装 Python3.6 版本,下载 Python3.6.5 的压缩包,tar xf 解压缩,设定编译的值:./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-wl, -rpath /usr/local/bin/lib",安装到/usr/local目录下,安装 pip 包管理工具。对pip3.6 进行软链接ln -s /usr/local/bin/pip3.6 /usr/local/pip,之后可以直接使用 pip 命令。 pip 可能会超时报错,指定源可以解决:pip install --index-url https://pypi.douban.com/simple
  2. 安装 virtualenv:pip install virtualenv
  3. 创建 Ansible 账户并安装 Python3.6 版本的 virtualenv 实例
# 创建 ansible 账户
useradd deploy
# 进入 delploy 账户的系统命令行界面
su - deploy
# 创建 virtualenv 实例,用来集成ansible2.5版本
virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
  1. 源码安装 ansible 2.5
cd /home/deploy/.py3-a2.5-env
git clone https://github.com/ansible/ansible.git
cd ansible && git checkout stable-2.5
  1. 加载 Python3.6 virtualenv 环境:source /home/deploy/.py3-a2.5-env/bin/activate
  2. 安装 ansible 依赖包:pip install paramko PyYAML jinja2
  3. 在 Python3.6 虚拟环境下加载 ansible2.5:source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
  4. 验证 ansible2.5:ansible --version

image.png

3 Playbooks 入门和编写规范

3.1 框架和格式

  • inventory/:Server 详细清单目录
    • testenv:具体清单和变量声明文件
  • roles/:roles 任务列表
    • testbox/:testbox 详细任务
      • tasks/
        • main.yml:testbox 主任务文件
  • deploy.yml:Playbook 任务入口文件 inventory/testenv
# Server组列表
[testservers]
# 目标部署服务器主机名
test.example.com
# Server组列表参数
[testservers:vars]
# 目标主机的参数
server_name=test.example.com
user=root
output=/root/test.txt

主任务文件 main.yml,引用inventory/testenv中定义的目标主机参数

# 任务名称
- name: Print server name and user to remote testbox
# 使用 shell 模块执行命令
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"

任务入口文件deploy.yml

# Server 列表
- hosts: "testservers"
# 获取 Server 基本信息
  gather_facts: true
# 目标服务器系统用户指定
  remote_user: root
# 进入roles/testbox任务目录
  roles:
      -testbox

3.2 SSH 免密码密钥认证

  1. Ansible 服务器创建 SSH 本地密钥:ssh-keygen rsa
  2. Ansible 服务器端建立与目标部署机器的密钥认证:ssh-copy-id -i /home/deploy/.ssh/id_rsa/pub root@test.example.com

3.3 执行Playbooks

  1. 部署到 testenv 环境:ansible-playbook -i inventory/testenv ./deploy.yml

image.png 2. 进入root@test.example.com,出现了test.txt,可以获取到参数

image.png

4 Ansible Playbooks 常用模块

4.1 File 模块

在目标主机创建文件或目录,并赋予其系统权限:

-name: create a file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'

4.2 Copy 模块

实现 Ansible 服务端到目标主机的文件传输

-name: copy a file
 file: 'remote_src=no src=roles/testbox//files/foo.sh dest=/root/foo.sh mode=0644 force=yes' 

4.3 Stat 模块

获取远程文件状态信息

- name: check if foo.sh exists
  stat: 'path=/root/foo.sh'
  register: script_stat

4.4 Debug 模块

打印语句到 Ansible 执行输出

- debug: msg=foo.sh exists
  when: script_stat.stat.exists

4.5 Command/Shell 模块

用来执行 Linux 目标主机命令行

- name: run the script
  command: "sh /root/foo.sh"
- name: run the script
  shell: "echo 'test' > /root/test.txt"

4.6 Template 模块

实现 Ansible 服务端到目标主机的 jinja2 模板传送

- name: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2dest=/etc/nginx/nginx.conf

4.7 Packaging 模块

调用目标主机系统包管理工具(yum、apt)进行安装

- name: ensure nginx is at the latest version
  yum: pkg=nginx state=latest

4.8 Service 模块

管理目标主机系统服务

- name: start nginx service
  service: name=nginx state=started

5 实例

在/root下使用touch命令创建一个foo.txt文件,赋予755的权限,属主和属组都是foo。

(.py3-a2.5-env) [deploy@jxd-worker1 tasks]$ cat main.yml 
- name: Print server name and user to remote testbox
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}'> {{ output }}"
- name: create a file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'

image.png

补充

Python 安装 ansible失败

在windows下用pip安装ansible时,发现怎么也安装不上。后来用setup.py的方式安装上了。

  1. 下载ansible的压缩包
  2. 解压压缩包
  3. 打开cmd,进入上面的解压目录中,先执行python setup.py build,再执行python setup.py install
  4. 然后import验证