https://www.devopsschool.com/blog/ansible-playbook-lab-excercise-part-2/
-
-
Save devops-school/39243de4e9f23d9687b3ecfeeecaf3d0 to your computer and use it in GitHub Desktop.
vsanjeevk
commented
Jul 2, 2019
•
---
- 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
...
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
- name: This creates group called deploy
hosts: web
tasks:- name: create group deploy
group: name=deploy
...
- name: create group deploy
- name: This creates user called deploy-user
hosts: web
tasks:- name: create user deploy-user
user: name=deploy-user shell=/bin/bash
...
- name: create user deploy-user
- name: This install httpd
hosts: web
tasks:- name: Install the httpd apps
yum: name=httpd
...
- name: Install the httpd apps
- 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: start the httpd service
- 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='\nWelcome to HP Ansible class
\n'
...
- name: create file index.html
- name: This creates reboots the machines
hosts: web
tasks:- name: reboots the machine
reboot: msg="rebooting now..."
...
- name: reboots the machine
- name: This install git and wget
hosts: web
tasks:- name: install git and wget
yum: name=git,wget state=present
...
- name: install git and wget
- name: This clone to git
hosts: web
tasks:- name: clone to git
git: repo=https://github.com/scmgalaxy/ansible-role-template clone=yes dest=/tmp/test
...
- name: clone to git
[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:
...
::::::::::::::
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: Delete the git service
- 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: Delete the httpd service
- 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: Create a group called deploy1
- 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
...
- name: Delete the wget service
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
...
- name: Create Group1 User Group
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
'
...`
- name: install httpd server
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
.....`
- name: installing git
Clone_Git.yaml
`
- name: Clone Repo
hosts: web
tasks:- name: cloning Repo
git: repo=https://github.com/scmgalaxy/ansible-role-template dest=/home/lab_git_repo
`
- name: cloning Repo
-
name: Create a group
hosts: web
tasks:- name: Create a group for test-deploy
group: name=test-deploy
- name: Create a group for 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 user test for test-deploy group
-
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: Creating a index.html for testing a web service
-
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: Updating a index.htm
-
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: Copy index file to the http directory
-
name: Installing git and wget
hosts: web
tasks:- name: Installing wget and git
yum: name=wget state=present name=git state=present
- name: Installing wget and git
-
name: Start http service on multiple machine
hosts: web
tasks:- name: starting a web
service: name=httpd state=started
...
- name: starting a web
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
- 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
...
- name: create file
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: sudo access
- name: test for playbook 1
hosts: testVM
tasks:- name: group create
group:
name: newgroup
state: present
...
- name: group create
- name: test for playbook 1
hosts: testVM
tasks:- name: assign group to user
user:
name: dummy_new
group: newgroup
shell: /bin/bash
...
- name: assign group to user
- name: test for playbook 1
hosts: testVM
tasks:- name: assign group to user
yum:
name: httpd
state: present
...
- name: assign group to user
- name: test for playbook 1
hosts: testVM
tasks:- name: assign group to user
service:
name: httpd
state: started
...
- name: assign group to user
- 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: assign group to user
- name: test for playbook 1
hosts: testVM
tasks:- name: install git
yum:
name: git
state: present
...
- name: install git
- name: test for playbook 1
hosts: testVM
tasks:- name: repos copy
git:
repo: https://github.com/ansible/ansible-examples.git
dest: /var/github
...
- name: repos copy
-
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"
- 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: install wget
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
...
- name: deploytest group creation
[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
-
name: create a group "group"
hosts: deploy
tasks:- name: create the group
group: name=group
- name: create the 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: create the user
-
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: install the httpd apps
-
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='\nWelcome 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
...
-
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
'"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
[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
'
...
- name: Create directory Swetha
[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
- 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
- name: create file
[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"
-
...
- 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"
...
- ansible_os_family == "RedHat"
- name:
- 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"
...
- name: create directory
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
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"