https://www.devopsschool.com/blog/ansible-playbook-lab-excercise-part-2/
Created
July 2, 2019 06:39
-
-
Save devops-school/39243de4e9f23d9687b3ecfeeecaf3d0 to your computer and use it in GitHub Desktop.
hp-ansible-classroom-adhoc-june-2019-batch1
- 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"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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"
...