-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsibleLineinfileModule
More file actions
81 lines (63 loc) · 2.73 KB
/
Copy pathAnsibleLineinfileModule
File metadata and controls
81 lines (63 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
QUESTION:
The Nautilus DevOps team want to install and set up a simple httpd web server on all app servers in Stratos DC. They also want to deploy a sample web page using Ansible. Therefore, write the required playbook to complete this task as per details mentioned below.
We already have an inventory file under /home/thor/ansible directory on jump host. Write a playbook playbook.yml under /home/thor/ansible directory on jump host itself. Using the playbook perform below given tasks:
Install httpd web server on all app servers, and make sure its service is up and running.
Create a file /var/www/html/index.html with content:
This is a Nautilus sample file, created using Ansible!
Using lineinfile Ansible module add some more content in /var/www/html/index.html file. Below is the content:
Welcome to xFusionCorp Industries!
Also make sure this new line is added at the top of the file.
The /var/www/html/index.html file's user and group owner should be apache on all app servers.
The /var/www/html/index.html file's permissions should be 0644 on all app servers.
Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure the playbook works this way without passing any extra arguments.
---
- hosts: stapp0*
become: yes
name: play1
tasks:
- name: "Install web server"
yum:
name: httpd
state: present
- name: "Start web server"
service:
name: httpd
state: started
- name: lineinlife
lineinfile:
path: /var/www/html/index.html
line: 'Welcome to xFusionCorp Industries!'
insertbefore: 'This is a Nautilus sample file, created using Ansible!'
state: present
create: True
owner: apache
group: apache
mode: 0644
Though the code was combined, but I had to install the first line of text and then later used the "insertbefore" to input the new line of text.
The reason is because if i use the code as stated above, the first text file will not be created but the "line" text will be created.
To remove the text already there:
---
- hosts: stapp0*
become: yes
name: play1
tasks:
- name: "Install web server"
yum:
name: httpd
state: present
- name: "Start web server"
service:
name: httpd
state: started
- name: lineinlife
lineinfile:
path: /var/www/html/index.html
regexp: 'Welcome to xFusionCorp Industries!'
state: absent
owner: apache
group: apache
mode: 0644
The inventory
stapp01 ansible_host=172.16.125.10 ansible_ssh_pass=****** ansible_user=gona
stapp02 ansible_host=172.16.125.11 ansible_ssh_pass=******** ansible_user=acid
stapp03 ansible_host=172.16.125.12 ansible_ssh_pass=******** ansible_user=babna