<<

ANSIBLE Environment Setup Ad- hoc Commands P l a y b o o k s

Types of machines: • General syntax of ad-hoc command: • It is the place where all YAML files are stored and CHEAT SHEET • Control machine : manages other machines Command hostngroup module/options[arguments] executed. Acts like a to-do list • Remote machine: controlled by other machines • YAML- yet another markup language Multiple remote systems can be handled by one machine. • A playbook can have more than one plays. Plays map the A n s i b l e • Remote machine managing is done by ansible by default. FUNCTION COMMANDS instructions defined against a particular host • Ansible doesn’t leave any software running on them. Therefore • Typically written in a text editor like notepad or • It is an open source engine that automates deployment, there is no need of an upgrade when moving to a newer notepad++ orchestration, cloud provisioning and other tools. Check connectivity of hosts #ansible -m ping version. Sample playbook/YAML file; • It uses a playbook to describe jobs and uses YAML which is human • Install it through , yumpkg, , OpenCSW name: install and configure DB readable • installing it through apt : Rebooting hosts #ansible -a “/bin/reboot” hosts: testServer • It is designed for multi- tier deployment. It is agentless and works by $ sudo apt-get update become: yes connecting nodes through ssh. $ sudo apt-get install software-properties-common Check host system’s info #ansible -m steup | less vars: oracle_db_port_value : 1521 $ sudo apt-add-repository ppa: ansible/ansible $ sudo apt-get tasks: update -name: Install the Oracle DB How Does it Work? #ansible -m copy -a $ sudo apt-get install ansible Transfering files : • Run ansible version to make sure it was installed properly. “src=home/ansible dest=/tmo/home” -name: Ensure the installed service is enabled • Connects nodes and pushes small programs called modules to them service: and are removed when they are done. #ansible -m user -a “name=ansible Create new user name: • The management node controls whole execution of the playbook. YAML password= ” • Tags of YAML: • The inventory file provides the list of hosts where the modules need • Name: name of the playbook to be run. #ansible -m user -a “name=ansible • YAML syntax is used to express ansible playbooks Deleting user • Hosts: specifies the list of hosts. Tasks can be • The management node does an ‘ssh’ connection and executes the • Key-value pair: state- absent” on the same machine or a different one. modules and installs the software. Dictionary is represented in key value pair • Vars: defines the variables which you can use Check if package is installed #ansible -m yum -a “name=httpd Ex: james: • Tasks: it is the list of action that needs to be name: james john and update it state=latest” performed. A task is always linked to a module. rollNo: 34 div: B Check if package is installed #ansible -m yum -a “name=httpd sex: male and dont update it state=present” V a r i a b l e s • Representing lists: • Each element has to be written in a new line with “-” Check if package is s #ansible -m yum -a “name=httpd- as the prefix specific version 1.8 state=latest” • Same as using variables in programming languages • countries: Ex: - hosts : - America Check if package is not #ansible -m yum -a “name= httpd • tomcat_port : 8080 - Iceland installed state= absent • Here tomcat_port is assigned to 8080 • Lists inside the dictionary: • Keywords used: #ansible -m service -a “name= • Block- ansible syntax to execute a block • name: james john Starting a service • rollNo: 34 httpd state=”started” • Name- name of the block • div: B • Action- the code that is to be executed #ansible -m service -a “name= • Register- registers the output • sex: male Stopping a service Troubleshooting • likes: httpd state=”stopped” • Always- states that below word will be run - english • Msg- displays the message #ansible -m service -a “name= • Exception handling: • Boolean terms are also used in YAML Restarting a service httpd state=”restarted” • Similar to any other programming language • Common strategies to debug playbooks are • Keywords : rescue and always • Debug and register Advantages of Ansible • The code is written in block • Use verbosity (verbosity level) • It goes to the rescue phase and gets executed • Playbook issues: • It is free and open source. T e r m s if the command in the block fails. • Quoting • Agentless. No master client model. • Thereby block is the same as “try block “, • Indentation • System requirements. catch block is like “ rescue” and always • Some drawbacks are: • Service/server- a process that provides service • performs the same function as we know. • OS restrictions: is OS dependent so code on one OS will Developed in python. • Machine - physical machine, Vm or a container not work for another • Lightweight and quick deployment. • Target machine - end machine to be configured by ansible • • Ansible uses YAML syntax in config files. • Once playbook is running, adding of hosts is not possible Task- an action FURTHERMORE: • • • Error reporting is mediocre. Large community base. Playbook - location where YAMl files are written and executed DevOps Certification Training Course