Playbook Using VARIABLES
- No special characters
- Avoid CAPS.
- '_' Allowed
- Case Sensitive
- DOT(.) not allowed
Make sure the Ansible Folders are outside the /etc
Using Variables - Installation of HTTPD & Firewalld
- Create the yaml file with below details,
cat httpd.yml
---
- name: deploy and start httpd service
hosts: centos
become: true
vars:
web_pkg: httpd
firewall_pkg: firewalld
web_service: httpd
firewall_service: firewalld
rule: http
tasks:
- name: required packages installed and up to date
yum:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
state: latest
- name: The {{ firewall_service }} service is started
service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: The {{ web_service }} service is started
service:
name: "{{ web_service }}"
enabled: true
state: started
- name: web content is in place
copy:
content: <h1> Devops team </h1>
dest: /var/www/html/index.html
- name: firewall port for {{ rule }} opened
firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabled
become: true --> run as root
rule --> defined for PORT
Check whether the playbook is correct , its like to validate before the RUN. This is called DRYRUN.
ansible-playbook httpd.yml --check
Actual run is below with verbose output
ansible-playbook httpd.yml -vvv
- Desired output
Now LETS create a users using file and variable declaration
cat vars.yml
user_details:
- {name: 'user3', uid: 1007}
- {name: 'user4', uid: 1008}
cat users.yml
---
- hosts: centos
become: true
vars_files:
- /home/sathishpy1808/ansible-playbooks/vars.yml
tasks:
- name: add several users
user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
state: present
with_items: "{{ user_details }}"
ansible-playbook users.yml -e @/home/sathishpy1808/ansible-playbooks/vars.yml --check
or
ansible-playbook users.yml --check
ansible-playbook users.yml -e @/home/sathishpy1808/ansible-playbooks/vars.yml -vvv
or
ansible-playbook users.yml -vvv
- Users are created in the TARGET machines
Important
- Indentation is important while writing the YAML file , so you can also set like below,
set
autoindentation
extended tab
tab space
cursor & column
set ai et ts=2 cuc
Hope you are able to see a LINE in shade.
Sites
This site will help to validate the YAML file,
https://www.yamllint.com/
Notes
- Facts gathering [TBD]
- Plugin integration [TBD]
- Ansible idempotent ???