Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devops-school/39243de4e9f23d9687b3ecfeeecaf3d0 to your computer and use it in GitHub Desktop.
Save devops-school/39243de4e9f23d9687b3ecfeeecaf3d0 to your computer and use it in GitHub Desktop.
hp-ansible-classroom-adhoc-june-2019-batch1
@vsanjeevk
Copy link

vsanjeevk commented Jul 2, 2019

Windows exercise

 ansible all -i ec2-13-233-119-65.ap-south-1.compute.amazonaws.com, -m win_file -a "path=\c\hp\sanjeev\ state=directory" -u Administrator -c winrm --ask-pass -e ansible_winrm_server_cert_validation=ignore

ansible all -i ec2-13-233-119-65.ap-south-1.compute.amazonaws.com, -m win_file -a "path=\c\hp\sanjeev\index.html state=touch" -u Administrator -c winrm --ask-pass -e ansible_winrm_server_cert_validation=ignore

 ansible all -i ec2-13-233-119-65.ap-south-1.compute.amazonaws.com, -m win_lineinfile -a "path=\c\hp\sanjeev\index.html line='<html>  <h1> Welcome to HP Ansible class </h1> </html>' state=present" -u Administrator -c winrm --ask-pass -e ansible_winrm_server_cert_validation=ignore

@pitchela
Copy link

pitchela commented Jul 2, 2019

[root@localhost ~]# cat inventory
[webservices]
10.76.137.151 ansible_become=yes ansible_user=tester ansible_password=tester1 ansible_become_pass=tester1
10.76.138.14 ansible_become=yes ansible_user=tester ansible_password=tester1 ansible_become_pass=tester1
#127.0.0.1 ansible_connection=local


[root@localhost ~]# cat one.yaml
---
- name: "Lab exercise 4 - 1"
  hosts: webservices
  tasks:
  - name: Create a group called deploy
    group: name=deploy state=present
...




[root@localhost ~]# cat two.yaml
---
- name: "Lab exercise 4 - 2"
  hosts: webservices
  tasks:
  - name: Create a user called deploy-user which is part of group called deploy
    user: name=deploy-user group=deploy
...



[root@localhost ~]# cat three.yaml
---
- name: "Lab exercise 4 - 3"
  hosts: webservices
  tasks:
  - name: Command to install package named httpd
    yum:  name=httpd state=present
...




[root@localhost ~]# cat four.yaml
---
- name: "Lab exercise 4 - 4"
  hosts: webservices
  tasks:
  - name: Commands to start and enable the service named “httpd”
    service: name=httpd state=started

...




[root@localhost ~]# cat five.yaml
---
- name: "Lab exercise 4 - 5"
  hosts: webservices
  tasks:
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: path=/var/www/html state=directory
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: dest=/var/www/html/index.html mode=600 state=touch
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    lineinfile: dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible classes </h1>  </html> '

...




[root@localhost ~]# cat six.yaml
---
- name: "Lab exercise 4 - 6"
  hosts: webservices
  tasks:
  - name: Command to reboot a machine.
    reboot:

...




[root@localhost ~]# cat seven.yaml
---
- name: "Lab exercise 4 - 7"
  hosts: webservices
  tasks:
  - name: Commands to install a package called “git”, “wget”.
    yum: name=git,wget state=present

...




[root@localhost ~]# cat eight.yaml
---
- name: "Lab exercise 4 - 8"
  hosts: webservices
  tasks:
  - name: Command to create a temp directory to host the repo
    file: path=/tmp/test state=directory
  - name: Command to clone git repo
    git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test
...




[root@localhost ~]# cat nine.yaml
---
- name: "Lab exercise 4 - 9 Merging of all previous commands"
  hosts: webservices
  tasks:
  - name: Create a group called deploy
    group: name=deploy state=present
  - name: Create a user called deploy-user which is part of group called deploy
    user: name=deploy-user group=deploy
  - name: Command to install package named httpd
    yum:  name=httpd state=present
  - name: Commands to start and enable the service named “httpd”
    service: name=httpd state=started
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: path=/var/www/html state=directory
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: dest=/var/www/html/index.html mode=600 state=touch
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    lineinfile: dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible classes </h1>  </html> '
  - name: Commands to install a package called “git”, “wget”.
    yum: name=git,wget state=present
  - name: Command to create a temp directory to host the repo
    file: path=/tmp/test state=directory
  - name: Command to clone git repo
    git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test
  - name: Command to reboot a machine.
    reboot:

...
`

Lab Exercise 5:
Connecting to Windows ARS from Linux Ansible ACS server and creating a directory "Swetha" under C:\hp, creating a file "index.html" with some contents.

[root@localhost ~]#
[root@localhost ~]# cat windows.yaml

  • name: "Lab exercise 5"
    hosts: win
    tasks:
    • name: Create directory Swetha
      win_file: path=C:\hp\Swetha state=directory
    • name: create the file
      win_file: path=C:\hp\Swetha\index.html state=touch
    • name: write the contents of the file
      win_lineinfile: path=C:\hp\Swetha\index.html line='

      Welcome to HPE Ansible classes

      '
      ...

[root@localhost ~]# cat inventory
[win]
ec2-13-233-119-65.ap-south-1.compute.amazonaws.com ansible_user=Administrator ansible_password=WsgPP?V%&JWvp.AwfKcacQp@22I!8QF! ansible_connection=winrm ansible_winrm_transport=basic ansible_winrm_server_cert_validation=ignore

[root@localhost ~]# ansible-playbook -i inventory windows.yaml

@nandiniabbinaholal
Copy link


  • name: create file and copy
    hosts: localhost
    tasks:
    • name: create file
      file: dest=/opt/test.txt mode=600 state=touch
      when: ansible_os_family == "RedHat"
    • name: copy file
      copy: src=/opt/test.txt dest=/var/www/html/test.txt
      when: ansible_os_family == "RedHat"
    • name: copy contents
      copy: dest=/opt/index.html content='

      this is linux redhat

      '
      when: ansible_os_family == "RedHat"
    • name: copy file
      copy: src=/opt/index.html dest=/var/www/html/index.html

@pitchela
Copy link

pitchela commented Jul 2, 2019

[root@localhost ~]# cat inventory
[webservices]
10.76.137.151 ansible_become=yes ansible_user=tester ansible_password=tester1 ansible_become_pass=tester1
10.76.138.14 ansible_become=yes ansible_user=tester ansible_password=tester1 ansible_become_pass=tester1
#127.0.0.1 ansible_connection=local


[root@localhost ~]# cat one.yaml
---
- name: "Lab exercise 4 - 1"
  hosts: webservices
  tasks:
  - name: Create a group called deploy
    group: name=deploy state=present
...




[root@localhost ~]# cat two.yaml
---
- name: "Lab exercise 4 - 2"
  hosts: webservices
  tasks:
  - name: Create a user called deploy-user which is part of group called deploy
    user: name=deploy-user group=deploy
...



[root@localhost ~]# cat three.yaml
---
- name: "Lab exercise 4 - 3"
  hosts: webservices
  tasks:
  - name: Command to install package named httpd
    yum:  name=httpd state=present
...




[root@localhost ~]# cat four.yaml
---
- name: "Lab exercise 4 - 4"
  hosts: webservices
  tasks:
  - name: Commands to start and enable the service named “httpd”
    service: name=httpd state=started

...




[root@localhost ~]# cat five.yaml
---
- name: "Lab exercise 4 - 5"
  hosts: webservices
  tasks:
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: path=/var/www/html state=directory
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: dest=/var/www/html/index.html mode=600 state=touch
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    lineinfile: dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible classes </h1>  </html> '

...




[root@localhost ~]# cat six.yaml
---
- name: "Lab exercise 4 - 6"
  hosts: webservices
  tasks:
  - name: Command to reboot a machine.
    reboot:

...




[root@localhost ~]# cat seven.yaml
---
- name: "Lab exercise 4 - 7"
  hosts: webservices
  tasks:
  - name: Commands to install a package called “git”, “wget”.
    yum: name=git,wget state=present

...




[root@localhost ~]# cat eight.yaml
---
- name: "Lab exercise 4 - 8"
  hosts: webservices
  tasks:
  - name: Command to create a temp directory to host the repo
    file: path=/tmp/test state=directory
  - name: Command to clone git repo
    git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test
...




[root@localhost ~]# cat nine.yaml
---
- name: "Lab exercise 4 - 9 Merging of all previous commands"
  hosts: webservices
  tasks:
  - name: Create a group called deploy
    group: name=deploy state=present
  - name: Create a user called deploy-user which is part of group called deploy
    user: name=deploy-user group=deploy
  - name: Command to install package named httpd
    yum:  name=httpd state=present
  - name: Commands to start and enable the service named “httpd”
    service: name=httpd state=started
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: path=/var/www/html state=directory
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    file: dest=/var/www/html/index.html mode=600 state=touch
  - name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
    lineinfile: dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible classes </h1>  </html> '
  - name: Commands to install a package called “git”, “wget”.
    yum: name=git,wget state=present
  - name: Command to create a temp directory to host the repo
    file: path=/tmp/test state=directory
  - name: Command to clone git repo
    git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test
  - name: Command to reboot a machine.
    reboot:

...

Lab Exercise 6:

Mention "This is windows" in windows OS.
C:\hp\Swetha\index.html

Mention "This is linux" in Linux OS.
/var/www/html/index.html

[root@localhost ~]# ansible-playbook -i inventory cond.yaml

[root@localhost ~]# cat inventory
[both]
ec2-13-233-119-65.ap-south-1.compute.amazonaws.com ansible_user=Administrator ansible_password=WsgPP?V%&JWvp.AwfKcacQp@22I!8QF! ansible_connection=winrm ansible_winrm_transport=basic ansible_winrm_server_cert_validation=ignore
10.76.137.151 ansible_become=yes ansible_user=tester ansible_password=tester1 ansible_become_pass=tester1
10.76.138.14 ansible_become=yes ansible_user=tester ansible_password=tester1 ansible_become_pass=tester1
[root@localhost ~]#

[root@localhost ~]# cat cond.yaml

  • name: "Lab exercise 5"
    hosts: both
    tasks:
    • name: Create directory Swetha
      win_file: path=C:\hp\Swetha state=directory
      when: ansible_os_family != "RedHat"

    • name: create the file
      win_file: path=C:\hp\Swetha\index.html state=touch
      when: ansible_os_family != "RedHat"

    • name: write the contents of the file
      win_lineinfile: path=C:\hp\Swetha\index.html line='

      This is Windows...

      '
      when: ansible_os_family != "RedHat"

    • name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
      file: path=/var/www/html state=directory
      when: ansible_os_family == "RedHat"

    • name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
      file: dest=/var/www/html/index.html mode=600 state=touch
      when: ansible_os_family == "RedHat"

    • name: Commands to create a file called “index.html” in /var/www/html with some dummy html contents.
      lineinfile: dest=/var/www/html/index.html line='

      This is RedHat Linux

      '
      when: ansible_os_family == "RedHat"

...

@Ashu110-8
Copy link


  • name: test for conditional
    hosts: all
    tasks:
    • name:
      win_lineinfile:
      path: C:\hp\Ashutosh\index.html
      line: 'this is window server'
      when:
      • ansible_distribution == "window"
    • name: copy on linux
      lineinfile:
      path: /var/www/html/index.html
      line: 'this is linux'
      when:
      • ansible_os_family == "RedHat"
        ...

@Ahamed-sultan
Copy link


  • name: create a directory and file and append content"
    hosts: deploy
    tasks:
    • name: create directory
      win_file: path=C:\hp\Sultan state=directory
    • name: create a file
      win_file: path=C:\hp\Sultan\index.html state=touch
    • name: adding content to the file
      win_lineinfile: path=C:\hp\Sultan\index.html line='

      Operating system is Red-hat

      '
      when: ansible_facts['os_family'] == "RedHat"
      ...

@neetesh80
Copy link

Lab4 - Windows

---
-  name: Create a file on windows server server
   hosts: windows
   tasks:
     - name: Create a dir
       win_file:
         path: C:\hp\neetesh
         state: directory
     - name: Create a file
       win_file:
         path: C:\hp\neetesh\index.html
         state: touch
     - name: push contents to the file
       win_copy:
         src: /home/tester/index.html
         dest: C:\hp\neetesh\index.html

@neetesh80
Copy link

Lab5- Conditonals

---
-  name: Gather facts for Redhat and windows
   hosts: mix
   tasks:
     - name: Gather windows facts
       win_copy:
         src: /home/tester/index.html
         dest: C:\hp\neetesh\index.html
       when: ansible_distribution == "window"

     - name: Get the linux facts
       lineinfile: dest=/var/www/html/index.html line='<html> <h1> This is Redhat machine </h1>  </html>'
       when: ansible_os_family == "RedHat"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment