Ansible Notes
Ansible Notes
Agentless :There is no software or agent to be installed on the client that communicates back
to the server.
Simple and extensible: Ansible is written in Python and uses YAML for playbook language,
both of which are considered relatively easy to learn.
PLAYBOOK:
Ansible playbooks are a way to send commands to remote computers in a scripted way.
Instead of using Ansible commands individually to remotely configure computers from the
command line, you can configure entire complex environments by passing a script to one or
more systems.
WHY ANSIBLE:
While managing the multiple servers its hard to keep their configuration identical. If you have
multiple servers which needs to configure the same setup in all. while doing the one to one
server their might be a chances to miss some configuration steps in some servers. Thats why
automation tools come into play! The automation tools like Ansible, Chef, Puppet and
SaltStack all are based on a same principle.
NOTE: REPETE ALL THESE STEPS ON ALL SLAVE SERVERS FROM STEP-3, 4
&5
STEP-7: COPY THE PUBLIC KEY TO ALL SLAVE SERVERS (ssh-copy-id root@slave_ip)
PLAYBOOKS:
one.yml two.yml
SYNTAX: [email protected]/username/repo.git
Ansible roles are a way to organize and structure your Ansible playbooks in a more modular
and reusable manner. They provide a means to group related tasks, variables, and files
together, making your playbooks more organized and easier to manage. Roles can be thought
of as a collection of tasks, templates, and variables that are designed for a specific purpose or
function, such as setting up a web server, configuring a database, or managing a specific
application.
You can create a role using the ansible-galaxy command or by manually creating the directory
structure. Let's create the directory structure manually:
roles/
├── webserver/
├── tasks/
│ └── main.yml
├── handlers/
│ └── main.yml
├── templates/
│ └── index.html.j2
├── vars/
│ └── main.yml
├── defaults/
│ └── main.yml
└── meta/
└── main.yml
---
apache_port: 80
---
<!DOCTYPE html>
<html>
<head>
<title>Welcome to My Website</title>
</head>
<body>
</body>
</html>
---
dependencies: []
7. With this role structure in place, you can now use the webserver role in your Ansible playbook by
specifying it in the roles section. For example:
---
hosts: web_servers
become: yes
roles:
- webserver
if you want to apply a see particular output, you can apply filter.
ADHOC COMMANDS:
Ansible ad-hoc commands are quick, one-time instructions you give to Ansible on the
command line to perform simple tasks on remote servers. These commands are not part of
Ansible's usual automation playbook and are typically used for tasks like running a single
command, checking server status, or making minor changes without writing full automation
scripts. Ad-hoc commands are handy for immediate, one-off tasks.
ansible remo -a “ls” [remo: Group name, -a: argument, ls: command]
ansible remo [0] -a “touch file1”
ansible all -a “touch file2”
ansible remo -a “sudo yum install httpd -y”
ansible remo -ba “yum install httpd -y” (b: become you will become sudo user)
ansible remo -ba “yum remove httpd -y”
ANSIBLE MODULES:
Ansible modules are like individual commands or tools that perform specific tasks on target
machines. They are the building blocks for Ansible automation. Modules can do things like
create files, install software, restart services, and more.
ANSIBLE GALAXY:
Ansible Galaxy is a website and command-line tool for sharing and managing collections of
Ansible roles and playbooks. In simple terms, it's like an online marketplace or repository for
Ansible automation content.
ANSIBLE VALUT:
Ansible Vault is a feature of the Ansible automation tool that is used to securely encrypt
sensitive data, such as passwords, API keys, and other secrets, so that they can be safely
stored and shared within Ansible playbooks and roles.
USE CASES:
Encryption
Secure Storage
Password Prompt
Automation
Secrets Management