DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Ansible - Part 2

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Actual run is below with verbose output

ansible-playbook httpd.yml -vvv
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

  • Desired output

Image description

Now LETS create a users using file and variable declaration

cat vars.yml
user_details:
  - {name: 'user3', uid: 1007}
  - {name: 'user4', uid: 1008}
Enter fullscreen mode Exit fullscreen mode
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 }}"
Enter fullscreen mode Exit fullscreen mode
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

Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

  • Users are created in the TARGET machines

Image description

Important

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

Image description

set
autoindentation
extended tab
tab space
cursor & column

set ai et ts=2 cuc

Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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

Notes

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

Image description

Top comments (0)