What is Ansible
Ansible is a configuration management, deployment and orchestration tool.
Features of Ansible
- Simple: Simple to install and setup
- Built on top of Python: Provides lot of python functionality.
- SSH for Secure Connection
- Agentless
- Platform independent
- Push Based unlike Puppet and Chef
Install and Setup:
- Install Ansible via pip install ansible
- Generate SSH-Key on Master node using ssh-keygen -t rsa
- Goto /root/.ssh and copy id_rsa.pub key to all other nodes and
- Run ssh-copy-id <Destination IP Address>
- remote_user specifies the user you want to use on the managed host. If you do not specify, it uses current user name.
- remote_port default is 22
- ask_pass controls whether ansible will prompt you for SSH password. By default, it is assuming you're using SSH key based authentication.
- become controls whether you will automatically use privilege escalation. default no
- become_user controls what user on the managed host ansible should become. default root
- become_method controls how ansible will become that user. default sudo.
- become_ask_pass controls whether to ask for password for your become method. default is no.
[defaults]
inventory = ./inventory.ini
remote_user = ansible
ask_pass = false
[privilege_escalation]
become = true
become_user = root
become_ask_pass = false
Host Based Connection Variables
#connection variables for db_01
ansible_host: 127.0.0.1
ansible_port: 3456
ansible_user: root
ansible_become: false
Ansible Playbooks
- name: Check daya user exists or not
hosts: all
become: yes
tasks:
- name: daya user exist or not
user:
name: daya
state: present
- name: ensure nginx is at the latest version
apt:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started
Dry run
---
name: New user is created
hosts: web_servers
become: true
vars:
username: test
tasks:
- name: User gets created.
user:
name: "{{ username }}"
state: present
Managing variables in playbooks
- hosts: all
vars:
user_name: joe
user_state: present
It is also possible to define variables in a external file and reference it inside a play
- hosts: all
vars_files:
- vars/users.yml
- name: Example Play
hosts: all
vars:
user_name: joe
tasks:
- name: Creates the user {{ username }}
user:
name: "{{ user_name }}"
state: present
Using list to install several packages as part of the task in a play
---
- name: Install Packages
hosts: all
vars:
packages:
- nmap
- httpd
- php
- mod_php
- mod_ssl
tasks:
- name: Install software
yum:
name: {{ package }}
state: present
Ansible-Vault - Protecting Sensitive data
- ansible-vault create filename to create a new file
- ansible-vault view filename to view encrypted file
- ansible-vault edit filename to edit an encrypted file
- ansible-vault encrypt filename to encrypt existing file
- ansible-vault decrypt filename to decrypt a file
- ansible-vault rekey filename to change the password of an encrypted file
Playbooks and Ansible-vault
- name : Print Variable
debug:
msg: "{{ secret }}"
- name : Print Variable
debug:
msg: "{{ secret }}"
no_log: true
Running Tasks Conditionally
---
- name: Simple Boolean Task
hosts: all
vars:
run_my_task: true
tasks:
- name: httpd package is installed
yum:
name: httpd
when: run_my_task
another example: It tests whether variable is defined.
---
- name: Test Variable is Defined Demo
hosts: all
vars:
my_service: httpd
tasks:
- name: "{{ my_service }} package is installed"
yum:
name: "{{ my_service }}"
when: my_service is defined
other examples for when condition
- ansible_machine == 'x86_64'
- max_memory == 512
- min_memory >= 128
- variable exists <variable> is defined
- name: Demonstrate "in" in a condition
hosts: all
gathered_facts: yes
become: yes
vars:
my_service: httpd
supported_os:
- RedHat
- Fedora
tasks:
- name: Install "{{ my_service }}"
yum:
name: "{{ my_service }}"
state: present
when: ansible_facts['distribution'] in supported_os
The ansible_facts['distribution'] variable is a fact set when the play runs, which identifies the operating system of the currently managed host.
The supported_os variable contains a list of operating systems supported by the playbook.
Testing multiple conditions using and or keywords
- when: ansible_distribution == "RedHat" or ansible_distribution == "Fedora"
- when ansible_distibution_version=="7.5" and ansible_kernel =="3.10.0-327.e17.x86_64"
Ansible Handlers
tasks:
- name: copy demo.example.conf configuration template
template:
src: /var/lib/templates/demo.example.conf.template
dest: /etc/httpd/conf.d/demo.example.conf
notify:
- restart apache
handlers:
- name: restart apache
service:
name: httpd
state: restarted
ReplyDeleteThank you for sharing very useful information.
Workday Online Training in India
Workday Online Training in Hyderabad