-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,392 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,132 @@ | ||
# Problem Statement | ||
|
||
--- | ||
Nautilus Application development team wants to test the Apache and PHP setup on one of the app servers in Stratos Datacenter. They want the DevOps team to prepare an Ansible playbook to accomplish this task. Below you can find more details about the task. | ||
|
||
There is an inventory file *~/playbooks/inventory* on *jump host*. | ||
|
||
Create a playbook ~*/playbooks/httpd.yml* on *jump host* and perform the following tasks on *App Server 1.* | ||
|
||
a. Install *httpd* and *php* packages (whatever default version is available in yum repo). | ||
|
||
b. Change default document root of Apache to */var/www/html/myroot* in default Apache config */etc/httpd/conf/httpd.conf*. Make sure */var/www/html/myroot* path exists (if not please create the same). | ||
|
||
c. There is a template ~*/playbooks/templates/phpinfo.php.j2* on **jump host**. Copy this template to the Apache document root you created as *phpinfo.php* file and make sure user owner and the group owner for this file is *apache* user. | ||
|
||
d. Start and enable *httpd* service. | ||
|
||
Note: Validation will try to run the playbook using command *ansible-playbook -i inventory httpd.yml*, so please make sure the playbook works this way without passing any extra arguments. | ||
|
||
--- | ||
|
||
## Solution: | ||
|
||
|
||
1. **Navigate to the Playbooks Directory:** | ||
|
||
```bash | ||
cd ~/playbooks | ||
``` | ||
|
||
|
||
|
||
2. **Inspect Inventory File:** | ||
|
||
```bash | ||
cat inventory | ||
``` | ||
|
||
```plaintext | ||
stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony | ||
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve | ||
stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner | ||
``` | ||
|
||
3. **Inspect Templates Directory:** | ||
|
||
```bash | ||
cd templates/ | ||
ls | ||
phpinfo.php.j2 | ||
``` | ||
|
||
4. **Create the Playbook:** | ||
|
||
Create the playbook file `httpd.yml` in the `~/playbooks/` directory with the following content: | ||
|
||
```yaml | ||
- hosts: stapp01 | ||
become: yes | ||
tasks: | ||
- name: Install Packages | ||
package: | ||
name: | ||
- httpd | ||
- php | ||
state: latest | ||
|
||
- name: Create document root | ||
file: | ||
path: /var/www/html/myroot | ||
state: directory | ||
group: apache | ||
owner: apache | ||
mode: '0755' | ||
|
||
- name: Change the document root | ||
replace: | ||
path: /etc/httpd/conf/httpd.conf | ||
regexp: 'DocumentRoot "/var/www/html"' | ||
replace: 'DocumentRoot "/var/www/html/myroot"' | ||
|
||
- name: Copy template phpinfo.php.j2 | ||
ansible.builtin.template: | ||
src: phpinfo.php.j2 | ||
dest: /var/www/html/myroot/phpinfo.php | ||
owner: apache | ||
group: apache | ||
|
||
- name: Start and enable the httpd service | ||
service: | ||
name: httpd | ||
state: started | ||
enabled: true | ||
``` | ||
5. **Execute the Playbook:** | ||
Run the playbook using the following command: | ||
```bash | ||
ansible-playbook -i inventory httpd.yml | ||
``` | ||
|
||
**Validation of Output:** | ||
|
||
Upon successful execution, you should see output indicating that all tasks have completed successfully. The expected output should be: | ||
|
||
```plaintext | ||
PLAY [stapp01] ************************************************************************** | ||
TASK [Gathering Facts] ****************************************************************** | ||
ok: [stapp01] | ||
TASK [Install Packages] ***************************************************************** | ||
ok: [stapp01] | ||
TASK [Create document root] ************************************************************* | ||
changed: [stapp01] | ||
TASK [Change the document root] ********************************************************* | ||
changed: [stapp01] | ||
TASK [Copy template phpinfo.php.j2] ***************************************************** | ||
changed: [stapp01] | ||
TASK [Start and enable the httpd service] *********************************************** | ||
changed: [stapp01] | ||
PLAY RECAP ****************************************************************************** | ||
stapp01 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,134 @@ | ||
# Problem Statement | ||
|
||
--- | ||
One of the Nautilus DevOps team members is working on to develop a role for _httpd_ installation and configuration. Work is almost completed, however there is a requirement to add a jinja2 template for _index.html_ file. Additionally, the relevant task needs to be added inside the role. The inventory file _~/ansible/inventory_ is already present on _jump host_ that can be used. Complete the task as per details mentioned below: | ||
|
||
a. Update _~/ansible/playbook.yml_ playbook to run the _httpd_ role on _App Server 1_. | ||
|
||
b. Create a jinja2 template _index.html.j2_ under _/home/thor/ansible/role/httpd/templates/_ directory and add a line _This file was created using Ansible on <respective server>_ (for example_ This file was created using Ansible on stapp01_ in case of App Server 1). Also please make sure not to hard code the server name inside the template. Instead, use *inventory_hostname* variable to fetch the correct value. | ||
|
||
c. Add a task inside _/home/thor/ansible/role/httpd/tasks/main.yml_ to copy this template on _App Server 1_ under */var/www/html/index.html*. Also make sure that */var/www/html/index.html* file's permissions are *0655*. | ||
|
||
d. The user/group owner of */var/www/html/index.html* file must be respective sudo user of the server (for example *tony* in case of *stapp01*). | ||
|
||
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. | ||
|
||
## Solution: | ||
|
||
|
||
|
||
#### 1. Update the `playbook.yml` to Run the `httpd` Role on App Server 3 | ||
|
||
You need to modify the existing playbook to ensure that it runs the `httpd` role on App Server 3. Open the `playbook.yml` file and update it as follows: | ||
|
||
```yaml | ||
- hosts: all | ||
become: yes | ||
roles: | ||
- role/httpd | ||
``` | ||
Ensure this playbook is in the `~/ansible` directory. | ||
|
||
#### 2. Create a Jinja2 Template for `index.html` | ||
|
||
Create the `index.html.j2` template file under the `~/ansible/role/httpd/templates/` directory. This file will use the `inventory_hostname` variable to dynamically include the server name. | ||
|
||
```bash | ||
mkdir -p ~/ansible/role/httpd/templates | ||
``` | ||
|
||
Create the `index.html.j2` file: | ||
|
||
```bash | ||
vi ~/ansible/role/httpd/templates/index.html.j2 | ||
``` | ||
|
||
Add the following content to `index.html.j2`: | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Welcome</title> | ||
</head> | ||
<body> | ||
<h1>This file was created using Ansible on {{ inventory_hostname }}</h1> | ||
</body> | ||
</html> | ||
``` | ||
|
||
This template uses the `inventory_hostname` variable to fetch the server name dynamically. | ||
|
||
#### 3. Add a Task in `main.yml` to Copy the Template | ||
|
||
You need to add a task to the `main.yml` file within the `httpd` role to copy the `index.html.j2` template to the appropriate location on App Server 3. | ||
|
||
Edit the `~/ansible/role/httpd/tasks/main.yml` file: | ||
|
||
```bash | ||
vi ~/ansible/role/httpd/tasks/main.yml | ||
``` | ||
|
||
Add the following task to `main.yml`: | ||
|
||
```yaml | ||
- name: Copy index.html template to* /var/www/html/index.html* | ||
ansible.builtin.template: | ||
src: index.html.j2 | ||
dest:* /var/www/html/index.html* | ||
owner: "{{ ansible_user }}" | ||
group: "{{ ansible_user }}" | ||
mode: '0655' | ||
when: inventory_hostname == "stapp03" | ||
``` | ||
|
||
|
||
#### 4. Verify the Inventory | ||
|
||
Ensure that your inventory file lists App Server 3 correctly. For example: | ||
|
||
```ini | ||
stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony | ||
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve | ||
stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner | ||
``` | ||
|
||
#### 5. Run the Ansible Playbook | ||
|
||
Execute the playbook using the `ansible-playbook` command: | ||
|
||
```bash | ||
ansible-playbook -i ~/ansible/inventory ~/ansible/playbook.yml | ||
``` | ||
|
||
#### 6. Verify the Output | ||
|
||
``` | ||
PLAY [all] ****************************************************************************** | ||
|
||
TASK [Gathering Facts] ****************************************************************** | ||
ok: [stapp02] | ||
ok: [stapp03] | ||
ok: [stapp01] | ||
|
||
TASK [role/httpd : install the latest version of HTTPD] ********************************* | ||
ok: [stapp02] | ||
ok: [stapp03] | ||
changed: [stapp01] | ||
|
||
TASK [role/httpd : Start service httpd] ************************************************* | ||
ok: [stapp03] | ||
ok: [stapp02] | ||
changed: [stapp01] | ||
|
||
TASK [role/httpd : Copy index.html.j2 on appserver 1] *********************************** | ||
skipping: [stapp02] | ||
skipping: [stapp03] | ||
changed: [stapp01] | ||
|
||
PLAY RECAP ****************************************************************************** | ||
stapp01 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 | ||
stapp02 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 | ||
stapp03 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,128 @@ | ||
# Problem Statement | ||
|
||
--- | ||
The Nautilus DevOps team had a discussion about, how they can train different team members to use Ansible for different automation tasks. There are numerous ways to perform a particular task using Ansible, but we want to utilize each aspect that Ansible offers. The team wants to utilise Ansible's conditionals to perform the following task: | ||
|
||
An *inventory* file is already placed under */home/thor/ansible* directory on *jump host*, with all the Stratos DC app servers included. | ||
|
||
Create a playbook */home/thor/ansible/playbook.yml* and make sure to use Ansible's *when* conditionals statements to perform the below given tasks. | ||
|
||
Copy *blog.txt* file present under */usr/src/dba* directory on *jump host* to *App Server 1* under */opt/dba* directory. Its user and group *owner* must be user tony and its permissions must be *0755* . | ||
|
||
Copy story.txt file present under */usr/src/dba* directory on *jump host* to App Server 2 under */opt/dba* directory. Its user and group *owner* must be user steve and its permissions must be *0755* . | ||
|
||
Copy _media.txt_ file present under */usr/src/dba* directory on *jump host* to App Server 3 under */opt/dba* directory. Its user and group *owner* must be user banner and its permissions must be *0755*. | ||
|
||
NOTE: You can use *ansible_nodename*variable from gathered facts with _when_ condition. Additionally, please make sure you are running the play for all hosts i.e use _- hosts: all_. | ||
|
||
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. | ||
|
||
## Solution: | ||
|
||
|
||
1. **Navigate to the Ansible Directory:** | ||
|
||
Change to the directory where you will create your playbook. | ||
|
||
```bash | ||
cd /home/thor/ansible | ||
``` | ||
|
||
2. **Check the Inventory File:** | ||
|
||
Make sure your inventory file is properly set up. It should list your app servers with their respective details. | ||
|
||
```bash | ||
cat inventory | ||
``` | ||
|
||
You should see entries like: | ||
|
||
``` | ||
stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony | ||
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve | ||
stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner | ||
``` | ||
|
||
3. **Create the Ansible Playbook:** | ||
|
||
Create a new file named `playbook.yml` using your preferred text editor. | ||
|
||
```bash | ||
vi playbook.yml | ||
``` | ||
|
||
4. **Write the Playbook:** | ||
|
||
Add the following content to your `playbook.yml` file. This playbook includes tasks that use the `when` conditional to check the hostname and perform actions accordingly. | ||
|
||
```yaml | ||
- hosts: all | ||
become: yes | ||
tasks: | ||
- name: Copy file blog.txt to stapp01 | ||
ansible.builtin.copy: | ||
src: */usr/src/dba*/blog.txt | ||
dest: */opt/dba*/blog.txt | ||
*owner*: tony | ||
group: tony | ||
mode: '*0755*' | ||
when: inventory_hostname == "stapp01" | ||
|
||
- name: Copy file story.txt to stapp02 | ||
ansible.builtin.copy: | ||
src: */usr/src/dba*/story.txt | ||
dest: */opt/dba*/story.txt | ||
*owner*: steve | ||
group: steve | ||
mode: '*0755*' | ||
when: inventory_hostname == "stapp02" | ||
|
||
- name: Copy file media.txt to stapp03 | ||
ansible.builtin.copy: | ||
src: */usr/src/dba*/media.txt | ||
dest: */opt/dba*/media.txt | ||
*owner*: banner | ||
group: banner | ||
mode: '*0755*' | ||
when: inventory_hostname == "stapp03" | ||
``` | ||
5. **Run the Ansible Playbook:** | ||
Execute the playbook with the `ansible-playbook` command. | ||
|
||
```bash | ||
ansible-playbook -i inventory playbook.yml | ||
``` | ||
|
||
6. **Verify the Output:** | ||
|
||
After running the playbook, check the output to ensure that the tasks were executed correctly. You should see output indicating that files were copied and permissions were set as specified. | ||
|
||
Example output: | ||
|
||
``` | ||
PLAY [all] ****************************************************************************** | ||
|
||
TASK [Gathering Facts] ****************************************************************** | ||
ok: [stapp01] | ||
ok: [stapp02] | ||
ok: [stapp03] | ||
|
||
TASK [Copy file blog.txt to stapp01] **************************************************** | ||
changed: [stapp01] | ||
|
||
TASK [Copy file story.txt to stapp02] *************************************************** | ||
changed: [stapp02] | ||
|
||
TASK [Copy file media.txt to stapp03] *************************************************** | ||
changed: [stapp03] | ||
|
||
PLAY RECAP ****************************************************************************** | ||
stapp01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 | ||
stapp02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 | ||
stapp03 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 | ||
``` | ||
Oops, something went wrong.