cheatsheets/ansible-guide.md

107 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2014-02-25 10:32:14 +00:00
---
title: "Ansible quickstart"
2015-11-24 04:32:36 +00:00
category: Ansible
description: |
A quick guide to getting started with your first Ansible playbook.
2014-02-25 10:32:14 +00:00
---
### Install Ansible
```bash
2014-02-25 10:32:14 +00:00
$ brew install ansible # OSX
$ [sudo] apt install ansible # elsewhere
2014-03-26 12:44:55 +00:00
```
2014-02-25 10:32:14 +00:00
Ansible is available as a package in most OS's.
See: [Installation](http://docs.ansible.com/ansible/latest/intro_installation.html)
2014-02-25 10:32:14 +00:00
### Start your project
```bash
2014-02-25 10:32:14 +00:00
~$ mkdir setup
~$ cd setup
2014-03-26 12:44:55 +00:00
```
2014-02-25 10:32:14 +00:00
Make a folder for your Ansible files.
2014-02-25 10:32:14 +00:00
See: [Getting started](http://docs.ansible.com/ansible/latest/intro_getting_started.html)
## Creating your files
### Inventory file
#### ~/setup/hosts
2014-02-25 10:32:14 +00:00
2014-03-26 12:44:55 +00:00
```dosini
2014-02-25 10:32:14 +00:00
[sites]
127.0.0.1
192.168.0.1
192.168.0.2
192.168.0.3
2014-03-26 12:44:55 +00:00
```
2014-02-25 10:32:14 +00:00
This is a list of hosts you want to manage, grouped into groups. (Hint: try
using `localhost ansible_connection=local` to deploy to your local machine.)
See: [Intro to Inventory](http://docs.ansible.com/ansible/latest/intro_inventory.html)
2014-02-25 10:32:14 +00:00
### Playbook
2014-02-25 10:32:14 +00:00
#### ~/setup/playbook.yml
```yaml
2014-02-25 10:32:14 +00:00
- 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
2014-03-26 12:44:55 +00:00
```
2014-02-25 10:32:14 +00:00
See: [Intro to Playbooks](http://docs.ansible.com/ansible/latest/playbooks_intro.html)
2014-02-25 10:32:14 +00:00
## Running
2014-02-25 10:32:14 +00:00
### Running ansible-playbook
2014-02-25 10:32:14 +00:00
```
~/setup$ ls
hosts
playbook.yml
```
2014-02-25 10:32:14 +00:00
#### Running the playbook
2014-02-25 10:32:14 +00:00
```
~/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]
...
```
2014-02-25 10:32:14 +00:00
## Read more
2014-02-25 10:32:14 +00:00
* [Getting started with Ansible](http://lowendbox.com/blog/getting-started-with-ansible/) _(lowendbox.com)_
* [Getting started](http://docs.ansible.com/ansible/latest/intro_getting_started.html) _(docs.ansible.com)_
* [Intro to Inventory](http://docs.ansible.com/ansible/latest/intro_inventory.html) _(docs.ansible.com)_
* [Intro to Playbooks](http://docs.ansible.com/ansible/latest/playbooks_intro.html) _(docs.ansible.com)_
2022-05-07 22:08:26 +00:00
* [Ansible Tutorial for Beginners: Playbook & Examples](https://spacelift.io/blog/ansible-tutorial) _(spacelift.io)_
* [Working with Ansible Playbooks Tips & Tricks with Examples](https://spacelift.io/blog/ansible-playbooks) _(spacelift.io)_