You are reading the article Guide To How Ansible Debug Work With Examples updated in December 2023 on the website Kientrucdochoi.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Guide To How Ansible Debug Work With Examples
Introduction to Ansible DebugIn Ansible, when we create and run playbooks, it’s very common that we run into an error due to some issues with our playbook. This can be a syntax error, a logical error or some mandatory parameter is missing. So, this is very important that we should always write our playbook in such have a way that it always prints enough information, be it successful or failure. In Ansible, a module named debug is the requirement when you need to debug and when you need more information in playbook execution output.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
In this article we will try to learn some concepts and see Ansible debug examples. This is a default module that comes with Ansible package. This module prints statements and variable’s values when executing playbooks. This can be very helpful in cases, where you can skip some erratic tasks in the playbook and don’t want to stop it altogether. Using debug in all the erratic tasks, will provide you enough information about the data execution in those tasks and all variables used in such tasks. This will help in troubleshooting and is very useful if you use it with registered variables. We will explore such cases in the example section.
How do Ansible Debug works?In Ansible debug module comes with some parameters and these parameters accept some options. This is given below:
msg: – This parameter accepts strings as inputs. This is used to print a customized message. If no message is given, then a generic message like “Hello World!” is
var: – This accepts strings as input and this is the variable that has been set either by Ansible facts or by the playbook. Also, the values written here will be having implicit double interpolation, as this option runs in the jinja2 context. So, you don’t need to use jinja2 delimiter unless you want to print double interpolation as well. You can use double interpolation when you print a variable in a
verbosity: – This has default as 0. This parameter is used to control when debug is in a run. For example if value 3 is given then debug will only run if -v or above is given while running the playbook.
Examples to Implement Ansible DebugNow we will take some examples, but before going there, we first understand our lab used for testing purposes. Here we have an Ansible control server named ansible-controller and two remotes hosts named host- one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and manage the users on remote hosts.
Examples #1To print a default message on the output of running a playbook via ansible-playbook, we can create a simple playbook like below:
Code:
name: Here we use Ansible debug debug:
Output:
ansible-playbook debug_dafault_msg.yml
Example #2To print the value of a variable that was defined in the same playbook. we can create a simple playbook like below:
Code:
var: fruit
Output:
ansible-playbook debug_print_var.yml
Example #3To print an Ansible fact of remote hosts, we can create a simple playbook like
Code:
Output:
ansible-playbook debug_ansible_fact.yml
Example #4To print a customized message on the screen, Write a playbook like below:
Code:
msg: “This is machine is {{ ansible_hostname }} with IP {{ ansible_default_ipv4.address }}”
Output:
ansible-playbook debug_with_customized_msg.yml
Example #5You can register the output of a task in a variable and as we know the values of these variables will be stored in JSON format. So, you can call that variable later in the same playbook. To test this or to test which value will be used when we call registered variables. We can use debug like below, creating a playbook.
Code:
Output:
ansible-playbook debug_register_values.yml
Example #6Controlling Verbosity is done by giving values against parameter verbosity and then mentioning the same or more number of –v while running the playbook. Like the playbook in previous. Where we didn’t define any verbosity, then by default it ran with 0 verbosity level. But if define the verbosity level like below:
Code:
var: free_mem.stdout_lines verbosity: 1
Output:
ansible-playbook debug_register_values.yml -v
ConclusionAnsible debug module is a very helpful tool for playbook developers and for administrators who work on need to update a playbook frequently on per need basis. Also, while working in a team where others also have the same stakes as you, using the debug module to add more information is always beneficial and can avoid confusion and dependencies.
Recommended ArticlesThis is a guide to Ansible Debug. Here we discuss an introduction to Ansible Debug, how does it work with examples. You can also go through our other related articles to learn more –
You're reading Guide To How Ansible Debug Work With Examples
Learn How Untar Works In Ansible With Examples
Introduction to Ansible untar
Ansible untar module also known as unarchive for Unix OS or win_zip for the windows operating system is used to extract or unpack the archive files or folders on the remote destination by copying the archive file first by default on the remote server and also supports many parameters to deal with the unarchive or win_zip functionality like the owner, dest, exclude and more.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
SyntaxFor the non-windows target unarchive or ansible.builtin.unarchive command uses the below syntax. We have shown the basics parameters
Parameters:
copy: No / Yes (Default)
The default Option is Yes and it copies the files from the local controller to the remote host if specified yes or nothing specified. For working with the remote source need to specify the remote_src and this copy parameter is not helpful there.
src (required):
Source path of the file. If remote_src is ‘yes’ then the source path would be on the remote location otherwise the source path is on the local controller node.
dest (required):
Remote server absolute path where the archive is going to extract.
remote_src: no (Default) / yes
If set ‘yes’ specifies that the source archive path is on the remote computer. For the ‘no’ value the path is on the local controller node.
owner:
name of the user that should own the files/directory after unpack and same replicates (added) to the chown.
group:
Name of the group that should own the files/directory and same replicates (added) to the chown.
exclude:
List of files or directories that need to be excluded from the unpacking.
include:
List of files or directories that need to be included in the unpacking process.
keep_newer: no (default) / yes
If set yes, it replaces the existing files that are newer than the files inside the archive.
mode:
Permissions that the files or directories should have after unpacked (like 644, 777, etc).
For the windows target src, dest, creates parameters remain the same as unarchive module and the additional parameters are as below.
Parameters:
delete_archive: no (default) / yes
Removes the zip file after unzipping.
password
If the Zip file is password encrypted then this parameter is useful and it requires the PSCX module to be installed.
recurse: no (default) / yes
recursively expands the zip file within the src directory.
Setting value ‘yes’ requires the PSCX module to be installed.
Please note: the win_unzip module doesn’t use the remote_src parameter.
How untar works in Ansible?Ansible unarchive module for non-windows target comes with the ansible-base and so it is included in the Ansible installations. For the windows target, we need to use the win_unzip module. If the module is not available then you can download it from the galaxy.
ansible-galaxy collection install community.windows
Below is the simple playbook for windows untar (unzip).
dest: c:temp7zipInstaller
Output:
The above playbook will unzip the file from the source location to the destination remote servers. If the destination path doesn’t exist it creates the destination path.
You must have noticed here, although the source path is remote, we don’t need to use remote_src because the win_unzip module doesn’t support it and when we use the remote source path for the non-target windows server we must specify the remote_src. For example,
remote_src: yes
ExamplesHere are the following examples mention below
Example #1 – unarchive module task for UNIX os.dest: /tmp/phyes
In this playbook, it will retrieve the chúng tôi file to the destination remote host /tmp/phyes. Before running this task unlike the win_unzip module, you need to make sure that the destination path exists otherwise it will throw an error.
Output:
Example #2 – Exclude files from the archive.To exclude the certain file from the unpacking we can use h
Code:
exclude: ’Test1 document.txt’
If there are multiple files to exclude then you can provide the list as below.
- ’sourcefile.py’
Example #3 – Include files from the archiveCode:
include: ’Test1 document.txt’
The above playbook will include only the test1 text file and to include the multiple documents, use the below command.
- ’sourcefile.py’
Example #4 – using multiple parameters together.Code:
owner: ansibleadmin
In the above playbook, it will include only 2 files, keep_newer parameter will not replace the existing files that are newer than files from the archive, remote_src indicates the remote source and it will set the owner permission on the files and it will keep the file permission 0644.
ConclusionUntar or unarchive or unzip modules are very useful when we write the playbook. It makes it easier to extract the files or folders on the destination server without using any third-party software and in addition, it uses various parameters like we don’t need to copy the file before extracting, adding permissions after extract, etc.
Recommended ArticlesThis is a guide to Ansible untar. Here we discuss How untar works in Ansible and Examples along with the codes and outputs. You may also look at the following articles to learn more –
How Do Variables Work In Xslt With Examples?
Definition of XSLT Variable
XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables. The scope of the element is done by the element that contains it. While we are setting a variable it could be done as global and local variables. The top-level element declared in the file is named as a global variable and the local variables are assigned within the template section.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Following is the syntax declaration of the variable element.
How do variables work in XSLT?The following templates assign the variable which includes both text and parameter values. The output is generated by referring to the value of the variable.
A variable takes a lot of types of values like integer or declared within XPath type expressions.
With Integer
With String
With Input document
For instance, let’s take a scenario like we need to output the book title of a respective Document in various places. We can do with the XSL variable and the book title is changed provided the changes done in one preferred location in the XSLT file. For example, we may need to use the value of current-time twenty times. Instead of passing a call to current-time(), we can call once and the corresponding value is stored in a variable.
ExamplesBelow example create a demo on two files XSL and XML with their elements and their child elements in the XML file and matches them with the XSL variable name. Let’s get started with the first demo.
Example #1 – Here is our XML file taking Book detailsFollowing this file we have XSL style sheets with a template match by assigning a variable name to the values.
Explanation
Here an XML file is converted into a new XML file by assigning a variable name simultaneously I have incremented the variable many times when need with the new variable. The above code uses a global variable and could be accessed throughout the chúng tôi supports three modes of Output methods XML, HTML and Text. Here I have used HTML to show. Well, we can see the output like this:
Output:
Example #2XML file
Xslt file
Explanation
As a result of adding this stylesheet and applying this rule of variable assignments, the output is generated. The resulting Output is given as follows:
Output:
Example #3Xml file
XSL file
Here is a page
Explanation:
The above code uses three variable names and all the values are been assigned.
Output:
AdvantagesXSL variables are very useful in many circumstances.
Variables help in avoiding long typing of XPath expression in case of complicated instructions.
XSL Being a Formatting language used for many XML Applications by providing elements and variable names with local or global declarations where they exclusively focus on formatting Objects.
ConclusionXSLT is gaining much importance in business logic and produces a few best practices in achieving a good result in XSL. Therefore in this article, we have seen how variables are declared with the example.
Recommended ArticlesThis is a guide to XSLT Variable. Here we discuss the definition, syntax, How do variables work in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –
Guide On How Does Ansible Synchronize Works
Introduction to Ansible Synchronize
Web development, programming languages, Software testing & others
Explaining the Ansible SynchronizeAnsible synchronizes work like rsync in many ways. But we must remember the below points while using it in our environments. These will explain this module and show its limitations:
To work with this module, rsync must be installed on both source and target systems.
By default, the source of files is localhost i.e. the controller machine and destination of files is the machine where the connection is made to transfer files. This default feature can be changed by using parameter delegate_to, which allows you to change your source from localhost to some other host. Thus you can copy files from a remote machine to another remote machine.
On synchronize source machine, file’s permissions are of the user who is running the tasks on localhost or remote_user in the case when delegate_to is used.
On synchronize destination machine, the file’s permission will be of remote_user on destination host or of become_user if become=yes is given in parameters, but to elevate the permission, password less sudo should be set up, as rsync itself does not give a way to pass sudo credentials.
Currently, we have only below few connection types to work with Ansible synchronize viz are ssh, paramiko, local, and docker.
Always give the full path of the destination host location, as there may be cases where you used sudo, but files will be copied to remote_user home directory.
Linux rsync limitations related to hard links are also applied here.
Ansible synchronizes module forces -delay-updates to avoid the broken state in case of connection failure.
How does Ansible Synchronize Works?Ansible synchronize user below parameters and their acceptable values are listed as below. Using the combination of these parameters, we can decide the behavior and output of Ansible synchronize.
archive: The acceptable values are yes and no. The default value is set to yes. This mirrors the rsync flag, enable recursive, links, perm, group, owner, time flag.
hecksum: The acceptable values are yes and no. The default value is set to no. This is used to skip based on checksum.
compress: The acceptable values are yes and no. The default value is set to yes. This is used to compress files during transfer to speed up the transfer.
copy_links: The acceptable values are yes and no. The default value is set to no. This is used to copy the referenced items rather than links.
delete: The acceptable values are yes and no. The default value is set to no. This is used to delete files in dest location, which does not exist on the source somehow when the transfer is completed. This works when recursive=yes is set.
dest: The absolute or relative path on the destination machine. Which will be synced from the source.
src: The absolute or relative path on the source machine. Which will be synced from the destination.
dirs: The acceptable values are yes and no. The default value is set to no. This is used to transfer directories without recursive.
dest_port: Port number of ssh on destination.
link_dest: Default value is null. This is used to add a destination to hard links against during the rsync.
links: This is used to copy syslinks as syslinks, not referenced items.
mode: Acceptable values are push and pull. The default value is push.
owner: The acceptable values are yes and no. This is used to preserve owner.
rsync_path: This is used to specify the rsync command path on remote hosts.
times: To preserve the modification times. The acceptable values are yes and no.
Examples of Ansible SynchronizeNow by using examples, we will try to learn about Ansible synchronize, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose. Here we have an Ansible control server named ansible-controller and few remote hosts named host- remote, host-one, and host-two. We will create playbooks and run Ansible commands on the ansible- controller node and see the results on the remote hosts.
In this example, we will do the synchronization of files from source machine viz. Ansible controller node to the remote host. We have a set of files under /var/tmp/sync_folder on the Ansible controller node which will be transferred to remote host via Ansible synchronize module. For this we have a playbook like below:
src: /var/tmp/sync_folder dest: /var/tmp/
ansible-playbook ansible_synchronize.yaml
ls -l
In this example, we will do the synchronization of files from one remote host to another remote host. For this we have a playbook like below:
src: /var/tmp/sync_folder dest: /var/tmp/ delegate_to: host-two
On the remote machine, we can cross-check to confirm the files have been transferred.
ls -l
ConclusionAs we saw, Ansible synchronize is powerful but easy to use the module, but we must also acknowledge that this is not the replacement of rsync in Linux systems. So keeping in mind, you must also remember all the limitations mentioned above in this article to avoid unexpected outcomes. So learn it first and then use it carefully.
Recommended ArticlesThis is a guide to Ansible Synchronize. Here we also discuss the Introduction and how does ansible synchronize works along with examples and its code implementation. You may also have a look at the following articles to learn more –
How Does Date Function Work In Xml With Examples?
Definition on XML Date
XML Date is defined as a data type to specify the date in the XML Documents. According to W3C XML Schema, a date is a period of one day in its time zone. The date rule could be customized according to the requirements where sometimes it is necessary to do some date operations or parse dates to a specific format provided time zone should be specified if not local time is used. And the date format is defined by date pattern Strings by assigning letters from A to Z.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
In this article, we have used a Class XML Gregorian Calendar for the data Utilities.
Syntax
The XML format for Date is given in the following formats
CCYY-MM-DDWhere the data type specifies Year, Month, and Day. It’s a finite-length sequence of characters with yy-mm-dd. The four-digit numeric represents a year, the negative value is restricted, and the year 0000 is neglected. And there is a symbol ‘- ‘separator between this format; finally, two-digit numerals signify month and date.
The element declaration is illustrated as
The current date on Xpath is defined as
xs:date fn:current-date() How does Date function work in XML?Generally, this Date function includes Data types like
xs:dateTime (CCYY-MM-DDThh:mm:ss)
xs:date (CCYY-MM-DD)
xs:time (hh:mm:ss)
xs:gYearMonth (CCYY-MM)
xs:gYear (CCYY)
xs:gMonthDay (–MM-DD)
xs:gMonth (–MM–)
xs:gDay (—DD)
and XML formatter prefers to use Simple Date Format, and it is not thread-safe. Also, this constructor is not supported in all the local files. The format comes like this:
public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)The Gregorian Calendar is specified as
2011-11-21
2011-11-21 +02:00
2011-11-21 Z
2011-11-21+00:00
2011-11-21
2011-02-03
The value of the datetime given is parsed by the XML parser, which converts the datetime value declared in the input XML to the value of the local time zone format. Thus, even a daylight-saving option could be made.
Using the Current date() function to display the current date of that day. This function is called without passing any parameters. As a result, it returns the date manipulated from the System time, i.e., gives out a constant value. Let’s see a simple working code of the XSD file.
<xsl: stylesheet version=”2.0″ The current date today is:
And in Xpath, we have like
And Few functions on Date are dateTime (), year-from-dateTime (), year-from-date(date), month-from-date(date) , day-from-date(date).
Next, for the sample XML schema, the customization of XML date and time is specified as
public class Client { @XmlElement(name = "dob") @XmlSchemaType(name = "date") protected XMLGregorianCalendar dateOfBirth; public XMLGregorianCalendar getDateOfBirth() {return dateOfBirth;} public void setDateOfBirth(XMLGregorianCalendar value) {this.dateOfBirth = value; } } ExamplesNow let’s see how this function works well in XSLT and java.
Example#1 – Simple XML file displays the datechúng tôi
Output:
Example#2Educ.java
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.GregorianCalendar; import javaxxml.datatype.DatatypeConfigurationException; import javaxxml.datatype.DatatypeFactory; import javaxxml.datatype.XMLGregorianCalendar; public class Educ { private final static String TIMESTAMP_PATTERN = "MM/dd/yyyy hh:mm a z"; private final static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(TIMESTAMP_PATTERN); public static void main(String[] args) throws DatatypeConfigurationException { GregorianCalendar c = new GregorianCalendar(); c.setTime(new Date()); XMLGregorianCalendar xc = DatatypeFactory.newInstance() .newXMLGregorianCalendar(c); ZonedDateTime zon = xc.toGregorianCalendar().toZonedDateTime(); System.out.println( DATE_TIME_FORMATTER.format(zon) ); ZonedDateTime zond = zon.withZoneSameInstant(ZoneId.of("UTC")); System.out.println( DATE_TIME_FORMATTER.format(zond) ); } }Explanation:
Output:
Example#3 – Showing java code to convert Date Object to String Value Using Gregorian CalendarRule.java
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import javaxxml.datatype.DatatypeConfigurationException; import javaxxml.datatype.DatatypeFactory; import javaxxml.datatype.XMLGregorianCalendar; public class Rule { public static void main(String[] args) throws DatatypeConfigurationException { GregorianCalendar gca = new GregorianCalendar(); gca.setTime(new Date()); XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gca); System.out.println(convertXmlGregorianToString(xgc)); } public static String convertXmlGregorianToString(XMLGregorianCalendar xgc) { DateFormat datef = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); GregorianCalendar gCalendar = xgc.toGregorianCalendar(); Date dd = gCalendar.getTime(); String dStr = datef.format(dd); return dStr; } }Explanation:
The above java code converts an XML file document into java code by converting their data objects to display date. It uses simple packages like Date Format and Simple Date Format.
Output:
Example#4 – Using a Style sheet to give out the date..xsl
chúng tôi
Explanation:
Here we have used an XML file with various elements, and XSLT is created,
Output:
Example#5 – Xml date using C#Code:
using System; using System.IO; using System.Xml; public class test { public static void Main() { Int16 cid = 3252; String oID = "3524f5"; DateTime orderDate = new DateTime(); orderDate = DateTime.Now; Double price = 20.95; XmlTextWriter writer = new XmlTextWriter (Console.Out); writer.Formatting = Formatting.Indented; writer.WriteStartElement("order"); writer.WriteAttributeString("date", XmlConvert.ToString(orderDate, "yyyy-MM-dd")); writer.WriteAttributeString("time", XmlConvert.ToString(orderDate, "HH:mm:ss")); writer.WriteElementString("orderID", oID); writer.WriteElementString("custID", XmlConvert.ToString(cid)); writer.WriteElementString("price", XmlConvert.ToString(price)); writer.WriteEndElement(); writer.Close(); } }Explanation:
The above code automatically generates the current date from the System, which is shown below. The XML is written using the writer methods, where it starts from the root element ‘order.’
Output:
ConclusionTherefore this article shows how to apply Date Format value types in java and also taught us the customization formats by changing the different settings as explained in the working sections.
Recommended ArticlesThis is a guide to XML Date. Here we discuss how the XML date function works well in XSLT and java, along with different examples and its code implementation. You may also have a look at the following articles to learn more –
How To Use Isblank With Examples
ISBLANK Function
Checks if a specified cell is blank or not
Written by
CFI Team
Published July 2, 2023
Updated July 7, 2023
What is Excel ISBLANK Function?The ISBLANK Function[1] is an Excel Information function that returns true if the argument cell has no information in it. ISBLANK checks a specified cell and tells us if it is blank or not. If it is blank, it will return TRUE; else, it will return FALSE. The function was introduced in MS Excel 2007.
In financial analysis, we deal with data all the time. The ISBLANK function is useful in checking if a cell is blank or not. For example, if A5 contains a formula that returns an empty string “” as a result, the function will return FALSE. Thus, it helps in removing both regular and non-breaking space characters.
However, if a cell contains good data, as well as non-breaking spaces, it is possible to remove the non-breaking spaces from the data.
Formula=ISBLANK(value)
Where:
Value (required argument) is the value that we wish to test. (This function takes in a cell)
How to use the Excel ISBLANK FunctionAs a worksheet function, ISBLANK can be entered as part of a formula in a cell of a worksheet. To understand the uses of this function, let us consider a few examples:
Highlight Missing Values – ExampleSuppose we are given the following data:
Suppose we wish to highlight cells that are empty. We can use the ISBLANK coupled with conditional formatting. For example, suppose we want to highlight the blank cells in the range A2:F9, we select the range and use a conditional formatting rule with the following formula: =ISBLANK(A2:F9).
How to do conditional formatting?The input formula is shown below:
We will get the results below.
Conditional formatting didn’t highlight cell E5. After checking, there is a formula inserted into the cell.
The Excel ISBLANK function will return TRUE when a cell is actually empty. If a cell is an empty string (“”), ISBLANK will return FALSE, as it is not technically blank, and it won’t be highlighted as shown above.
Extracting the first NON-Blank value in an arraySuppose we wish to get the first non-blank value (text or number) in a one-row range. We can use an array formula based on the INDEX, MATCH, and ISBLANK functions.
We are given the data below:
Here, we want to get the first non-blank cell, but we don’t have a direct way to do that in Excel. We could use VLOOKUP with a wildcard *, but that will only work for text, not numbers.
Hence, we need to build the functionality by nesting formulas. One way to do it is to use an array function that “tests” cells and returns an array of TRUE/FALSE values that we can then match with MATCH. Now MATCH looks for FALSE inside the array and returns the position of the first match found, which, in this case, is 2. Now the INDEX function takes over and gets the value at position 2 in the array, which, in this case, is the value PEACHES.
As this is an array formula, we need to enter it with CTRL+SHIFT+ENTER.
We get the results below:
Additional ResourcesThanks for reading CFI’s guide to important Excel functions! By taking the time to learn and master these functions, you’ll significantly speed up your financial modeling and valuation analysis. To learn more, check out these additional CFI resources:
Update the detailed information about Guide To How Ansible Debug Work With Examples on the Kientrucdochoi.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!