OSDN Git Service

Regular updates
[twpd/master.git] / ansible-guide.md
1 ---
2 title: "Ansible quickstart"
3 category: Ansible
4 layout: 2017/sheet
5 description: |
6   A quick guide to getting started with your first Ansible playbook.
7 ---
8
9 ### Install Ansible
10
11 ```bash
12 $ brew install ansible            # OSX
13 $ [sudo] apt install ansible      # elsewhere
14 ```
15
16 Ansible is available as a package in most OS's.
17
18 See: [Installation](http://docs.ansible.com/ansible/latest/intro_installation.html)
19
20 ### Start your project
21
22 ```bash
23 ~$ mkdir setup
24 ~$ cd setup
25 ```
26
27 Make a folder for your Ansible files.
28
29 See: [Getting started](http://docs.ansible.com/ansible/latest/intro_getting_started.html)
30
31 ## Creating your files
32
33 ### Inventory file
34
35 #### ~/setup/hosts
36
37 ```dosini
38 [sites]
39 127.0.0.1
40 192.168.0.1
41 192.168.0.2
42 192.168.0.3
43 ```
44
45 This is a list of hosts you want to manage, grouped into groups. (Hint: try
46 using `localhost ansible_connection=local` to deploy to your local machine.)
47
48 See: [Intro to Inventory](http://docs.ansible.com/ansible/latest/intro_inventory.html)
49
50 ### Playbook
51
52 #### ~/setup/playbook.yml
53
54 ```yaml
55 - hosts: 127.0.0.1
56   user: root
57   tasks:
58     - name: install nginx
59       apt: pkg=nginx state=present
60
61     - name: start nginx every bootup
62       service: name=nginx state=started enabled=yes
63
64     - name: do something in the shell
65       shell: echo hello > /tmp/abc.txt
66
67     - name: install bundler
68       gem: name=bundler state=latest
69 ```
70
71 See: [Intro to Playbooks](http://docs.ansible.com/ansible/latest/playbooks_intro.html)
72
73 ## Running
74
75 ### Running ansible-playbook
76
77 ```
78 ~/setup$ ls
79 hosts
80 playbook.yml
81 ```
82
83 #### Running the playbook
84
85 ```
86 ~/setup$ ansible-playbook -i hosts playbook.yml
87 PLAY [all] ********************************************************************
88
89 GATHERING FACTS ***************************************************************
90 ok: [127.0.0.1]
91
92 TASK: [install nginx] *********************************************************
93 ok: [127.0.0.1]
94
95 TASK: start nginx every bootup] ***********************************************
96 ok: [127.0.0.1]
97 ...
98 ```
99
100 ## Read more
101
102 * [Getting started with Ansible](http://lowendbox.com/blog/getting-started-with-ansible/) _(lowendbox.com)_
103 * [Getting started](http://docs.ansible.com/ansible/latest/intro_getting_started.html) _(docs.ansible.com)_
104 * [Intro to Inventory](http://docs.ansible.com/ansible/latest/intro_inventory.html) _(docs.ansible.com)_
105 * [Intro to Playbooks](http://docs.ansible.com/ansible/latest/playbooks_intro.html) _(docs.ansible.com)_