Ansible Playbook Lab & Excercise – Part 4

Execution Mode – Remote

  1. Write a Ansible Playbook to create a group called “deploy”

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

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

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

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

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

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

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

  9. Now Merge all Top Playbook into one and run and verify

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
7 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Francisco Jose Montoya
Francisco Jose Montoya
5 months ago

This is the complete playbook


name: complete playbook
  hosts: localhost

  tasks:
  – name: group deploy
    ansible.builtin.group:
      name: deploy
      state: present
  – name: create user
    ansible.builtin.user:
      name: deploy-user
      state: present
      groups: deploy
      shell: “/bin/bash”
name: install se httpd
    ansible.builtin.apt:
      name: apache2
      state: latest
  – name: Start service
    ansible.builtin.service:
      name: apache2
      state: started
  – name: copy file
    ansible.builtin.copy:
      content: “<h1> Hola Mundo </h1>”
      dest: /var/www/html/index.html
  – name: reboot machines
    ansible.builtin.reboot:
      reboot_timeout: 3600
  – name: install packages
    ansible.builtin.apt:
      name: git,wget
      state: latest
  – name: replicate repository
    ansible.builtin.git:
      repo: https://github.com/scmgalaxy/ansible-role-template
      dest: /home/ubuntu/ansible-role-template

Linus Shen
Linus Shen
5 months ago

– name: Linus lab
 hosts: localhost
 become: yes 
 tasks:
  – name: Create a user group
   group:
    name: deploy     
    state: present
  – name : Create user and add to deploygroup
    user:
     name: deploy-user
     group: deploy
     shell: /bin/bash
     home: /home/deploy-user
   – name: install httpd
     yum:
     name: httpd
     state: installed
   – name: Starting a Apache Server
   ansible.builtin.service:
   name: apache2
   state: started
 – name: Copy file with owner and permissions
  ansible.builtin.copy:
   src: index.html
   dest: /var/www/html/index.html
 – name: Install install git
  ansible.builtin.apt:
   name: “git”
   state: present
 – name: Install install git
  ansible.builtin.apt:
   name: “wget”
   state: present
 – name: Git checkout
  ansible.builtin.git:
   repo: ‘https://github.com/scmgalaxy/ansible-role-template’
   dest: /tmp/checkout

Arturo Alvarado Esparza
Arturo Alvarado Esparza
5 months ago

1
– name: create group
 hosts: all

 tasks:
 – name: create group “deployE
  ansible.builtin.group:
   name: deploy
   state: present

2
– name: create user
 hosts: all

 tasks:
 – name: create group “deployE
  ansible.builtin.user:
   name: deploy-user
   state: present
   shell: /bin/bash

3
– name: install package
 hosts: all

 tasks:
 – name: install package httpd
  ansible.builtin.apt:
   name: httpd
   state: present

4
– name: start service httpd
 hosts: all

 tasks:
 – name: Start service httpd
  ansible.builtin.service:
   name: httpd
   state: started

5
– name: create file
 hosts: all

 tasks:
 – name: create file
  ansible.builtin.file:
   dest: /var/www/html/index.html
   state: touch

6
– name: create file
 hosts: all

 tasks
 – name: reboot the machine with all defaults
  ansible.builtin.reboot:

7
– name: install package
 hosts: all
 vars:
  packages:
  – git
  – wget

 tasks:
 – name: install package httpd
  ansible.builtin.apt:
   name: “{{ item }}
   state: present
  with_items: “{{packages}}”  

Sammi
Sammi
5 months ago

1. Write a Ansible Playbook to create a group called “deploy”

– name: Create the “deploy” group
 hosts: localhost
 become: yes

 tasks:
  – name: Ensure the “deploy” group exists
   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 the “deploy-user” and add to the “deploy” group
 hosts: localhost
 become: yes

 tasks:
  – name: Ensure the “deploy” group exists
   group:
    name: deploy
    state: present

  – name: Create the “deploy-user” user
   user:
    name: deploy-user
    group: deploy
    shell: /bin/bash
    state: present

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

– name: Install httpd package
 hosts: localhost
 become: yes

 tasks:
  – name: Install the httpd package
   package:
    name: httpd
    state: present

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

– name: Start and enable the httpd service on RHEL
 hosts: localhost
 become: yes

 tasks:
  – name: Ensure the httpd service is enabled
   service:
    name: httpd
    enabled: yes

  – name: Start the httpd service
   service:
    name: httpd
    state: started

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

– name: Create index.html in /var/www/html
 hosts: localhost
 become: yes

 tasks:
  – name: Create the directory if it doesn’t exist
   file:
    path: /var/www/html
    state: directory

  – name: Create the index.html file
   copy:
    content: |
     <!DOCTYPE html>
     <html>
     <head>
       <title>Dummy Page</title>
     </head>
     <body>
       <h1>This is a dummy HTML page</h1>
     </body>
     </html>
    dest: /var/www/html/index.html

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

– name: Reboot the local machine
 hosts: localhost
 become: yes

 tasks:
  – name: Reboot the machine
   reboot:

Adam
Adam
5 months ago
Write a Ansible Playbook to create a group called “deploy”

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - name: create group
         group: 
           name: deploy
           state: present          
...

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

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - name: create user
         user: 
           name: deploy-user
           group: deploy
           state: present
           shell: /bin/bash
...

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

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - dnf:
         name: httpd
         state: latest
...

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

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - service:
         name: httpd
         state: started
         enabled: true
...


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

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - copy:
         dest: /var/www/html/index.html
         content: foobar 
...

Write a Ansible Playbook to reboot a self machine.

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - reboot:
...

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

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - dnf:
         name:
            - git
            - wget
         state: latest
...


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

---
- name: whatever
  hosts: all
  become: true

  tasks:
     - git:
         name: https://github.com/scmgalaxy/ansible-role-template
         dest: /tmp/role-template
...


shardendu
shardendu
5 months ago

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


– name: Start and Enable the httpd Service
 hosts: rhel
 become: yes 

 tasks:
   – name: httpd install and start
     service:
       name: httpd
       state: started
       enabled: yes

Write a Ansible Playbook to reboot a self machine.


– name: Reboot Managed Node
 hosts: centos
 become: yes

 tasks:
   – name: Reboot the managed node
     reboot:

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


– name: Install Git and Wget
 hosts: centos
 become: yes

 tasks:
   – name: Install Git and Wget
     yum:
       name:
         – git
         – wget
       state: present

   – name: Install Git and Wget on Debian-based systems
     apt:
       name:
         – git
         – wget
       state: present

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 user
 hosts: rhel
 become: yes

 tasks:
   – name: Create group
     group:
       name: deploy
       state: present

   – name: Create user
     user:
       name: deploy-user
       group: deploy
       shell: /bin/bash
     notify: Restart SSH

 handlers:
   – name: Restart SSH
     service:
       name: ssh
       state: restarted

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


– name: Clone Repository
 hosts: rhel
 become: yes

 tasks:
   – name: Clone the GitHub repository
     git:
       repo: https://github.com/scmgalaxy/ansible-role-template.git
       dest: /tmp/scmgalaxy

Jacek Szałęga
Jacek Szałęga
5 months ago
---
- name: Lab excercise
  hosts: localhost


  tasks:
    - name: make sure group exist
      group:
        name: deploy 
        state: present
    - name: make sure user exist 
      user:
        name: deploy-user 
        group: deploy 
        shell: /bin/bash
    - name: make sure latest apache is installed
      apt: 
        name: apache2
        state: latest 
    - name: make sure apache is started
      service:
        name: apache2 
        state: started
    - name: make sure the index html is in place
      copy:
        src: index.html 
        dest: /var/www/html/
    - name:  make sure second html is in place
      copy:
        content: 'second file content' 
        dest: /var/www/html/second.html
    - name: 
      apt: 
        state: present 
        name: 
          - git
          - wget
    - name: Clone repo
      git: 
        repo: https://github.com/scmgalaxy/ansible-role-template 
        dest: /root/ansible-role-template 
        clone: yes 
        update: yes
...
7
0
Would love your thoughts, please comment.x
()
x