Practice Modules
Overview
Note that these are not the exact same questions. You can try to answer them and create a Playbook file in your own machines based on the questions.
Use the expandable answers to compare your solution after you finish each exercise.
1. Create a playbook with a play to Execute a script on a group of web server nodes called "web_nodes". The script is located at /tmp/install_script.sh
Answer
-
name: 'Execute a script on all web server nodes'
hosts: web_nodes
tasks:
-
name: 'Execute a script on all web server nodes'
script: /tmp/install_script.sh
2. Update the playbook below to add a new task to start httpd services on all web nodes. This task should be named 'Start httpd service'.
-
name: 'Execute a script on all web server nodes'
hosts: web_nodes
tasks:
-
name: 'Execute a script'
script: /tmp/install_script.sh
Hint: use the service module
Answer
-
name: 'Execute a script on all web server nodes'
hosts: web_nodes
tasks:
-
name: 'Execute a script'
script: /tmp/install_script.sh
-
name: 'Start httpd service'
service: 'name=httpd state=started'
3. Update the playbook to add a new task in the beginning to add an entry into /etc/resolv.conf file for hosts. The line to be added is nameserver 10.1.250.10
-
name: 'Execute a script on all web server nodes'
hosts: web_nodes
tasks:
-
name: 'Execute a script'
script: /tmp/install_script.sh
-
name: 'Start httpd service'
service:
name: httpd
state: present
Hint: Use the Lineinfile module**
Answer
-
name: 'Execute a script on all web server nodes'
hosts: web_nodes
tasks:
-
name: 'Update entry into /etc/resolv.conf'
lineinfile:
path: /etc/resolv.conf
line: 'nameserver 10.1.250.10'
-
name: 'Execute a script'
script: /tmp/install_script.sh
-
name: 'Start httpd service'
service:
name: httpd
state: present
4. Update the playbook to add a new task at second position (right after adding entry to resolv.conf) to create a new web user.
User details to be used are given below:
- Username: web_user
- uid: 1040
- group: developers
-
name: 'Execute a script on all web server nodes and start httpd service'
hosts: web_nodes
tasks:
-
name: 'Update entry into /etc/resolv.conf'
lineinfile:
path: /etc/resolv.conf
line: 'nameserver 10.1.250.10'
-
name: 'Execute a script'
script: /tmp/install_script.sh
-
name: 'Start httpd service'
service:
name: httpd
state: present
Hint: Use the user module module.
Answer
-
name: 'Execute a script on all web server nodes and start httpd service'
hosts: web_nodes
tasks:
-
name: 'Update entry into /etc/resolv.conf'
lineinfile:
path: /etc/resolv.conf
line: 'nameserver 10.1.250.10'
-
name: 'Create a new user'
user:
name: web_user
uid: 1040
group: developers
-
name: 'Execute a script'
script: /tmp/install_script.sh
-
name: 'Start httpd service'
service:
name: httpd
state: present