Day 2 – Ansible Notes – Pep – Aug – 2023
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
ansible behavioral inventory parameters
==============================================
https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters
inventory
------------------------
[web]
14.4.45.5 ansible_user=deploy ansible_password=pass123
14.4.45.6 ansible_ssh_private_key_file=node.pem
[db]
14.4.45.7 ansible_connection=winrm
14.4.45.8 ansible_connection=ssh
db1 ansible_host=14.4.45.9 ansible_port=23 ansible_become=true
inventory
------------------------
[web]
14.4.45.5
14.4.45.6
[db]
14.4.45.7
14.4.45.8
db1
[db:vars]
ansible_user=deploy
ansible_password=pass123
ansible_connection=ssh
ansible_port=23
ansible_become=true
vars in inventory
=========================
CMD Vs Host Vs group
========================================================================
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
PLAYBOOK
======================================================
- YAML FILE
- Contains
- PLAY
- PLAY
Hosts: localhost | Groupname
Tasks:
Mod1 and its param|arguments
Mod2 and its param
Mod3 and its param
Mod4 and its param
Mod5 and its param
Mod6 and its param
- PLAY
Hosts: localhost | Groupname
Tasks:
Mod1 and its param
Mod2 and its param
Mod3 and its param
Mod4 and its param
Mod5 and its param
Mod6 and its param
- PLAY
Hosts: localhost | Groupname
Tasks:
Mod1 and its param
Mod2 and its param
Mod3 and its param
Mod4 and its param
Mod5 and its param
Mod6 and its param
ansible web -i inventory -m yum -a"state=absent name=httpd" -u root -k
ansible web -i inventory -m yum -a"state=latest name=httpd" -u root -k
ansible web -i inventory -m copy -a"dest=/var/www/html/index.html src=index.html" -u root -k
ansible web -i inventory -m service -a"name=httpd state=started" -u root -k
ansible web -i inventory -m service -a"name=firewalld state=stopped enabled=no" -u root -k
---
- name: Install and configure web server
hosts: web_servers
tasks:
- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
state: absent
- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
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: httpd
state: started
- name: Start service httpd, if not started
ansible.builtin.service:
name: firewalld
state: stopped
enabled: no
- name: Update package cache
apt:
update_cache: yes
when: ansible_os_family == "Debian"
ansible-playbook -i inventory web.yaml -u root -k
====================================================================
===================================================================
Sc1 - One Playbook is calling another playbook
Sc2 - One playbook is calling another tasks file
---
- name: Install and configure web server
hosts: web
tasks:
- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
state: absent
- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
state: latest
---
- name: Install and configure web server
hosts: web
tasks:
- 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: httpd
state: started
- name: Start service httpd, if not started
ansible.builtin.service:
name: firewalld
state: stopped
enabled: no
One Playbook is calling another playbook
====================================
Mehtod 1- master.yaml
play1
play2
- import_playbook: play1.yaml
- import_playbook: play2.yaml
ansible-playbook -i inventory play1.yaml -u root -k
ansible-playbook -i inventory play2.yaml -u root -k
ansible-playbook -i inventory master.yaml -u root -k
Sc2 - One playbook is calling another tasks file
===================================================
---
- name: Install and configure web server
hosts: web
tasks:
- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
state: absent
- name: Install the latest version of Apache
ansible.builtin.yum:
name: httpd
state: latest
- include: tasks.yaml
============================================================
Vars
============================================================
IN PLAYBOOK
---
- name: Install and configure web server
hosts: web
vars:
myname: rajesh
age: 16
package: httpd
service: httpd
tasks:
- name: Install the latest version of Apache
ansible.builtin.yum:
name: "{{ package }}"
state: absent
- name: Install the latest version of Apache
ansible.builtin.yum:
name: "{{ service }}"
state: latest
- include: tasks.yaml
- name: Print the gateway for each host when defined
ansible.builtin.debug:
msg: My name is {{ myname }} & my age is {{ age }}
myname: rajesh in vars files
age: 18
package: httpd
service: httpd
---
- name: Install and configure web server
hosts: web
vars:
myname: rajesh
age: 16
package: httpd
service: httpd
vars_files:
- "diffvars.yaml"
vars_prompt:
- name: "version"
prompt: "Which version Do you want to install?"
private: no
tasks:
- name: Install the latest version of Apache
ansible.builtin.yum:
name: "{{ package }}"
state: absent
- name: Install the latest version of Apache
ansible.builtin.yum:
name: "{{ service }}"
state: latest
- include: tasks.yaml
- name: Print the gateway for each host when defined
ansible.builtin.debug:
msg: My name is {{ myname }} & my age is {{ age }}
- name: Print the gateway for each host when defined
ansible.builtin.debug:
msg: Version is = {{ version }}
---
- name: Install and configure web server
hosts: web
vars:
myname: rajesh
age: 16
package: httpd
service: httpd
vars_files:
- "diffvars.yaml"
vars_prompt:
- name: "version"
prompt: "Which version Do you want to install?"
private: no
tasks:
- name: Install the latest version of Apache
ansible.builtin.yum:
name: "{{ package }}"
state: absent
- name: Install the latest version of Apache
ansible.builtin.yum:
name: "{{ service }}"
state: latest
- include: tasks.yaml
- name: include default step variables
include_vars: task_vars.yaml
- name: Print the gateway for each host when defined
ansible.builtin.debug:
msg: My name is {{ myname }} & my age is {{ age }}
- name: Print the gateway for each host when defined
ansible.builtin.debug:
msg: Version is = {{ version }}
- name: Ansible register variable basic example
shell: "find *.txt"
args:
chdir: "/root/rajesh"
register: find_output
- debug:
var: find_output
- debug:
var: find_output.stdout_lines
- debug:
var: find_output.stdout_lines[0]