Trending February 2024 # How Does Find Module Work In Ansible With Examples? # Suggested March 2024 # Top 4 Popular

You are reading the article How Does Find Module Work In Ansible With Examples? updated in February 2024 on the website Flu.edu.vn. 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 March 2024 How Does Find Module Work In Ansible With Examples?

Introduction to Ansible find

Ansible find module or command as the name suggests is used to search the file, directory, or link recursively if specified and based on the single or multiple criteria like age, regular expression, or pattern, and this module is a part of the ansible-base which is included default in the ansible installation which generally works with the Unix system and to use this module with the Windows system we need to use the ansible windows module, ansible.windows.win_find.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Ansible find

The syntax for this module:

“find:”

And they can be used with single or multiple parameters, as shown below. Not all parameters are covered, but only those which are required in the regular search operation.

1. paths: List of paths to include for the search. All paths should be fully qualified.

Selects the files equal or greater than the age specified in time.

Negative age searches the files equal or less than the age specified in time.

Age can be in seconds, minutes, hours, days, or weeks (e.g. 1w – 1 week, 2d – 2days).

3. recurse: Default No.

If specified yes, then the directory searches the files recursively.

4. file_type: Searches the type based on the choice below:

file (default)

directory

link

any

5. excludes: Excludes the items based on the string or the specified pattern.

6. depth: Maximum level of the depth to search for. By default is unlimited, and if the specified recurse parameter value is no, then the depth is 1.

7. contains: Only works with the file_type=file to search the files based on their content.

8. pattern: Searches the items based on the regex pattern.

9. size:

Selects the files based on the size equal to or greater than the specified size.

If the size is negative, then find the files equal or less than the specified size.

The default size is in bytes. However, you can explicitly mention (b=bytes, k=kilobytes, m=megabytes, g=gigabytes, t=terabytes).

10. hidden: The default value is no. If specified, yes, it includes hidden files.

11. read_whole_file: The default value is no. If specified yes, the whole file is read into memory instead of the line-by-line pattern, which can impact the server performance if the file size is large.

How does find Module Work in Ansible?

The find module ships with the ansible-base, which means it is included by default for the in ansible installation, and as described in the above syntax, we can use the various parameters to search files, directories, or links.

When we work with the windows search for the items, we need to use ansible.windows.win_find module, which uses almost the same parameters as specified above.

The following playbook will search the files from the Windows servers c:temp folders.

Code:

paths: c:temp

Output:

When you run the above playbook, you won’t get any output because the output is not stored anywhere. To store the retrieved output, we need to use the register variable as shown below.

Code:

msg: “{{ myfiles }}”

Output:

Code:

Now you can see the file names in the output.

Output:

Examples of Ansible find

Given below are the examples of Ansible find:

Example #1

Recursively search the /etc. folder files older than 1 week and greater than 1kb.

Code:

Output:

Example #2

Search the *.ps1, *.txt files.

The below playbook will search the files on the multiple folders with the specific extensions (ps1 and txt) and whose size is less than 1mb, store the output in the variable, and manipulate the output by the JSON query.

Code:

An alternate way, you can use the regex pattern to search the files as shown below.

Code:

use_regex: yes

Output:

Example #3

Search all the directories from the root folder, including the hidden directories.

Code:

The above playbook searches the root folder directories, including hidden directories.

Example #4

Exclude folders.

The below playbook will search all the directories from the /etc. folders and will exclude 3 directories (python3, selinux, and lvm).

Code:

Conclusion

Ansible builtin module find and the windows module win_find work like the shell, batch, or PowerShell language that we use for searching the files or directories, but we don’t need to manipulate commands in ansible to get the desired search result; but instead, we can use the parameters that directly help us to search the files and the folders like file age, size, etc.

Recommended Articles

This is a guide to Ansible find. Here we discuss the introduction; how does find module work in ansible? And examples, respectively. You may also have a look at the following articles to learn more –

You're reading How Does Find Module Work In Ansible With Examples?

How Does Ansible Fetch Work? (Examples)

Introduction to Ansible Fetch

Ansible fetch module is a file-based module, which is intended to work on files. This module is similar to Ansible copy module, but by default works in reverse order, in terms of source and destination. It is one of such modules that you might need on day to day basis while working on administrative or change management tasks on remote target machines. There are many other modules like a copy, template, etc. which works more or less in a similar way, there usability completely depends on the available feature parameters. In this article, we will learn about the Ansible Fetch module by using some examples and details on parameters that are usable under this module.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is Ansible Fetch?

This module will treat remote machines as a source of files and local controller machines as a destination.

Fetched files are organized by hostname, in a file tree structure.

Files with different contents but the same name are overwritten on destination.

This module works as it is, for Microsoft Windows machines as well.

It is suggested to not use this module with Ansible to become parameters as this will lead to doubling the size of the transfer file. As checksum will also be calculating. This will then lead to consuming all available memory on remote or local hosts causing MemoryError.

When reading of remote files is not possible, then we shall use fail_whenor ignore_errorsor fail_on_missing, to avoid failure of the playbook.

How Does Ansible Fetch Work?

To efficiently use Ansible fetch module, one much learn about all available parameters, their acceptable values. Also, there are some parameters that have default values set always, which will be realized to your playbook even if you do not mention the related parameter in the playbook. So these become more important to know. Below is a list of all available parameters and related acceptable or default values.

dest: This is to set the directory where fetched files will be saved. Point to note that under this directory another directory will be created which is named after the hostname of target remote systems, but based of inventory entries. Under this directory, the whole directory structure as a source will be created and then file will be copied into it.

fail_on_missing: Default is yes. This will cause the playbook failure when Ansible is unable to read files on source systems, reason can be anything like permission, non-existence, etc. Available values are yes and no.

flat: Default is no. Acceptable values are yes and no. This is to set the override the default behavior of the fetch module. Thus to behave in such a way where files will be copied directly to destination location without creating hostname-based directory structure.

src: To give the file name on remote systems to fetch. This must be a filename, not any directory name, as fetching of the directory is not supported yet.

validate_checksum: Default is yes. Acceptable values are yes and no. To verify the source and destination checksum of files after fetching.

Examples to Implement Ansible Fetch

In this section, we will learn by doing looking at some examples where we tried to test the functionality of the Ansible reboot module. But we shall know about our lab environment first before moving ahead in this section.

Here we have one Ansible controller node named as ansible-controller. As target nodes, we have two remote machines. First machine is a Red Hat Enterprise Linux machine named as host-one and the second machine is an Ubuntu machine named as host-two. We will run our playbooks on the Ansible controller machine and make changes on remote target machines.

Example #1

In this example, we have a playbook that is used to fetch a file from remote target nodes and store on the local Ansible controller machine. Then we will check on the local Ansible controller machine that how the file is stored and what directory tree structure is created for this.

Code:

dest: /tmp/fetched

Then running this playbook like below:

# ansible-playbook ansible_fetch.yaml

Now in the output, we can see that files are fetched and stored on Ansible local machine in a directory structure.

Output:

Upon checking the directory structure, we found that a full directory structure has been created on the local machine.

Also, check the timestamp of files and directories.

Example #2

In this example, we have a playbook that is used to fetch a file from remote target nodes and store on local Ansible controller machine. Here we have used a parameter named flat=yes. This is used to override the default behavior of the Ansible fetch module and copy file as it is with name and under path mentioned in the dest parameter. Then we will check on the local Ansible controller machine that how the file is stored and what directory tree structure is created for this.

Code:

Then running this playbook like below: –

# ansible-playbook ansible_fetch_flat.yaml -v

Output:

Then stored on Ansible local machine in the same directory with a new name and no new directory structure is created.

Conclusion

As we saw in this article, Ansible fetch module is easy to use though a very useful module, which you will definitely need in your Ansible skill set. Also having knowledge of its all available features and possibilities will enable you to use it to full extend. So learn it first and then use it.

Recommended Article

This is a guide to Ansible Fetch. Here we discuss What is Ansible Fetch and its syntax along with examples as well as code implementation. You can also go through our other suggested articles to learn more –

How Does Ansible Run_Once Work? Examples

Introduction to Ansible run_once

The following article provides an outline for Ansible run_once. In Ansible, we always tend to work on remote target nodes, for which we write plays and tasks. This is common understanding that anything written in a play or task will be executed on all remoted target hosts unless specified otherwise. But many times you get a situation where you need to run a task only once on a specific machine to generate some data or do some modification, in between of a playbook or in middle of two tasks in a play. In such cases, we need feature parameters like local_action, delegate_to, run_once. run_once, which is used to run a task only one time for the group of hosts mentioned in playbook.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is Ansible run_once?

Ansible is designed to work in multiple tier environments. It is very useful where running tasks on a host on behalf of another is the requirement. For such requirements where we need one tasks to run only once on a batch of hosts and we will be running that from Ansible controller node, we have feature parameter named run_once. When we have this parameter mentioned in a task, that task will run only once on first host it finds despite the host batch. This is useful in scenarios like Patching on environment where you will send one-time email notification to all system users before starting.

How does Ansible run_once work?

Ansible run_once parameter is used with a task, which you want to run once on first host. When used, this forces the Ansible controller to attempt execution on first host in the current hosts batch, then the result can be applied to the other remaining hosts in current batch. Its best use is with delegate_to parameter, which enables us to run a task on delegated host once and the continue the rest of tasks on remaining hosts.

# …

Examples of Ansible run_once

Given below are the examples of Ansible run_once:

Here we will see Ansible parameter run_once, which you might have to use in day to day operations. We will first see our lab, we used for testing purpose. 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 ansible-controller node and see the results on remote hosts.

Example #1

In this example, we have created a playbook with contents like below, here we are trying to download a tarball file from internet to Ansible control node. As this download is needed only once, so we will use run_once in this task. Also as this is to be done on local machine only, we need to mention delegate_to and pass localhost to it.

Then we will unarchive this tarball to remote machines, which will be done separately on both the hosts:

Code:

dest: /tmp/

On running this playbook like below with some verbosity, to get some detailed output:

ansible-playbook ansible_run_once_downlaod_copy.yaml -v

We get output like below, where we can see that the tarball was first downloaded to localhost, as we used delegate_to, then using unarchive module, we unarchived this tarball to both remote target machines.

Output:

Example #2

In this example, we have created a playbook with contents like below, here we are trying to achieve that when we fetch IPv4 details from all hosts without using run_once, then we will get different details from each host. But if we use run_once, then the IPv4 details fetched from first host in host batch will be stored and shown another host’s output as well.

Code:

var: remote_ipv4

When running this playbook, we have output like below, in which we can see that output is different from different hosts:

Output:

Now update the playbook like below, where we have mentioned run_once in the task.

Code:

var: remote_ipv4

Now after running it, in output we can see that the data fetched from first host is also shown in the output of second host.

ansible-playbook ansible_run_once.yaml

Output:

Example #3

In this example, we have created a playbook with contents like below, here we are trying to achieve that when we are updating a package on remote machines, a mail should be sent to a email account, with a mail body and subject, so that that mail owner is notified of the changes. Also note use of run_once, which make the sending mail to execute only once.

Code:

Executing the playbook using below command:

ansible-playbook ansible_local_action_mail_update.yaml

Output:

We can check that if the mail was created and sent, to check that we have mailx utility.

Using this we can read mail like below.

mailx

Output:

Conclusion

As we saw, using local_action is a bit tricky, but while using it, you do not need to use condition for running a task once like where one action is required to be run once and depending on that all other hosts need to do other actions.

Recommended Articles

This is a guide to Ansible run_once. Here we discuss what is ansible run_once? how does ansible run_once work? and examples respectively. You may also have a look at the following articles to learn more –

How Does Merger Arbitrage Work With Examples?

What is Merger Arbitrage

Start Your Free Investment Banking Course

Download Corporate Valuation, Investment Banking, Accounting, CFA Calculator & others

How does Merger Arbitrage work?

As has already been discussed above, investors use merger arbitrage to exploit the uncertainties surrounding the successful execution of a merger, especially during the period between the announcement of the acquisition and the formal completion of the same. For instance, let us assume that Company A is the acquirer, and Company B is the target in a merger transaction. On Jan 1, 2023, Company A announced that it would acquire Company B in the next six months at the offered price of $100 per share. On the announcement day, Company B’s share price jumped from a pre-announcement price of $70 to close at $85 per share.

The period between the announcement of the deal and its formal execution is critical for a merger arbitrage, which in this example is six months. This period includes many processes, such as shareholders’ approval for the deal, approval from the regulatory authorities, tracking of the target company’s performance, and a bunch of legal paperwork. The spread between $85 and $100 captures the perceived risk of the deal not going through as per plan. Now, as the day for the deal arrives, and if there is no negative news about the merger, the target company’s share price will continue to inch toward the target price of $100.

Examples of Merger Arbitrage

Some of the significant examples have been discussed below:

In June 2024, Microsoft Corp. announced that it would acquire LinkedIn Corporation as per a definitive agreement. It was an all-cash transaction worth $26.2 billion, under which Microsoft bought each LinkedIn share for $196. On the announcement day (June 13, 2024), LinkedIn stocks started trading at $131.08 per share to close at $192.21. The deal was completed in December 2024. If an investor had bought a LinkedIn share at $192.21 and waited for seven months, he would have made an annualized profit of 3.38% (= ($196 – $192.21) / $192.21 * 12 / 7). It is an example of merger arbitrage.

In October 2023, IBM and Red Hat entered into a definitive agreement under which IBM agreed to purchase the entire equity share of Red Hat at a target price of $190 per share in an all-cash merger. The transaction was one of the most significant tech acquisitions of the year, valued at approximately $34 billion. The pre-announcement price of $116.87 per share soared up to $$169.93 by the end of the announcement day. The deal was completed in July 2023. If an investor had bought Red Hat’s share at $169.93 and waited for eight months, he would have made an annualized profit of 17.7% (= ($190 – $169.93) / $169.93 * 12 / 8). It is another example of merger arbitrage.

Merger Arbitrage in Investment Strategy

Now, a merger arbitrageur has a strategy for both situations.

High Probability of Successful Closure: A merger arbitrageur will purchase the shares of the target company (trading at a lower price band) while shorting the acquiring company’s shares (trading at a higher price band). Now, after the successful closure of the deal, the target company’s share converts into the acquiring company’s shares. In this case, the investor will use the converted shares to cover its short position and, as a result, will sell the shares at a higher price.

Low Probability of Successful Closure: In this case, the investor will short-sell the target company’s stocks. When the merger fails, the target company’s share price will fall back to the pre-announcement level. The failure of the deal can be due to multiple reasons. However, the arbitrageur can profit by purchasing the company’s stocks at a lower price and covering its short position.

Advantages

In most cases, merger arbitrage strategies focus on limiting downside risk and making informed decisions. As a result, these strategies are market neutral and can profit in any market situation.

These aggressive strategies can yield a high return in a brief period if appropriately executed.

At times, some investors use these strategies speculatively, which may surge the stock prices to levels that cannot be explained through fundamental analysis.

More considerable hedge funds deploy bulk transactions and use these strategies to influence the market.

Recommended Articles

This is a guide to Merger Arbitrage. Here we also discuss the introduction and how merger arbitrage work with different examples. You may also have a look at the following articles to learn more –

How Does Css Order Work With Examples?

Introduction to CSS Order

In this article, we are discussing an order property of CSS. This order property is a sub-property of the flexible box layout module. The order property arranges the flex items in some order inside the flexible container. The element arranges these flex items in either ascending or descending order based on the order value of the flex items. You can use the order property to change the ordering in the source document. By default, the flex items are listed in the same order as in the source document. In general, the order property arranges the flex items in order relative to other flex items in the same container.

Start Your Free Software Development Course

How does CSS Order work?

In CSS, the order property specifies the order of flexes items to display in either ascending or descending order in the element layout within the container. The order property serves the purpose of ordering visual elements and does not impact the logical tab order. As a result, it should not be applied to non-visual media like voice or speech mediums. Hence, this property creates a disconnection between the DOM and the visual presentation of element content, which will affect the low navigation. Major browsers like Firefox, Google Chrome, Internet Explorer, Apple Safari, Opera, etc., support the order property.

Let us see the syntax and example below. This property is invalid if there is no flex item in the element.

Syntax:

integer: This value is specified in integer value that is in number. By default, the value here is 0 if no value is specified.

initial: The order property sets the value as the default value.

inherit: This value is derived as an inherited value from the computed value of its parent element.

Generally, the users prefer the order property value as an integer value which is easy to specify the order of the items accordingly. As integer values can be positive and negative, the order property takes both values to arrange the flex items in the element layout.

Examples to Implement CSS Order

Let us see a general example of order property used in CSS which uses both positive and negative order property values. Below are the examples:

Example #1

Code:

#main { width: 500px; height: 200px; border: 7px solid black; display: flex; flex-wrap: nowrap; margin-left:80px; margin-bottom:80px; } #main div { width: 250px; height: 100px; } h1 { color:#007700; font-size:53px; margin-left:70px; margin-top:70px; } h3 { margin-top:-20px; margin-left:50px; } div#red1{order:-2;} div#yellow1{order:0;} div#green1{order:-3;} div#pink1{oreder:1;} div#orange1{orde4r:2;} div#blue1{order:-1;} div{ color:white;} }

Output:

Explanation: In the above program, we can see that we have specified both positive and negative numbers. Where we can see the negative numbers are displayed first, then the color with zero value, and then positive values are displayed.

Example #2 order: integer;

This property value specifies the order value in numbers, which can be either a positive number or a negative number, where this property gives numbers to each item in the container.

Syntax:

order: number;

Code:

#main { width: 500px; height: 200px; border: 7px solid black; display: flex; flex-wrap: nowrap; margin-left:80px; margin-bottom:80px; } #main div { width: 250px; height: 100px; } h1 { color:#007700; font-size:53px; margin-left:70px; margin-top:70px; } div#red1{order:6;} div#yellow1{order:4;} div#green1{order:3;} div#pink1{oreder:1`;} div#orange1{orde4r:2;} div#blue1{order:5;} div{ color:white;} }

Output:

Explanation: In the above program, we can see that we have used an integer property value to specify the order for the colors. In the above program, we have six different colors with a different arrangement where we can see we have given pink as “1”, orange as “2”, green as “3”, yellow as “4”, blue as “5”, and red as “6”.

Syntax:

order: initial;

Code:

#main { width: 500px; height: 200px; border: 7px solid black; display: flex; flex-wrap: nowrap; margin-left:80px; margin-bottom:80px; } #main div { width: 250px; height: 100px; } h1 { color:#007700; font-size:53px; margin-left:70px; margin-top:70px; } div{ order: initial; color:white;} }

Output:

Explanation: In the above program, the order property is specified as initial and the font color as white. This property value displays the order of color as we first tried with which color as the first color. So the order at the initial stage was given as for red as “1”, yellow as “2”, green as “3”, pink as “4”, and orange as “5” and, blue as “6”. In this way, the color items in the element layout.

Conclusion

In this article, we conclude that the order property of CSS is used to arrange the flex items in the order. This property is a sub-property of the flexible box layout. This property has different values like integer value which can have both positive and negative values, an initial value that takes the value, which was the default value. It inherits values that take the values from the parent element. The default value for order property is zero.

Recommended Articles

We hope that this EDUCBA information on “CSS Order” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

Learn How Does Any Work In Linq With Examples?

Introduction to LINQ any

Web development, programming languages, Software testing & others

Syntax

LINQ Any comes in two formats as shown below, let’s see the following syntax,

This is the first overloaded method that takes zero arguments and is only used to check whether the sequence contains any items in the collection or not, depends upon that it returns a bool value as a result.

This is the second overloaded method which takes the predicate method as an argument and is used to check whether any single element in the collection satisfies our given condition or not.

How does any work in LINQ?

The foremost thing in the LINQ ANY operator is to make sure or to check whether any single element in the collection satisfies our condition or not. The operator available only the method syntax, it does not support query syntax. It returns a bool value as a result, it returns true if it satisfies the condition, it returns false once the condition fails. Linq Any available in two formats, let’s see below

Firstly it checks whether the collection is empty or not.

Secondly, it checks whether any single element in the collection satisfies our given condition or not.

First overloaded method

If(productList.Any()) { Console.WriteLine(“Product List has items present in it”); } Else { Console.WriteLine(“Empty Product List”); }

It displays Empty Product List as output, it returns bool value, as a result, see the below code to display only the Boolean result,

Var _result= productList.Any(); Console.WriteLine(_result);

Here it returns either true or false depends on the result. The product list contains no items so it returns false as result.

Second overloaded method

In the second overloaded method, the list contains the product details to check whether any of the product costs have an amount less than 500? We have to check any product in the list contains an amount less than 500 or not we need not to displays any names in it just check the condition and returns the bool value as result, let’s see the below code as follows:

productList.Add(new ProductClass { pName = “Speakers”, pCost = 2880 }); productList.Add(new ProductClass { pName = “Disk-Drive”, pCost = 4000 }); productList.Add(new ProductClass { pName = “KeyBoard”, pCost = 1540 }); productList.Add(new ProductClass { pName = “Processor”, pCost = 7590 }); productList.Add(new ProductClass { pName = “Monitor”, pCost = 3250 }); productList.Add(new ProductClass { pName = “Pendrive”, pCost = 475 }); { Console.WriteLine(“Products contains amount less that 500”); } else { Console.WriteLine(“No less amount products present in the list”); }

It list contains the product cost less than 500 so it displays “Products contains amount less than 500” as output, it returns bool value, as a result, see the below code to display only the Boolean result,

Examples

LINQ Any is used to check whether the given condition satisfies the sequence of elements. This operator is available only in Method syntax, it does not support query syntax. Let’s see the example programmatically,

Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Console_LINQAny { class ProductDetails { public int pID { get; set; } public string pName { get; set; } public int pCost { get; set; } } class CustomerDetails { public string cName { get; set; } public string cMobile { get; set; } public int totalProduct { get; set; } public int totalCost { get; set; } } class Program { static void Main(string[] args) { productList.Add(new ProductDetails { pName = "Speakers", pCost = 2880 }); productList.Add(new ProductDetails { pName = "Graphics-Card", pCost = 3000 }); productList.Add(new ProductDetails { pName = "Disk-Drive", pCost = 4000 }); productList.Add(new ProductDetails { pName = "KeyBoard", pCost = 1540 }); productList.Add(new ProductDetails { pName = "Processor", pCost = 7590 }); productList.Add(new ProductDetails { pName = "Monitor", pCost = 3250 }); productList.Add(new ProductDetails { pName = "Pendrive", pCost = 475 }); productList.Add(new ProductDetails { pName = "Pendrive", pCost = 650 }); productList.Add(new ProductDetails { pName = "Pendrive", pCost = 870 }); productList.Add(new ProductDetails { pName = "Desktop-Table", pCost = 1350 }); customerList.Add(new CustomerDetails { cName = "Mithran", cMobile = "9898880901", totalProduct = 3, totalCost = 9880 }); customerList.Add(new CustomerDetails { cName = "Peter", cMobile = "900783221", totalProduct = 2, totalCost = 2190 }); customerList.Add(new CustomerDetails { cName = "Prem", cMobile = "9905003421", totalProduct = 1, totalCost = 4000 }); customerList.Add(new CustomerDetails { cName = "Jhon", cMobile = "8900211056", totalProduct = 4, totalCost = 6120 }); Console.WriteLine("ntUsing LINQ - ANY n"); Console.WriteLine(result_any); Console.ReadKey(); } } }

Output:

Recommended Articles

This is a guide to LINQ any. Here we discuss the need for a LINQ Any operator with examples by using this operator we can check our conditions easily. You may also have a look at the following articles to learn more –

Update the detailed information about How Does Find Module Work In Ansible With Examples? on the Flu.edu.vn 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!