Thursday, September 22, 2022

Ansible Sample Playbook

First Playbook:

---
  - name: Playbook
    hosts: webservers
    become: yes
    become_user: root
    tasks:
      - name: ensure apache is at the latest version
        yum:
          name: httpd
          state: latest
      - name: ensure apache is running
        service:
          name: httpd
          state: started



name Name of the playbook


hosts A set of hosts usually grouped together as a host group and defined in inventory file


become To tell ansible this play has to be executed with elevated privileges


become_user the user name that we want to switch to like compare it with sudo su - user


tasks set of tasks to execute, All tasks would be defined below this


and then we have two tasks with two modules, the first module is yum and the second module is service


 become_user is something similar to su , and become means something like sudo su or "perform all commands as a sudo user".



Ansible Playbook examples


  1. Write a Ansible Playbook to create a group called “deploy”
- name: Create a group
  hosts: group
  tasks:
   - name: Add group "deploy"
     group:
       name: deploy
       state: present

2.Write a Ansible Playbook to create a user called “deploy-user” which is part of group called “deploy” and with /bin/bash shell.

- name: Create a user
  hosts: user
  tasks:
    - name: Add user "deploy-user"
      user: 
        name=deploy-user groups=deploy password="" shell=/bin/bash 
        append=yes 
        state=present

3. Write a Ansible Playbook to install package named “httpd” in RHEL/centos.

- name: To install  an httpd webserver
  hosts: web
  tasks:
  - name: Install httpd packages 
    yum:
      name: httpd
      state: present

4. Write a Ansible Playbook to start and enable the service named “httpd”

- name: To start and enable an httpd webserver
  hosts: web
  tasks
    - name: start httpd
    service:
      name: httpd 
      state: started


  - name: enable httpd
    service:
      name: httpd 
      enabled: yes

5. Write a Ansible Playbook to create a file called “index.html” in /var/www/html with some dummy html contents.

- name: creating a file
  hosts: all
    tasks:
    - name: create a file index.html
      file:
        path: "/var/www/htmlindex.html"
        state: touch

    - name: adding contents to indexfile
    blockinfile:
      dest: /var/www/html/index.html
      block: |
        <html>
        <h1> Welcome to HP Ansible class </h1>
        </html>

6. Write a Ansible Playbook to reboot a self machine.

- name: rebooting self machine
  hosts: web
    task:
    - name: Reboot a Linux machine 
        reboot:
        reboot_timeout: 1800

7. Write a Ansible Playbook to install a package called “git”, “wget”.

- name: To install a package "git" and "wget"
  hosts: web
  tasks:
  - name: Install git packages 
    yum:
      name: git
      state: present
 
  - name: Install wget packages 
    yum:
      name: wget
      state: present

8. Write a Ansible Playbook to clone git repo. thttps://github.com/scmgalaxy/ansible-role-template

- name: Cloning git repo
  hosts: web
  environment:
      http_proxy: 'http://web-proxy:8080'
      https_proxy: 'http://web-proxy:8080'
  tasks:
  - name: cloning....
    git: 
      repo=https://github.com/scmgalaxy/ansible-role-template
      clone=yes 
      dest=./ansible-role-template

No comments:

Post a Comment

Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117