Ansible All Scripts and Modules
Ansible All Scripts and Modules
Ansible All Scripts and Modules
1 ) singlelineplaybook.yml
- hosts: web
tasks:
2 ) multilineplaybook.yml
- hosts: web
tasks:
- copy: src=/https/www.scribd.com/etc/passwd
dest=/tmp/
owner=ubuntu
group=ubuntu
mode=777
- file: path=/tmp/general
state=directory
3 ) multilinewithtwogroups.yml
- hosts: web
tasks:
- copy: src=/https/www.scribd.com/etc/passwd
dest=/tmp
owner=ubuntu
group=ubuntu
mode=777
- hosts: database
tasks:
- file: path=/tmp/ansible
state=directory
4 ) multilinewithtwogroups2.yml
- hosts: web
tasks:
- copy: src=/https/www.scribd.com/etc/passwd
dest=/tmp/
owner=ubuntu
group=ubuntu
mode=777
- hosts: database
tasks:
- file: path=/tmp/abc
state=/directory
// ansible loops
1 ) loopsplaybook.yml
- hosts: all
tasks:
- copy:
dest: /tmp
with_items:
- /etc/passwd
- /etc/group
- /etc/shadow
2) loopsplaybook2.yml
- hosts: all
tasks:
- copy:
src: /etc/passwd
with_items:
- /tmp
- /tmp/abc
- /tmp/xyz
- file:
state: directory
with_items:
- /tmp/anil
- /tmp/sunil
3 ) nestedloopsplaybook4.yml
- hosts: all
tasks:
- copy:
with_nested:
- /tmp
- /etc/passwd
4) nestedloopsplaybook5.yml
- hosts: web
vars:
- mydir:
- /tmp
- /tmp/dir1
- /tmp/dir3
tasks:
- copy:
with_nested:
- "{{ mydir }}"
- [ "/etc/passwd" , "/etc/shadow" ]
5 ) nestedloopwithvarplaybook.yml
- hosts: all
vars:
- myfile:
- /etc/passwd
- /etc/shadow
- /etc/group
- mydir:
- /tmp
- /tmp/dir1
- /tmp/dir2
- /tmp/dir3
tasks:
- copy:
with_nested:
- hosts: all
gather_facts: false
tasks:
- copy:
with_items:
- { a: '/etc/passwd' , b: '/tmp' }
- { a: '/etc/group' , b: '/opt' }
- { a: '/etc/shadow' , b: '/mnt' }
// builtinfunction
1 ) builtinfunction.yml
- hosts: all
#gather_facts: false
tasks:
- ansible.builtin.apt:
name: python3
state: present
- hosts: all
tasks:
- ansible.builtin.apt:
name: python3
state: present
3 ) builtinfunctionwithNOT.yml
- hosts: all
tasks:
- ansible.builtin.apt:
name: python3
state: present
4 ) builtinfunctionwithOR.yml
- hosts: all
tasks:
- ansible.builtin.apt:
name: python3
state: present
1)notifyservice.yml
- hosts: all
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
notify:
- MyNotification
handlers:
- name: MyNotification
service:
name: sshd
state: start
- name: MyNotification1
service:
name: python3
state: restarted
2 ) notifyservice2.yml
- hosts: all
tasks:
- lineinfile:
path: /etc/ssh/ssh_config
notify: MyNotification
handlers:
- name: MyNotification
service:
name: sshd
state: restarted
3 ) notifyservice3.yml
- hosts: all
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
state: present
notify: MyNotification
- lineinfile:
path: /etc/ssh/sshd_config
notify: MyNotification1
handlers:
- name: MyNotification1
service:
name: sshd
state: reloaded
4 ) notifyservice4.yml
- hosts: web
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
state: present
notify: MyNotification
- lineinfile:
path: /etc/ssh/sshd_config
state: present
notify: MyNotification1
handlers:
- name: MyNotification1
file:
path: /tmp/abcdef
state: touch
5 ) notifyservice5.yml
- hosts: all
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
notify: MyNotification
handlers:
- name: MyNotification
service:
name: sshd
state: restarted
// stat
1 ) stat.yml
- hosts: all
tasks:
- file:
path: /tmp/abc
state: touch
- stat:
path: /tmp/abc
register: FileExist
- debug: var=FileExist
- copy:
dest: /tmp/abc
1 ) task.yml
debug:
debug:
2 ) import_task.yml
- name: Main Playbook
hosts: web
gather_facts: false
tasks:
debug:
import_tasks: task.yml
// create users
1 ) usergroup.yml
- hosts: all
gather_facts: false
tasks:
- user:
state: present
with_items:
172.31.8.163
172.31.0.19
myfile1=/etc/passwd
2 ) varforgroup.yml
hosts: web
gather_facts: true
tasks:
debug:
copy:
src: /tmp/file1 # Replace with the path to your local file
172.31.8.163 myfile1=/etc/passwd
172.31.0.19 myfile1=/etc/passwd
Here above infront of private ip of ec2, we declare one variable that is myfile1
2 ) varforIP.yml
hosts: web
gather_facts: true
tasks:
debug:
copy:
src: /tmp # Replace with the path to your local file
1 ) defaultvaransible.yml
- hosts: all
vars:
myfile:
- /etc/passwd
- /etc/shadow
- /etc/group
tasks:
- copy:
dest: /tmp
// ansible tags
1 ) ansibletags.yml
- hosts: all
tasks:
- copy:
src: /etc/passwd
dest: /tmp
tags: dev-team
- file:
path: /tmp/bbc
state: directory
tags: test-team
- apt:
name: httpd
state: directory
tags: prod-team
// ignore errors
1 ) ignoreerror.yml
- hosts: all
tasks:
- copy:
src: /etc/passwd
dest: /tmp
ignore_errors: yes
- file:
path: /tmp/tbc
state: directory
tags: test-team
1 ) BlockRescueAlways.yml
- hosts: all
tasks:
- block:
- lineinfile:
path: /tmp/rahul
rescue:
- file:
path: /tmp/rahul
state: touch
- lineinfile:
path: /tmp/rahul
always:
- apt:
name: python3
state: present
A module is a reusable, standalone script that Ansible runs on your behalf, either
locally or remotely.
Modules interact with your local machine, an API, or a remote system to perform
specific tasks like changing a database password or spinning up a cloud instance.
2) command module
ansible webserver -m command -a "uptime"
3 ) shell module –
It is same like command module olny but it execute cmd in /bin/sh -
ansible webserver -m shell -a "sh/tmp/myscript.sh"
4)copy module –
In above both senario file was present in controller and we were copying itt in worker .
but now if file is present in worker olny and we want to copy it in another location of worker
only then -
ansible-doc copy
5) fetch module –
we use fetch module to copy file from worker to controller -
6) file module –
this module is used to create files and folders, to create file use below file
module command -
ansible webserver -m file -a "path=/tmp/india1 state=directory"
7) yum module –
It is same as package module use to install software & remove them , to install package httpd
–
8) apt module –
ansible webserver -m apt -a "name=package state=present"
9) package module –
It is very userfull to install & remove a software
There is specific yum and apt module also available.
ansible webserver -m package -a “name=httpd state=present”
to remove package –
ansible webserver -m package -a "name=httpd state=absent"
10 ) user module –
11 ) group module –
we use to create groups
ansilbe webserver -m group -a "name=wipro state=present"
14 ) lineinfile module –