Ansible – Environment Setup ”; Previous Next In this chapter, we will learn about the environment setup of Ansible. Installation Process Mainly, there are two types of machines when we talk about deployment − Control machine − Machine from where we can manage other machines. Remote machine − Machines which are handled/controlled by control machine. There can be multiple remote machines which are handled by one control machine. So, for managing remote machines we have to install Ansible on control machine. Control Machine Requirements Ansible can be run from any machine with Python 2 (versions 2.6 or 2.7) or Python 3 (versions 3.5 and higher) installed. Note − Windows does not support control machine. By default, Ansible uses ssh to manage remote machine. Ansible does not add any database. It does not require any daemons to start or keep it running. While managing remote machines, Ansible does not leave any software installed or running on them. Hence, there is no question of how to upgrade it when moving to a new version. Ansible can be installed on control machine which have above mentioned requirements in different ways. You can install the latest release through Apt, yum, pkg, pip, OpenCSW, pacman, etc. Installation through Apt on Ubuntu Machine For installing Ansible you have to configure PPA on your machine. For this, you have to run the following line of code − $ sudo apt-get update $ sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install ansible After running the above line of code, you are ready to manage remote machines through Ansible. Just run Ansible–version to check the version and just to check whether Ansible was installed properly or not. Print Page Previous Next Advertisements ”;
Category: Ansible
Ansible – YAML Basics
Ansible – YAML Basics ”; Previous Next Ansible uses YAML syntax for expressing Ansible playbooks. This chapter provides an overview of YAML. Ansible uses YAML because it is very easy for humans to understand, read and write when compared to other data formats like XML and JSON. Every YAML file optionally starts with “—” and ends with “…”. Understanding YAML In this section, we will learn the different ways in which the YAML data is represented. key-value pair YAML uses simple key-value pair to represent the data. The dictionary is represented in key: value pair. Note − There should be space between : and value. Example: A student record — #Optional YAML start syntax james: name: james john rollNo: 34 div: B sex: male … #Optional YAML end syntax Abbreviation You can also use abbreviation to represent dictionaries. Example James: {name: james john, rollNo: 34, div: B, sex: male} Representing List We can also represent List in YAML. Every element(member) of list should be written in a new line with same indentation starting with “- “ (- and space). Example — countries: – America – China – Canada – Iceland … Abbreviation You can also use abbreviation to represent lists. Example Countries: [‘America’, ‘China’, ‘Canada’, ‘Iceland’] List inside Dictionaries We can use list inside dictionaries, i.e., value of key is list. Example — james: name: james john rollNo: 34 div: B sex: male likes: – maths – physics – english … List of Dictionaries We can also make list of dictionaries. Example — – james: name: james john rollNo: 34 div: B sex: male likes: – maths – physics – english – robert: name: robert richardson rollNo: 53 div: B sex: male likes: – biology – chemistry … YAML uses “|” to include newlines while showing multiple lines and “>” to suppress newlines while showing multiple lines. Due to this we can read and edit large lines. In both the cases intendentation will be ignored. We can also represent Boolean (True/false) values in YAML. where boolean values can be case insensitive. Example — – james: name: james john rollNo: 34 div: B sex: male likes: – maths – physics – english result: maths: 87 chemistry: 45 biology: 56 physics: 70 english: 80 passed: TRUE messageIncludeNewLines: | Congratulation!! You passed with 79% messageExcludeNewLines: > Congratulation!! You passed with 79% Some common words related to Ansible. Service/Server − A process on the machine that provides the service. Machine − A physical server, vm(virtual machine) or a container. Target machine − A machine we are about to configure with Ansible. Task − An action(run this, delete that) etc managed by Ansible. Playbook − The yml file where Ansible commands are written and yml is executed on a machine. Print Page Previous Next Advertisements ”;
Ansible – Variables
Ansible – Variables ”; Previous Next Variable in playbooks are very similar to using variables in any programming language. It helps you to use and assign a value to a variable and use that anywhere in the playbook. One can put conditions around the value of the variables and accordingly use them in the playbook. Example – hosts : <your hosts> vars: tomcat_port : 8080 In the above example, we have defined a variable name tomcat_port and assigned the value 8080 to that variable and can use that in your playbook wherever needed. Now taking a reference from the example shared. The following code is from one of the roles (install-tomcat) − block: – name: Install Tomcat artifacts action: > yum name = “demo-tomcat-1” state = present register: Output always: – debug: msg: – “Install Tomcat artifacts task ended with message: {{Output}}” – “Installed Tomcat artifacts – {{Output.changed}}” Here, the output is the variable used. Let us walk through all the keywords used in the above code − block − Ansible syntax to execute a given block. name − Relevant name of the block – this is used in logging and helps in debugging that which all blocks were successfully executed. action − The code next to action tag is the task to be executed. The action again is a Ansible keyword used in yaml. register − The output of the action is registered using the register keyword and Output is the variable name which holds the action output. always − Again a Ansible keyword , it states that below will always be executed. msg − Displays the message. Usage of variable – {{Output}} This will read the value of variable Output. Also as it is used in the msg tab, it will print the value of the output variable. Additionally, you can use the sub properties of the variable as well. Like in the case checking {{Output.changed}} whether the output got changed and accordingly use it. Exception Handling in Playbooks Exception handling in Ansible is similar to exception handling in any programming language. An example of the exception handling in playbook is shown below. tasks: – name: Name of the task to be executed block: – debug: msg = ”Just a debug message , relevant for logging” – command: <the command to execute> rescue: – debug: msg = ”There was an exception.. ” – command: <Rescue mechanism for the above exception occurred) always: – debug: msg = “this will execute in all scenarios. Always will get logged” Following is the syntax for exception handling. rescue and always are the keywords specific to exception handling. Block is where the code is written (anything to be executed on the Unix machine). If the command written inside the block feature fails, then the execution reaches rescue block and it gets executed. In case there is no error in the command under block feature, then rescue will not be executed. Always gets executed in all cases. So if we compare the same with java, then it is similar to try, catch and finally block. Here, Block is similar to try block where you write the code to be executed and rescue is similar to catch block and always is similar to finally. Loops Below is the example to demonstrate the usage of Loops in Ansible. The tasks is to copy the set of all the war files from one directory to tomcat webapps folder. Most of the commands used in the example below are already covered before. Here, we will concentrate on the usage of loops. Initially in the ”shell” command we have done ls *.war. So, it will list all the war files in the directory. Output of that command is taken in a variable named output. To loop, the ”with_items” syntax is being used. with_items: “{{output.stdout_lines}}” –> output.stdout_lines gives us the line by line output and then we loop on the output with the with_items command of Ansible. Attaching the example output just to make one understand how we used the stdout_lines in the with_items command. — #Tsting – hosts: tomcat-node tasks: – name: Install Apache shell: “ls *.war” register: output args: chdir: /opt/ansible/tomcat/demo/webapps – file: src: ”/opt/ansible/tomcat/demo/webapps/{{ item }}” dest: ”/users/demo/vivek/{{ item }}” state: link with_items: “{{output.stdout_lines}}” Blocks The playbook in totality is broken into blocks. The smallest piece of steps to execute is written in block. Writing the specific instruction in blocks helps to segregate functionality and handle it with exception handling if needed. Example of blocks is covered in variable usage,exception handling and loops above. Conditionals Conditionals are used where one needs to run a specific step based on a condition. — #Tsting – hosts: all vars: test1: “Hello Vivek” tasks: – name: Testing Ansible variable debug: msg: “Equals” when: test1 == “Hello Vivek” In this case, Equals will be printed as the test1 variable is equal as mentioned in the when condition. when can be used with a logical OR and logical AND condition as in all the programming languages. Just change the value of test1 variable from Hello Vivek to say Hello World and see the output. Print Page Previous Next Advertisements ”;