Ansible scripts all:
1 ) singlelineplaybook.yml
- hosts: web
tasks:
- copy: src=/etc/passwd dest=/tmp/ owner=ubuntu group=ubuntu mode=777
- file: path=/tmp/ansible state=directory
2 ) multilineplaybook.yml
- hosts: web
tasks:
- copy: src=/etc/passwd
dest=/tmp/
owner=ubuntu
group=ubuntu
mode=777
- file: path=/tmp/general
state=directory
3 ) multilinewithtwogroups.yml
- hosts: web
tasks:
- copy: src=/etc/passwd
dest=/tmp
owner=ubuntu
group=ubuntu
mode=777
- hosts: database
tasks:
- file: path=/tmp/ansible
state=directory
4 ) multilinewithtwogroups2.yml
- name: this message for web
- hosts: web
tasks:
- copy: src=/etc/passwd
dest=/tmp/
owner=ubuntu
group=ubuntu
mode=777
- name: this message for database
- hosts: database
tasks:
- file: path=/tmp/abc
state=/directory
// ansible loops
1 ) loopsplaybook.yml
- hosts: all
tasks:
- copy:
src: "{{ item }}"
dest: /tmp
with_items:
- /etc/passwd
- /etc/group
- /etc/shadow
2) loopsplaybook2.yml
- hosts: all
tasks:
- copy:
src: /etc/passwd
dest: "{{ item }}"
with_items:
- /tmp
- /tmp/abc
- /tmp/xyz
- file:
path: "{{ item }}"
state: directory
with_items:
- /tmp/anil
- /tmp/sunil
3 ) nestedloopsplaybook4.yml
- hosts: all
tasks:
- copy:
src: "{{ item[1] }}"
dest: "{{ item[0] }}"
with_nested:
- /tmp
- /etc/passwd
4) nestedloopsplaybook5.yml
- hosts: web
vars:
- mydir:
- /tmp
- /tmp/dir1
- /tmp/dir3
tasks:
- copy:
src: "{{ item[1] }}"
dest: "{{ item[0] }}"
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:
src: "{{ item[0] }}"
dest: "{{ item[1] }}"
with_nested:
- "{{ myfile }}"
- "{{ mydir }}"
6 ) hashloop.yml
- hosts: all
gather_facts: false
tasks:
- copy:
src: "{{ item.a }}"
dest: "{{ item.b }}"
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
when: ansible_distribution == "Ubuntu"
when: ansible_distribution_major_version == "22"
2) builtinfunctionwithAND.yml
- hosts: all
tasks:
- ansible.builtin.apt:
name: python3
state: present
when: ansible_distribution != "Ubuntu" and ansible_distribution_major_version == "22"
3 ) builtinfunctionwithNOT.yml
- hosts: all
tasks:
- ansible.builtin.apt:
name: python3
state: present
when: ansible_distribution != "Ubuntu"
when: ansible_distribution_major_version == "22"
4 ) builtinfunctionwithOR.yml
- hosts: all
tasks:
- ansible.builtin.apt:
name: python3
state: present
when: ansible_distribution == "Ubuntu" or ansible_distribution_major_version == "22"
// notify handler
1)notifyservice.yml
- hosts: all
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
line: DenyUser ubuntu
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
line: DenyUser ubuntu
notify: MyNotification
handlers:
- name: MyNotification
service:
name: sshd
state: restarted
3 ) notifyservice3.yml
- hosts: all
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
line: "PermitRootLogin yes"
state: present
notify: MyNotification
- lineinfile:
path: /etc/ssh/sshd_config
line: "PermitRootLogin no"
state: absent
notify: MyNotification1
handlers:
- name: MyNotification1
service:
name: sshd
state: reloaded
4 ) notifyservice4.yml
- hosts: web
tasks:
- lineinfile:
path: /etc/ssh/sshd_config
line: "PermitRootLogin yes"
state: present
notify: MyNotification
- lineinfile:
path: /etc/ssh/sshd_config
line: "PermitRootLogin no"
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
line: DenyUser ubuntu
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:
content: "welcome to ansible"
dest: /tmp/abc
when: FileExist.stat.exists == true
// import task (tasks and hosts both are in different files)
1 ) task.yml
- name: Additional Task 1
debug:
msg: "This is the first additional task."
- name: Additional Task 2
debug:
msg: "This is the second additional task."
2 ) import_task.yml
- name: Main Playbook
hosts: web
gather_facts: false
tasks:
- name: Display Message
debug:
msg: "This is the main playbook."
- name: Import additional tasks from another file
import_tasks: task.yml
// create users
1 ) usergroup.yml
- hosts: all
gather_facts: false
tasks:
- user:
name: "{{ item.user }}"
state: present
groups: "{{ item.group }}"
with_items:
- { user: 'rushi', group: 'ubuntu' }
- { user: 'dhananjay', group: 'ubuntu' }
// create variable for group of slave IPS in host file, first
declare variable for group in host file
Below Path to add hosts or private ip of ec2 machines -
1 ) vi /etc/ansible/hosts
[web] //host group name
172.31.8.163
172.31.0.19
[web:vars] // variable for above web host group
myfile1=/etc/passwd
2 ) varforgroup.yml
- name: Copy File to Hosts in a Group
hosts: web
gather_facts: true
tasks:
- name: Display File Variable
debug:
msg: "Copying file to hosts with myfile1={{ myfile1 }}"
- name: Copy File to Hosts
copy:
src: /tmp/file1 # Replace with the path to your local file
dest: "{{ myfile1 | dirname }}"
// create variable for IP of slave in host file, first declare
variable for IP in host file
Below Path to add hosts or private ip of ec2 machines -
1 ) vi /etc/ansible/hosts
[web] // host group
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
- name: Copy File to Hosts
hosts: web
gather_facts: true
tasks:
- name: Display Private IP and File
debug:
msg: "Copying file to {{ inventory_hostname }} with myfile1={{ myfile1 }}"
- name: Copy File to Host
copy:
src: /tmp # Replace with the path to your local file
dest: "{{ myfile1 }}"
// default ansible variable
1 ) defaultvaransible.yml
- hosts: all
vars:
myfile:
- /etc/passwd
- /etc/shadow
- /etc/group
tasks:
- copy:
src: "{{ item }}"
dest: /tmp
with_items: "{{ myfile }}"
// 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
// ansible using block rescue always
1 ) BlockRescueAlways.yml
- hosts: all
tasks:
- block:
- lineinfile:
path: /tmp/rahul
line: "Hello Ansible"
rescue:
- file:
path: /tmp/rahul
state: touch
- lineinfile:
path: /tmp/rahul
line: "Hello India"
always:
- apt:
name: python3
state: present
// important ansible modules using adhoc method
Ansible modules :
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.
1)Ping module command –
ansible webserver -m ping
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 –
ansible webserver -m copy -a "src=/etc/passwd dest=/etc/temp"
COPY A FILE WITH DIFFRENT PERIMISSION AND PROPERTIES .
ansible web -m copy -a "src = /etc/passwd dest=/tmp owner=rahul group=aws mode=777 " -k
--(yellow coloer o/p)
if you repeat same command it will give green color
yello --> change
green --> no change
red ---> error
if a file is already present in worker (slave node ) , you want to keep that file as well the n set
backup=yes
ansible web -m copy -a "src = /etc/passwd dest=/tmp owner=rahul group=aws mode=777
backup=yes" -k --(will keep older file as bak up )
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 web -m copy -a "src = /etc/passwd remote_src=yes dest=/tmp owner=rahul
group=aws mode=777 " -k
To write any content inside a file -
ansible web -m copy -a 'content="welcome to Ansible " dest=/tmp/newfile.txt'
to see full doc of copy module -
ansible-doc copy
5) fetch module –
we use fetch module to copy file from worker to controller -
ansible webserver –m fetch -a ‘src=/var/log/auth.log dest=samplelog1’
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"
change permission of file using file module –
ansible webserver -m file -a "path=/tmp/india2 mode=777"
delete file or folder using file module –
ansible webserver -m file -a "path =/tmp/india2 state=absent"
7) yum module –
It is same as package module use to install software & remove them , to install package httpd
–
ansible webserver -m yum -a "name=httpd state=present"
to remove httpd package using yum module –
ansible webserver -m yum -a "name=httpd state=absent"
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 –
user module we use to create users –
ansible web -m user -a 'name=natasha comment="IT Admin" uid=1010 group=wipro'
11 ) group module –
we use to create groups
ansilbe webserver -m group -a "name=wipro state=present"
12) raw module –
ansible webserver -m raw -a "uptime;pwd;ls -a"
13) service module –
It control the service on remote hosts (slave machines )
ansible webserver -m service -a "name=apache2 state=started"
To stop apache2 service on workker node
ansible webserver -m service -a "name=apache2 state=stopped"
14 ) lineinfile module –
This module is used to insert a line inside a file
To add a line in file
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL'"
To delete a line in file
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL' state=absent
"
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL'
insertafter=BOF"
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL'
insertafter=EOF"
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL'
insertafter='^root'"
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL'
insertbefore='^root'"
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL'
regexp='^root'"
ansible webserver -m lineinfile -a "path=/etc/sudoers line='rahul ALL=(ALL)ALL' regexp='^root'
state=absent"