Professional Documents
Culture Documents
Ansible does not have any service/daemon running, but relies on SSH. So it is called as
agentless service, only make sure that ssh service is running properly on both server and
clients and ansible server user public key is copied to all the agent servers.
2) hosts: which stores all client machine details in order to communicate from the ansible
server.
4) Task: block which defines the task that need to be executed like: install apache2
5) module: which is developed to do a specific task on the servers which is written in python
language. like apt, yum, service, ini_file modules.
6) role: Roles are good for organizing multiple, related Tasks and encapsulating data needed
to accomplish those Tasks. It’s a pre-defined way for organizing playbooks and other files in
order to facilitate sharing and reusing portions of a provisioning.
===
If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play
If roles/x/vars/main.yml exists, variables listed therein will be added to the play
If roles/x/defaults/main.yml exists, variables listed therein will be added to the play
If roles/x/meta/main.yml exists, any role dependencies listed therein will be added to the list
of roles
===
7) Facts: Before running any Tasks, Ansible will gather information about the system it's
provisioning, like no of network interfaces, ip address or operating system details like OS
family, version etc.,
8) Handlers: used to trigger service status changes, like restarting or stopping a service.
9) variables: which hold the value to pass in the playbook. Example to store the variable in
vars_files, group_vars etc.,
10) loop: it is typically used to repeat a task using different input values
===
- name: install packages
apt: name={{ item }} state=present
with_items:
- apache2
- mysql-server
- telnet
===
11) conditions: It is used to confirm whether to execute the specific task on the machines or
not.
12) Template: It is used to setup the configuration files on the application etc., It uses jinja2
template engine.
13) Meta: The main.yml file within the meta directory contains Role metadata, including
dependencies.
---
dependencies: []
---
dependencies:
- { role: ssl }
dependencies:
- { role: common, some_parameter: 3 }
- { role: apache, apache_port: 80 }
- { role: postgres, dbname: blarg, other_parameter: 12 }
By default we specify the variables in main inventory file. So we can store the group and
host specific variable values in individual files according to the inventory file.
Example:
1) /etc/ansible/group_vars/<group_name>
2) /etc/ansible/host_vars/<host_name>
Also, we can create files under gourp_vars directory to define variables of group hosts.
1) /etc/ansible/group_vars/<group_name>/<files>
2) /etc/ansible/host_vars/<host_name>/<files>
Note: In Ansible 1.2 or later the group_vars/ and host_vars/ directories can exist in the
playbook directory OR the inventory directory. If both paths exist, variables in the playbook
directory will override variables set in the inventory directory
---
- hosts: all
sudo: true
vars:
packages: [ 'apache2', 'wget', 'telnet' ]
tasks:
- name: Install Package
apt: name={{ item }} state=latest
with_items: packages
-----------------
Starting in 1.0, variables can also be passed to include files using an alternative syntax,
which also supports structured variables:
tasks:
- include: wordpress.yml
vars:
wp_user: timmy
ssh_keys:
- keys/one.txt
- keys/two.txt
++++++++++++++++++++++++++
+++++++++++++++++++++++++++
name: laravel
repository: https://github.com/do-community/do-ansible-adv-php.git
branch: example
domain: laravel.example.com
+++++++++++++++++++++++++++
- include: intro_example.yml
+++++++++++++++++++++++++++
---
- hosts: webservers
roles:
- { role: some_role, when: "ansible_os_family == 'RedHat'" }
- hosts: webservers
roles:
- common
- { role: foo_app_instance, dir: '/opt/a', app_port: 5000 }
- { role: foo_app_instance, dir: '/opt/b', app_port: 5001 }
- hosts: webservers
roles:
- { role: foo, tags: ["bar", "baz"] }
++++++++++++++++++++++++++++
---
- hosts: webservers
pre_tasks:
- shell: echo 'hello'
roles:
- { role: some_role }
tasks:
- shell: echo 'still busy'
post_tasks:
- shell: echo 'goodbye'
++++++++++++++++++++++++++++
Senarios:
--------------
1) Install webserver on both Debian 14.04 and RedHat6 machines using single playbook.
2) Install tomcat on Redhat 7 machine only using playbook.
3) Install LAMP on Ubuntu 16.04 and CentOS 7.