• LOGIN
  • No products in the cart.

DevOps on AWS – Ansible PlayBook Exercises – 2

Playbook – 7:

— # Debugging 2
–   hosts: ‘{{ myHosts }}’
    remote_user: ansible
    become: yes
    become_method: sudo
    connection: ssh
    gather_facts: ‘{{ gather }}’
    vars:
        myHosts: centos
        gather: yes
        pkg: telnet
    tasks:
    – shell: cat /etc/ansible/hosts
      register: hello
    – debug: msg=”Hello File is empty”  
      when: hello.stdout == “”

Playbook – 8:

—# Install http server and Deploy the Html File
–  hosts: centos
   remote_user: ansible
   become: yes
   tasks: 
      – name: Install Apache Server
        yum:
            name: httpd
            state: present
        notify:
        – startservice
      – name: create a file if does not exist
        file:
            state: touch
            path: /var/www/html/index.html
      – name: Adding line 
        lineinfile:
             state: present
             path: /var/www/html/index.html
             line: Hostname= {{ansible_hostname}}
   handlers:
    – name: startservice
      service:
         name: httpd
         state: restarted

Playbook – 9:

— # Upload the Shell Script to Remote Host and Run it.
–  hosts: centos
   remote_user: ansible
   become: yes
   become_method: sudo
   connection: ssh
   gather_facts: yes    
   tasks: 
   –  name: Upload a file to the remote host if does not exist
      copy:
               src: /opt/hostname.sh
               dest: /home/ansible/hostname.sh
               mode: 0755
   –  name: If line not there then add it
      lineinfile:
               state: present
               dest: /home/ansible/hostname.sh
               regexp: ‘^HOSTNAME=’
               line: HOSTNAME={{ ansible_hostname}}
   –  name: Run the program
      shell: /home/ansible/hostname.sh
      register: result
   –  debug: msg=”stdout={{ result }}”
        

Playbook – 10:

— # Upload the Shell Script to Remote Host and Run it.
–  hosts: centos
   remote_user: ansible
   become: yes
   become_method: sudo
   connection: ssh
   gather_facts: yes    
   tasks: 
   –  name: Upload a file to the remote host if does not exist
      copy:
               src: /opt/hostname.sh
               dest: /home/ansible/hostname.sh
               mode: 0755
   –  name: If line not there then add it
      lineinfile:
               state: present
               dest: /home/ansible/hostname.sh
               regexp: ‘^HOSTNAME=’
               line: HOSTNAME={{ ansible_hostname}}
   –  name: Run the program
      shell: /home/ansible/hostname.sh
      register: result
   –  debug: msg=”stdout={{ result }}”
       

Playbook – 11:

— # Multiple Tasks on Multiple Servers in Single Playbook
– hosts: centos
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes    
  tasks:
  – name: Install httpd on centos Servers
    yum: pkg=lynx state=installed update_cache=true
  – name: Querying for Telnet Install
    yum: pkg=telnet state=present update_cache=true

– hosts: local
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes    
  tasks:
  – name: Install telnet on Local Server
    yum: pkg=telnet state=installed update_cache=true
  – name: Querying for Httpd Install
    yum: pkg=httpd state=present update_cache=true

Playbook – 12:

— # Multiple Itmes
–  hosts: centos
   become: yes
   tasks: 
   – name: Multiple Items – {{ item }}
     file:
            state: touch
            path: /home/ansible/{{ item }}
     with_items:
     – file_no_1
     – file_no_2
     – file_no_3
   – name: “Install Common Libraries from CentOS mirror”
     yum: state=present name={{ item }}
     with_items:
      – sudo
      – vim
      – gcc
      – gcc-c++
      – autoconf
      – automake
      – libxml2-devel
      – unzip
      – createrepo
      – htop
      – iotop
      – wget
      – tar

February 22, 2021

0 responses on "DevOps on AWS - Ansible PlayBook Exercises - 2"

    All rights reserved.
    error: Alert: Content is protected !!
    X