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

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

- name: Creating deploy user and group
  hosts: web
  tasks:
  - name: Add group "deploy"
    group: name=deploy
    become: true

  - name: Add user "deploy-user"
    user: name=deploy-user groups=deploy password="" shell=/bin/bash append=yes  state=present
    become: true

# Write a Ansible Playbook to install package named “httpd” in RHEL/centos.
# Write a Ansible Playbook to start and enable the service named “httpd”

- name: Deploying a webserver and start it
  hosts: web
  tasks:
  - name: Deploying a webserver
    yum: name=httpd state=present
  - name: starting the httpd server
    service: name=httpd state=started

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

- name: create an index.html for the webserver.
  hosts: web
  tasks:
  - name: touch the index file
    file:
      path: /var/www/html/index.html
      mode: 644
      state: touch

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

# Write a Ansible Playbook to reboot a self machine.
- name: Rebooting the hosts
  hosts: web
  tasks:
  - name: Rebooting now.
    reboot:

# Write a Ansible Playbook to install a package called git, wget.
- name: Installing git and wget.
  hosts: web
  tasks:
  - name: Installing .....
    yum: name={{item}} state=present
    with_items:
     - wget
     - git
# Write a Ansible Playbook to clone git repo. thttps://github.com/scmgalaxy/ansible-role-template
- name: Cloning git repo https://github.com/scmgalaxy/ansible-role-template
  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


...

@chinnigit
Copy link

---
- name: This is for eight steps exersise
  vars:  
    ansible_user: user1
    ansible_password: user1
  hosts: web
  tasks:
  - name: Create a Group
    group: name=deploy
  - name: Create a User and assign to Group
    user: name=deploy-user group=deploy shell=/bin/bash state=present
  - name: Install the httpd apps
    yum: name=httpd
  - name: start the httpd service
    service: name=httpd state=started
  - name: Copy HTML Content to a file
    copy: dest=/var/www/html/index.html content='<html><h1>Welcome to HPE</h1></html>'
  - name: Install GIT
    yum: name=git state=present
  - name: Install WGET
    yum: name=wget state=present
  - name: Clone Code From GIT
    git: dest=/home/harish/GitRepo1 repo=https://github.com/scmgalaxy/ansible-role-template  
...

@neetesh80
Copy link

Lab4 - Playbooks


- import_playbook: user.yaml
- import_playbook: web.yaml
- import_playbook: web_contents.yaml
#- import_playbook: reboot.yaml
- import_playbook: pkg.yaml
- import_playbook: repo.yaml
-  name: Create a user and add it to the group
   hosts: web
   tasks:
     - name: Create a group
       group: name=deploy state=present
     - name: Create a user and add to group
       user: name=deploy group=deploy
-  name: Setup a web server
   hosts: web
   tasks:
     - name: Install Apache
       yum: name=httpd
     - name: Start Apache server
       service: name=httpd state=started
-  name: Write contents to a file on webserver
   hosts: web
   tasks:
     - name: Create a directory
       file: path=/var/www/html state=directory
     - name: Create a file
       file: dest=/var/www/html/index.html mode=600 state=touch
     - name: Write contents to the file
       lineinfile: dest=/var/www/html/index.html line='<html> <h1> Welcome to HP Ansible class </h1>  </html>'
-  name: Reboot the Apache server
   hosts: web
   tasks:
     - name: Restart the httpd server
       reboot:
---
-  name: Install git and wget packages
   hosts: web
   tasks:
     - name: Install git, wget packages
       yum: name=git,wget state=present
-  name: Clone the repo
   hosts: web
   tasks:
     - name: Create a temp directory to host repo
       file: path=/tmp/test state=directory
     - name: Clone the repo using url
       git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test

@hemananth91
Copy link


  • name: This creates group called deploy
    hosts: web
    tasks:
    • name: create group deploy
      group: name=deploy
      ...

  • name: This creates user called deploy-user
    hosts: web
    tasks:
    • name: create user deploy-user
      user: name=deploy-user shell=/bin/bash
      ...

  • name: This install httpd
    hosts: web
    tasks:
    • name: Install the httpd apps
      yum: name=httpd
      ...

  • name: This enable and start httpd webserver
    hosts: web
    tasks:
    • name: start the httpd service
      service: name=httpd state=enable
      service: name=httpd state=started
      ...
  • name: This creates file named index.html
    hosts: web
    tasks:
    • name: create file index.html
      file: dest=/var/www/html/index.html mode=755 state=touch owner=root
    • name: add content to a file
      lineinfile: dest=/var/www/html/index.html line='\n

      Welcome to HP Ansible class

      \n'
      ...

  • name: This creates reboots the machines
    hosts: web
    tasks:
    • name: reboots the machine
      reboot: msg="rebooting now..."
      ...

  • name: This install git and wget
    hosts: web
    tasks:
    • name: install git and wget
      yum: name=git,wget state=present
      ...

@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:

...



@manju0821
Copy link

::::::::::::::
git_playbook.yml
::::::::::::::

  • name: This sets up an httpd webserver
    hosts: localhost
    tasks:
    • name: Delete the git service
      yum: name=git state=removed
    • name: Install the git service
      yum: name=git state=present
      ...
      ::::::::::::::
      httpd_playbook.yml
      ::::::::::::::

  • name: This sets up an httpd webserver
    hosts: localhost
    tasks:
    • name: Delete the httpd service
      yum: name=httpd state=removed
    • name: Install the httpd service
      yum: name=httpd state=present
    • name: Start the httpd service
      service: name=httpd state=started
    • name: Change the content of the index.html
      copy: dest=/var/www/html/index.html content="

      Welcome to HP Ansible class Excercise-5

      "
      ...
      ::::::::::::::
      user_playbook.yml
      ::::::::::::::

  • name: This sets up an user and hte group
    hosts: localhost
    tasks:
    • name: Create a group called deploy1
      group: name=deploy1
    • name: Create a user called deploy1-user under the group called deploy1
      user: name=deploy1-user shell=/bin/bash group=deploy1
      ...
      ::::::::::::::
      wget_playbook.yml
      ::::::::::::::

  • name: This sets up an httpd webserver
    hosts: localhost
    tasks:
    • name: Delete the wget service
      yum: name=wget state=removed
    • name: Install the wget service
      yum: name=wget state=present
      ...

@raghvendrasawara
Copy link

ansible-playbook -i inventory_for_create_user createuser.yaml ansible-playbook -i inventory install_httpd.yaml ansible-playbook -i inventory install_WGET_GIT.yaml ansible-playbook -i inventory Clone_Git.yaml

Inventory_for_create_user
[web] 10.76.137.86 ansible_user=root ansible_password=iso*help 10.76.143.93 ansible_user=root ansible_password=iso*help 127.0.0.1 ansible_connection=local

Inventory
[web] 10.76.137.86 ansible_user=inventory_user ansible_password=inventory_user ansible_password=yes ansible_password=inventory_user ansible_password=inventory_user 10.76.143.93 ansible_user=inventory_user ansible_password=inventory_user ansible_password=yes ansible_password=inventory_user ansible_password=inventory_user 127.0.0.1 ansible_connection=local

createuser.yaml
`---

  • name: Create Usergroup and User
    hosts: web
    tasks:
    • name: Create Group1 User Group
      group: name=Group1 state=present
    • name: Create groupuser1 user
      user: name=groupuser1 group=Group1 shell='/bin/bash' state=present
      ...

install_httpd.yaml
`---

  • name: install and start httpd server
    hosts: web
    tasks:
    • name: install httpd server
      yum: name=httpd state=present
    • name: start httpd server
      service: name=httpd state=started
    • name: modify index.html file
      copy: dest=/var/www/html/index.html content='

      Welcome to HPE

      '
      ...`

install_WGET_GIT.yaml
`---

  • name: install wget and git
    hosts: web
    tasks:
    • name: installing git
      yum: name=git state=present
    • name: installing wget
      yum: name=wget state=present
      .....`

Clone_Git.yaml
`

@prakashjawari
Copy link


  • name: Create a group
    hosts: web
    tasks:

    • name: Create a group for test-deploy
      group: name=test-deploy
  • name: Creating a User for group test-deploy
    hosts: web
    tasks:

    • name: Creating a user test for test-deploy group
      user: name=test group=test-deploy shell=/bin/bash state=present
  • name: Creating a file index.xml from user test
    hosts: web
    tasks:

    • name: Creating a index.html for testing a web service
      file: dest=/opt/index.html mode=755 state=touch
  • name: Updating a index.html
    hosts: web
    tasks:

    • name: Updating a index.htm
      lineinfile: state=present dest=/opt/index.html line='

      Welcome to HP Ansible class

      '
  • name: Copy Index file to the /var/www/html
    hosts: web
    tasks:

    • name: Copy index file to the http directory
      copy: src=/opt/index.html dest=/var/www/html
  • name: Installing git and wget
    hosts: web
    tasks:

    • name: Installing wget and git
      yum: name=wget state=present name=git state=present
  • name: Start http service on multiple machine
    hosts: web
    tasks:

    • name: starting a web
      service: name=httpd state=started
      ...

@nandiniabbinaholal
Copy link

playbook exercise:
name: create group and user
hosts: localhost
tasks:

name: create group
group: name=deploy1 state=present
name: create user
user: name=deploy-user1 group=deploy1 state=present
name: This sets up git
hosts: localhost
tasks:

name: intall git
yum: name=git state=present
name: install wget
yum: name=wget state=present
name: clone git repo
git: repo=https://github.com/scmgalaxy/ansible-role-template dest=/src/ansible-examples separate_git_dir=/src/ansible-examples.gitstate=present
name: This sets up an httpd webserver
hosts: localhost
tasks:

name: Install the httpd apps
yum: name=httpd
name: start the httpd service
service: name=httpd state=started
name: create file and copy
hosts: localhost
tasks:

name: create file
file: dest=/opt/index.html mode=600 state=touch
name: copy contents
copy: dest=/opt/index.html content='
Welcome to HP Ansible class
'
name: copy file
copy: src=/opt/index.html dest=/var/www/html/index.html

@BhanuPrakashP
Copy link


  • name: something1
    hosts: web
    tasks:
    • name: create file
      file: dest=/opt/b123.txt mode=600 state=touch owner=root
    • name: start the httpd service
      service: name=httpd state=started enabled=yes
    • name: create package wget
      package: name=wget state=present
    • name: create group
      group: name=deploy2 state=present
    • name: create user
      user: name=deploy-user2 shell=/bin/bash state=present groups=deploy2 append=yes
    • name: create file
      file: dest=/opt/index99.html mode=755 state=touch owner=root
      ...

@Ashu110-8
Copy link


name: test for playbook 1
hosts: testVM
tasks:
- name: user createtion
user: dummy_new
shell=/bin/bash
...


  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: sudo access
      lineinfile:
      path: /etc/sudoers
      regexp: '^dummy_new'
      line: 'dummy_new ALL=(ALL) NOPASSWD: ALL'
      ...

  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: group create
      group:
      name: newgroup
      state: present
      ...

  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: assign group to user
      user:
      name: dummy_new
      group: newgroup
      shell: /bin/bash
      ...

  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: assign group to user
      yum:
      name: httpd
      state: present
      ...

  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: assign group to user
      service:
      name: httpd
      state: started
      ...

  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: assign group to user
      copy:
      dest: /var/www/html/index.html
      content: 'this HPe content'
      ...

  • name: test for playbook 1
    hosts: testVM
    tasks:
    • name: install git
      yum:
      name: git
      state: present
      ...

@mail2maiya
Copy link


  • hosts: localhost
    name: "create user"
    tasks:

    name: "creating user"
    user: name=deploy-user1 group=deploy1 state=present
    

...


name: Delete the httpd service
yum: name=httpd state=removed
name: Install the httpd service
yum: name=httpd state=present
name: Start the httpd service
service: name=httpd state=started
name: Change the content of the index.html
copy: dest=/var/www/html/index.html content="Welcome to HP Ansible class Excercise-5"

@mail2maiya
Copy link

  • name: intall git
    yum: name=git state=present

@muralidharbm1411
Copy link

muralidharbm1411 commented Jul 2, 2019

cat combined.yaml

  • name: second day
    hosts: test
    tasks:
    • name: deploytest group creation
      group: name=deploytest state=present
    • name: deploytest user creation
      user: name=deploytest group=deploytest shell=/bin/bash
    • name: package installation
      yum: name=git state=latest
    • name: service starting
      service: name=httpd state=started
    • name: file creation
      file: dest=/var/www/html/index1.html state=touch owner=root mode=755
    • name: package installation
      yum: name=unzip state=latest
    • name: git
      git: repo=https://github.com/scmgalaxy/ansible-role-template dest=/home/murali clone=yes
      ...

[root@see1 ansible]# cat inventory
[test]
127.0.0.1 ansible_connection=local
15.213.56.127 ansible_user=root ansible_become=yes
15.213.56.128 ansible_user=root ansible_become=yes

[root@see1 ansible]#
[root@see1 ansible]# ansible-playbook -i inventory combined.yaml

@Ahamed-sultan
Copy link


  • name: create a group "group"
    hosts: deploy
    tasks:

    • name: create the group
      group: name=group
  • name: create a user called “deploy-user” which is part of group called “group” and with /bin/bash shell
    hosts: deploy
    tasks:

    • name: create the user
      user: name=deploy-user group=group shell=/bin/bash
  • name: this is setup an httpd webserver
    hosts: deploy
    tasks:

    • name: install the httpd apps
      yum: name=httpd
    • name: start the httpd service
      service: name=httpd state=started
  • name: create a file called “index.html” in /var/www/html with some dummy html contents
    hosts: deploy
    tasks:

    • name: create a directory
      file: path=/home/Ahamed/ state=directory

    • name: create a file
      file: dest=/home/Ahamed/index.html mode=644 state=touch

    • name: adding content to the index.html
      lineinfile: dest=/home/Ahamed/index.html line='\n

      Welcome to HP Ansible class

      \n'

    • name: copy index.html to /var/www/html
      copy: src=/home/Ahamed/index.html dest=/var/www/html/

    • name: install a package called “git”, “wget”
      yum: name=git

    • name: install a package called "wget"
      yum: name=wget

    • name: clone git repo. https://github.com/scmgalaxy/ansible-role-template
      git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/home/Ahamed/git
      ...

@Ashu110-8
Copy link

ansible window -i inventory -m win_file -a "path=C:\hp\Ashutosh state=directory"

ansible window -i inventory -m win_lineinfile -a "path=C:\hp\Ashutosh\index.html line='

Welcome to HP Ansible

'"

@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