ANSIBLE
ANSIBLE
Configuration management systems are designed to streamline the process of controlling large numbers of servers, for administrators and operations teams. They allow you to control many different systems in an automated way from one central location.
While there are many popular configuration management tools available for Linux systems, such as Chef and Puppet, these are often more complex than many people want or need. Ansible is a great alternative to these options because it offers an architecture that doesn’t require special software to be installed on nodes, using SSH to execute the automation tasks and YAML files to define provisioning details.
Step 1 — Installing Ansible
First, refresh your system’s package index with:
Following this update, you can install the Ansible software with:
Press Y
when prompted to confirm installation.
Your Ansible control node now has all of the software required to administer your hosts. Next, we’ll go over how to set up an inventory file, so that Ansible can communicate with your managed nodes
Step 2 — Setting Up the Inventory File
$ ansible-playbook mail.yml -kK
SSH password:
BECOME password[defaults to SSH password]:
- -k, --ask-pass: ask for connection password
- -K, --ask-become-pass: ask for privilege escalation password
#ansible your_host -m command -a 'command what you need'
example:
ansible ansible_client -m command -a 'hostname'
Manage Users and Groups on Linux using Ansible
Create/Add user and group using Ansible
user.yml
:---
- hosts: ansible_client #change to your hosts
become: yes
vars:
# NOTICE!!!:
# DO NOT PUT PLAIN TEXT PASSWORDS HERE!
# use encrypted passwords or put them in Ansible vault
# but this is just a demo
vaulted_password: student
tasks:
- name: Add a group called developer
group:
name: developer
state: present
- name: Add user exam with a password
user:
name: student
password: "{{ vaulted_password | password_hash('sha512') }}"
update_password: on_create
shell: /bin/bash
groups: developer
append: yes
$ ansible-playbook user.yml -K
Delete/Remove users using Ansible
user_delete.yml
:
Executing Shell Programing
APT Example Ansible
---
- name: Playbook to install Apache
hosts: webservers
become: true
tasks:
- name: Ansible apt install Apache
apt:
name: apache2
state: present
How to remove a Package with Ansible apt
-
- name: Playbook to install Apache
hosts: webservers
become: true
tasks:
- name: Ansible apt install Apache
apt:
name: apache2
state: absent
How to copy files with Ansible – Local to Remote
All you need is an SSH connection to the remote server. ( with password or SSH key)
Let me explain this in detail
- hosts: A target host group should be already defined in the ansible inventory aka hosts file
- tasks: all the tasks (plays) would be defined under this
- become: this is to tell ansible to execute the corresponding task as a sudo user
root
unless specified any other user withbecome_user
- copy: module name we are going to use in this task
- src: source file path on the local machine where the playbook or ad-hoc command is invoked ( can set ansible to look for the file in remote server using
remote_src
as well ) - dest: destination path on the remote server/host where the file should be copied to
- owner: Owner of the file at the destination server once copied
- group: Group of the file at the destination server once copied
- mode: setting the permission of the file after copying.
0644
would be set as permission on the filerw- r-- r--
- src: source file path on the local machine where the playbook or ad-hoc command is invoked ( can set ansible to look for the file in remote server using
this can be executed in a single line as an ad hoc command as well.
ansible remoteserver1 -m copy -a "src=~/Downloads/index.html dest=/var/www/html owner=apache group=apache mode=0644"
Sending Notification to All clients
ansible/ansible.cfg
in your project directory (i.e. ansible.cfg
in the provisioning_path
on the target) with the following contents:[defaults]
host_key_checking = false
https://stackoverflow.com/questions/42462435/ansible-provisioning-error-using-a-ssh-password-instead-of-a-key-is-not-possibl
ANSIBLE NEW VERSION UPDATE
sudo -H pip install --upgrade ansible
courtesty : https://stackoverflow.com/questions/34903026/update-ansible-1-9-4-to-ansible-2-0
courtesy:https://www.youtube.com/watch?v=EcnqJbxBcM0
Comments
Post a Comment