Introduction to Ansible and Basics.
Ansible is part of the Extra Packages for Enterprise Linux (EPEL) repository so you need to install epel-release package first
$ sudo yum install epel-release
$ sudo yum repolist
$ sudo yum install ansible
-
Ansible can interact with one more servers at a time. To interact ansible will use
- SSH for Linux
- Power Shell Remoting for Windows
-
Ansible is Agentless - No client software is needed on servers.
-
Information about the target machines will be stored in a inventory file.
- Default Inventory file for ansible is available at /etc/ansible/hosts
-
Ansible Inventory Parameters
- ansible_host - used to specify fqdn or ip address of a server
- ansible_connection - Defines how ansible connects to target server (ssh, winrm etc)
- ansible_port - Defines which port to connect to. (By default configured to port-22)
- ansible_user - The login details ( linux - root/ windows - administrator)
- ansible_ssh_pass - The ssh password for linux
- ansible_password - The password for windows
[group1]
host1
host2
..
[group1:children]
child_group1
child_group2
..
- https://github.com/sumanth979/AnsiblePractise/blob/master/sample_inventory_file_1.yaml
- https://github.com/sumanth979/AnsiblePractise/blob/master/sample_inventory_file_2.yaml
ansible <hostname> -m <modulename>
&&
ansible <hostname> -a <command>
To specify the inventory file:
ansible <hostname> -m <modulename> -i <inventoryfile>
- Running a series of instructions on different servers in a sequence and restarting the servers in a particular order.
- How a Play Book is writtened:
Playbook - single YAML file
* play - Defines a Set of activities(tasks) to be run n hosts.
* Task - An action to be perfomed on the host
* Execute a Command
* Run a script
* Install a package
* Shutdown/Restart
...
- The each task execute by a Playbook is called Ansible Module
To get the list of modules available with ansible, use the following command:
ansible-doc -l
ansible-playbook --help
ansible-playbook <playbook name> -i <inventoryfile>
- Ansible Modules are categorized into different types. They are as follows:
- system -> actions to be performed at system level. (modifying users, firewalls, stopping and starting services etc)
- commands -> Used to execute commands or scripts on target machines.
- Files -> Help to work with files
- Databases
- Cloud
- windows -> to work with windows
- ...
- Ansible Variables can create either in inventory file or in playbook file.
- Either we can create seperate file to maintain variable information.
vars:
dns_server: <server_value>
# sample inventory file
server1 variable1=<value> variable2=<value>
# sample variable file - variable.yaml
variable1: <value>
variable2: <value>
variable3: <value>
It to use directly - `{{variableName}}`
Inside a sentence - Inside a {{variableName}} sentence.
- We can use AND and OR to check multiple conditions.
tasks:
- name : <name>
yum:
name: nginx
state: present
when: ansible_os_family == "RedHat"
- Register is used to store the value of the command executed.
Sample Usage:
tasks:
-
shell: 'cat /etc/resolv.conf'
register: command_output
- loop previously names as [with_*] in ansible.
- To run same set of commands multiple times.
Sample Usage:
-
name: create users
hosts: localhost
tasks:
- user: name= '{{item}}' state=present
loop:
- user1
- user2
- user3
...
Sample Usage:
-
name: create users
hosts: localhost
tasks:
- user: name= '{{item.name}}' uid='{{item.id}}' state=present
loop:
- name: user1
id: 1
- name: user2
id: 2
- name: user3
id: 3
...
-
Asigning a role means
- Installing pre-requisities
- Installing packages
- Configuring services
- Configuring users and others
-
Sample Roles are - mysql, nginx, redis, monitor, backup etc..
-
For every task you no need to create your own role. You can make use of exsting roles (in ansible galaxy).
-
/etc/ansible/roles is the default location where ansible searches for the roles.
ansible-galaxy search <roleName>. -> will search for role in ansible galaxy.
ansible-galaxy install <roleName>. -> will install role from ansible galaxy.
ansible-galaxy init <roleName>. -> will create a directory structure as required for a new role.