Ansible variable lab exercise
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
1. Create a playbook and setup a webserver(httpd) and use httpd as a variable decalared in role var file
---
- name: Setup web server
hosts: web
vars_files:
- ext-vars.yaml
tasks:
- name: Install httpd
yum:
name={{ packagename }}
state=present
- name: Copy file with owner and permissions
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Start service httpd, if not started
ansible.builtin.service:
name={{ servicename }}
state=started
2. Create a playbook and setup a webserver(httpd) and use httpd as a variable decalared using prompt
---
- name: Setup httpd server
hosts: web
vars_prompt:
- name: "packagename"
prompt: "package-name"
tasks:
- name: Install httpd
yum:
name={{ packagename }}
state=latest
- name: Copy file with owner and permissions
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Start service httpd, if not started
ansible.builtin.service:
name={{ packagename }}
state=started
- name: Print the vars
ansible.builtin.debug:
msg: package {{ packagename }} installed
3. Create a playbook and setup a webserver(httpd) and find a list of .txt file in some directory and output using register variables
---
- name: Setup httpd server
hosts: web
vars:
packagename: httpd
servicename: httpd
name: sreepada
tasks:
- name: Install httpd
yum:
name={{ packagename }}
state=latest
- name: Copy file with owner and permissions
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Start service httpd, if not started
ansible.builtin.service:
name={{ servicename }}
state=started
- name: Ansible register variable example
shell: "find *.txt"
args:
chdir: "/home/centos/ansible/sreepada"
register: find_output
- debug:
var: find_output.stdout_lines[0]
4. Create a playbook and setup a webserver(httpd), copy a index.html and interpolate variables in template file decalared in playbook.
---
- name: Setup httpd server
hosts: web
vars:
myname: sreepada
age: 23
tasks:
- name: Install httpd
yum:
name=httpd
state=latest
- name: Start service httpd, if not started
ansible.builtin.service:
name=httpd
state=started
- name: Template a file to /var/www/html/
ansible.builtin.template:
src: index.html.j2
dest: /var/www/html/index.html