Ansible is an
open-source software provisioning, configuration management, and
application-deployment tool. It runs on many Unix-like systems, and can
configure both Unix-like systems as well as Microsoft Windows. It includes its
own declarative language to describe system configuration. Ansible was written
by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless,
temporarily connecting remotely via SSH or Windows Remote Management (allowing
remote PowerShell execution) to do its tasks.
Platform support
Control machines have to be a Linux/Unix host (for example SUSE Linux Enterprise, Red Hat Enterprise Linux, Debian, CentOS, macOS, BSD, Ubuntu, and Python 2.7 or 3.5 is required.
Managed nodes, if they are Unix-like, must have Python 2.4 or later. For managed nodes with Python 2.5 or earlier, the python-simplejson package is also required. Since version 1.7, Ansible can also manage Windows nodes. In this case, native PowerShell remoting supported by the WS-Management protocol is used, instead of SSH.
Cloud integration
Ansible can deploy to bare metal hosts, virtualized systems and cloud environments, including Amazon Web Services, Atomic, CenturyLink, Cloudscale, CloudStack, DigitalOcean, Dimension Data, Docker, Google Cloud Platform, KVM, Linode, LXC, LXD, Microsoft Azure, OpenStack, Oracle Cloud, OVH, oVirt, Packet, Profitbricks, PubNub, Rackspace, Scaleway, SmartOS, SoftLayer, Univention, VMware, Webfaction, and XenServer.
Difference Between Ansible Vs Puppet Vs Chef
Ansible components:
Inventory:
The “inventory” is a configuration file where you define the host information. Below we have configured "webservers" group for our web-servers.
File:- /etc/ansible/hosts
Playbooks:
In most cases – especially in enterprise environments – you should use Ansible Playbooks. A playbook is where you define how to apply policies, declare configurations, orchestrate steps and launch tasks either synchronously or asynchronously on your servers. Each playbook is composed of one or more “plays”. Playbooks are normally maintained and managed in a version control system like Git. They are expressed in YAML (Yet Another Markup Language).
Plays:
– hosts: all
Tasks:
- name: Install Apache httpd
Handlers:
Handlers are similar to tasks except that a handler will be executed only when it is called by an event. For example, a handler that will start the httpd service after a task installed httpd. The handler is called by the [notify] directive. Important: the name of the notify directive and the handler must be the same.
Templates:
Templates files are based on Python’s Jinja2 template engine and have a .j2 extension. You can, if you need, place contents of your index.html file into a template file. But the real power of these files comes when you use variables. You can use Ansible’s [facts] and even call custom variables in these template files.
Roles:
A role is the Ansible way of bundling automation content and making it reusable. Roles are organizational components that can be assigned to a set of hosts to organize tasks. Therefore, instead of creating a monolithic playbook, we can create multiple roles, with each role assigned to complete a unit of work. For example: a webserver role can be defined to install Apache and Varnish on a specified group of servers.
Sample Playbook:
## PLAYBOOK TO INSTALL AND CONFIGURE APACHE HTTP ON Servers - hosts: all tasks: - name: Install Apache httpd yum: pkg=httpd state=installed notify: - Start Httpd handlers: - name: Start httpd service: name=httpd state=started
Contents:
1. Installation & configuration of Ansible on RHEL2. Configuring agent less Node as an Ansible Client3. Passwordless Authentication between Ansible Master & Ansible Client.4. Creating our first Playbook.5. Configuring resources on the agent based on below playbooks. Please refer below link.https://gitlab.com/Azam-devops/ansible
6. Handlers7. What are Roles in Ansible
8. Ansible Galaxy9. What are templates10. Overview on Ansible Tower
11. Ansible VaultInstallation of Ansible:
yum install epel-release oramazon-linux-extras install epel -yyum install ansible -yansible --version
Enable ansible logging by adding below line to /etc/ansible/ansible.cfg[defaults]log_path = ./ansible.logAlso add below line to dsiable host key verication[defaults]log_path = ./ansible.loghost_key_checking = FalsePasswordless Authentication between Master & the Client
Create a Private DNS server between Ansible Master & Clients.
Deploy an Ansible client & permit root login to "YES" in below file. Just un-comment the line[root@ip-Ansible-Client ~]# vi /etc/ssh/sshd_config[root@ip-Ansible-Client ~]# systemctl restart sshdGo to Ansible & generate public & private key for SSH connection between Ansible Host & Client.
vi /etc/ansible/hosts# Ex 2: A collection of hosts belonging to the 'webservers' group[webservers]ansibleclient1Using Ansible Simple Commandsansible -m ping allansible -m ping webserversansible -m ping ansibleclient1ansible -m shell -a 'free -m' ansibleclient1Follow below link & start using your own playbooks
https://gitlab.com/Azam-devops/ansible
ansible-playbook "Your_Playbook.yaml"
Command to create an Ansible Role
ansible-galaxy init "Your_Role_Name"yum install tree -ytree "Your_Role_Name"Go to your role
cd "your_role"
run *.yml file to execute your_role
ansible-playbook *.yaml or *.yml
Please check below link for all the sample files.https://gitlab.com/Azam-devops/ansibleHow to use Jenkins to run Playbooks
Install Ansible on Jenkins Master or Jenkins Slave, i.e whichever Machine you use to run the job.
Copy id_rsa.pub file of jenkins user from that Jenkins master/slave to theansibleclient's --> ec2-users --> authorized_keys to initiate passwordless authentication
between Jenkins master/slave & your ansibleclient. Assuming that "jenkins" user will goto ansibleclient as ec2-user & become root to run ansible commands.Check doing ssh from jenkins master/slave as jenkins user to ec2-user on ansibleclient
Once doneUpdate your *yaml files with below text to use ec2-user as remote user & become sudo toperform root tasks on Ansible-client. (If you need some other user other than root to performthis operation then create that user on ansibleclient & give sudo permissions. Then copyjenkins user's id_rsa.pub file to that ansibleclient user's .ssh/authorized_keys file)--- - hosts: webservers remote_user: ec2-user become: true become_method: sudo tasks: - name: Create a login user user: name: jordan password: 'supp0rt' state: present shell: /bin/bash # Defaults to /bin/bash createhome: yes # Defaults to yes home: /home/jordan # Defaults to /home/<username>Now, you can use Ansible playbooks in jenkins job to configure Ansible-Clients.
Note: Change log file location in /etc/ansible/ansible.cfg file after you use jenkins as an Ansible user.
It will not log in root directory unless you mention specific user's home directory for logging.Ansible Vault:-
Useful Links:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-centos-7
https://linuxacademy.com/blog/linux-academy/ansible-roles-explained/
https://cloudacademy.com/blog/ansible-aws/https://docs.ansible.com/ansible/latest/modules/shell_module.html
https://www.mydailytutorials.com/ansible-register-variables/
https://serverfault.com/questions/531004/where-do-i-find-the-latest-ansible-error-log
https://stackoverflow.com/questions/58772082/can-i-include-multiple-tasks-in-include-tasks-from-tasks-main-yml
https://stackoverflow.com/questions/24732627/ansible-roles-and-handlers-cannot-get-role-handlers-to-work
https://stackoverflow.com/questions/24851575/ansible-how-to-pass-multiple-commands
https://www.tecmint.com/ansible-variables-and-facts/
https://stackoverflow.com/questions/28489705/how-to-assign-a-random-number-to-a-variable-in-ansible/28490160
https://www.toptechskills.com/ansible-tutorials-courses/ansible-include-import-variables-tutorial-examples/
https://www.mydailytutorials.com/ansible-template-module-examples/
https://www.mydailytutorials.com/how-to-copy-files-and-directories-in-ansible-using-copy-and-fetch-modules/
How to use Jenkins user to configure other ec2 instances.
https://github.community/t/ansible-playbook-setup-py-permission-denied/11496
Ex:-
--- - hosts: my-test remote_user: myuser become: true become_method: sudo
Comments
Post a Comment