Ansible - Part 2
Published: 2023-10-26 04:00:40

### Playbook Using VARIABLES

1. No special characters
2. Avoid CAPS.
3. '_' Allowed
4. Case Sensitive
5. DOT(.) not allowed

> Make sure the **_Ansible_** Folders are outside the /etc

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ji79lv5uzdzmarr8w3g.png)

> **Using Variables - Installation of HTTPD & Firewalld**

- Create the yaml file with below details,

```
cat httpd.yml

---
- name: deploy and start httpd service
  hosts: centos
  become: true
  vars:
    web_pkg: httpd
    firewall_pkg: firewalld
    web_service: httpd
    firewall_service: firewalld
    rule: http

  tasks:
    - name: required packages installed and up to date
      yum:
        name:
          - "{{ web_pkg }}"
          - "{{ firewall_pkg }}"
        state: latest

    - name: The {{ firewall_service }} service is started
      service:
        name: "{{ firewall_service }}"
        enabled: true
        state: started
    - name: The {{ web_service }} service is started
      service:
        name: "{{ web_service }}"
        enabled: true
        state: started
    - name: web content is in place
      copy:
        content: <h1> Devops team </h1>
        dest: /var/www/html/index.html
    - name: firewall port for {{ rule }} opened
      firewalld:
        service: "{{ rule }}"
        permanent: true
        immediate: true
        state: enabled
```

**_become: true  --> run as root
rule --> defined for PORT_**

> **Check whether the playbook is correct , its like to validate before the RUN. This is called DRYRUN.**

```
ansible-playbook httpd.yml --check
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s86wstp53hi2uylqiejp.png)

> **Actual run is below with verbose output**

```
ansible-playbook httpd.yml -vvv
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4jw3hu3pa1c1hw7qxfb.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/176mp2pkjqbqodree18k.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1199q3405q1zzuc7o6rm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w79urcq2fdcmx7w48k8m.png)

- Desired output

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwlkibi8th1kd2t2rlm7.png)

> **Now LETS create a users using file and variable declaration**

```
cat vars.yml
user_details:
  - {name: 'user3', uid: 1007}
  - {name: 'user4', uid: 1008}
```

```
cat users.yml

---
- hosts: centos
  become: true
  vars_files:
    - /home/sathishpy1808/ansible-playbooks/vars.yml
  tasks:
    - name: add several users
      user:
        name: "{{ item.name }}"
        uid: "{{ item.uid }}"
        state: present
      with_items: "{{ user_details }}"
```

```
ansible-playbook users.yml -e @/home/sathishpy1808/ansible-playbooks/vars.yml --check
or
ansible-playbook users.yml --check


ansible-playbook users.yml -e @/home/sathishpy1808/ansible-playbooks/vars.yml -vvv
or
ansible-playbook users.yml -vvv

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzsbbra4b1bvz57jzfsf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr4bss1twuriofkbjt23.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a820w6bwknux1199grn3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrojse8x1zz926aqeahd.png)

- Users are created in the TARGET machines

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61tlrkauxo5kbce2mobz.png)


### Important

1. Indentation is important while writing the YAML file , so you can also set like below,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ehhy56s86iyyv2qkqwy.png)

set 
autoindentation
extended tab
tab space 
cursor & column

```
set ai et ts=2 cuc

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/soq4fzh451gcdpjjaog3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwq1d0mqcumrhkfkn8bl.png)

Hope you are able to see a LINE in shade.

### Sites

This site will help to validate the YAML file,
https://www.yamllint.com/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqnzut2dpjv5l7mx0399.png)

### Notes

1. Facts gathering [TBD]
2. Plugin integration [TBD]
3. Ansible idempotent ???

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wcdvk73zp1b8k6h2lhv.png)