Loading...
Loading...
Loading...
This guide will walk you through creating a basic Ansible playbook to help you get started with automation. We'll also cover multi-task playbooks to enhance your understanding.
# Ansible Playbook: Getting Started Guide
This guide will walk you through creating a basic Ansible playbook to help you get started with automation. We'll also cover multi-task playbooks to enhance your understanding.
### Step-by-Step Instructions
### 1. Check Your Current Directory
To ensure you're in the correct working directory, run:
```bash
pwd
```
- If you are already in `/etc/ansible/`, proceed to Step 3.
- Otherwise, follow Step 2 to navigate to the appropriate directory.
### 2. Change Directory to Ansible's Default Location
```bash
cd /etc/ansible/
```
This is where Ansible configurations and playbooks are typically stored.
### 3. Create a Directory for Playbooks
```bash
mkdir Playbooks
```
- The `Playbooks` directory will contain all your playbook files.
### 4. List Files and Directories
```bash
ls
```
This ensures your `Playbooks` folder was successfully created.
### 5. Navigate to the Playbooks Directory
```bash
cd Playbooks
```
### 6. Confirm the Path
```bash
pwd
```
You should now be in `/etc/ansible/Playbooks`. This is where you'll create your playbook file. **Note**: You can store playbooks in other locations, but ensure that location is accessible.
### 7. Create Your First Playbook File
You can use any text editor, such as **vi** or **VSCode**. Here, we'll use **vi**.
```bash
sudo vi tushar1_playbook.yml
```
Alternatively, if you prefer to write your playbook in **VSCode**, you can create the file and then paste it into your editor.
**Basic Playbook**:
```yaml
---
- name: tusharplaybook1
hosts: localhost
tasks:
- name: Test Connectivity
ping:
```
- The playbook tests connectivity to the localhost.
### 8. Verify the File Creation
```bash
ls
ls -ltr
```
This confirms that your playbook file has been created.
### 9. Syntax Check the Playbook
Before running the playbook, ensure it is syntactically correct by running:
```bash
ansible-playbook --syntax-check tushar1_playbook.yml
```
- This helps catch any YAML formatting issues.
### 10. Run the Playbook
Now, execute the playbook:
```bash
ansible-playbook tushar1_playbook.yml
```
- If your playbook is stored in a different location, specify the absolute path.
### Expected Result:
```bash
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [tusharplaybook1] *****************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Test Connectivity] ***************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
---
## Multi-task Playbook
You can add multiple tasks to a playbook to perform actions sequentially. Let's add an extra task to print a message.
### Steps to Add Another Task
**Updated Playbook**:
```yaml
---
- name: tusharplaybook1
hosts: localhost
tasks:
- name: Test Connectivity
ping:
- name: Print Output
debug: msg="Ready to go"
```
### 1. Verify the Playbook
Check your updated playbook with:
```bash
ansible-playbook --syntax-check tushar1_playbook.yml
```
### 2. Run the Multi-task Playbook
```bash
ansible-playbook tushar1_playbook.yml
```
### Expected Result:
```bash
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [tusharplaybook1] *****************************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Test Connectivity] ***************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Print Output] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Ready to go"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
The playbook will first test connectivity to the localhost, followed by printing the output message `"Ready to go"`.
---
### Conclusion
This guide provided you with a basic Ansible playbook setup, syntax checking, and running a playbook. We also explored multi-task playbooks to enhance functionality._Status: Work in progress_
1. [Overview](#overview)
You will need to decide where your entity should be located and how it will be structured. This is largely driven by tax considerations, but may also be driven by governance preferences.
This document aims to help you get started with profiling test suites and answers the following questions: which profiles to run first? How do we interpret the results to choose the next steps? Etc.