Cheat Sheet速查表
跳转到主要内容

Ansible 快速入门 速查表

本文根据 Devhints / Ansible quickstart 翻译。代码示例及版本范围沿用原文。

安装 Ansible

$ brew install ansible            # OSX
$ [sudo] apt install ansible      # elsewhere

在大多数 OS 系统中,Ansible 均以软件包形式提供。

参见: 安装

启动项目

~$ mkdir setup
~$ cd setup

为你的 Ansible 文件创建一个文件夹。

参见: 入门

创建文件

清单文件 (Inventory file)

~/setup/hosts

[sites]
127.0.0.1
192.168.0.1
192.168.0.2
192.168.0.3

这是你想要管理的主机列表,按组进行分类。(提示:尝试使用 localhost ansible_connection=local 部署到您的本地机器。)

参见: 清单 (Inventory) 简介

Playbook 自动化脚本

~/setup/playbook.yml

- hosts: 127.0.0.1
  user: root
  tasks:
    - name: install nginx
      apt: pkg=nginx state=present

    - name: start nginx every bootup
      service: name=nginx state=started enabled=yes

    - name: do something in the shell
      shell: echo hello > /tmp/abc.txt

    - name: install bundler
      gem: name=bundler state=latest

参见: 剧本 (Playbook) 简介

运行

运行 ansible-playbook

~/setup$ ls
hosts
playbook.yml

运行剧本 (Playbook)

~/setup$ ansible-playbook -i hosts playbook.yml
PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [127.0.0.1]

TASK: [install nginx] *********************************************************
ok: [127.0.0.1]

TASK: start nginx every bootup] ***********************************************
ok: [127.0.0.1]
...

阅读更多