{"id":2520,"date":"2023-02-28T22:10:50","date_gmt":"2023-02-28T22:10:50","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2520"},"modified":"2023-02-28T22:10:52","modified_gmt":"2023-02-28T22:10:52","slug":"lookup-ansible","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/02\/28\/lookup-ansible\/","title":{"rendered":"`lookup`: Ansible"},"content":{"rendered":"\n<p>Ansible&#8217;s <code>lookup<\/code> plugin allows you to access data from a variety of sources, including files, templates, and external programs. You can use <code>lookup<\/code> to fetch data dynamically at runtime and use it in your playbooks and roles.<\/p>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"129\" height=\"129\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-lookup.png\" alt=\"\" class=\"wp-image-2521\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>The <code>lookup<\/code> plugin is used in the following format: <\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background has-small-font-size\"><code><code>{{ lookup('&lt;<strong>plugin<\/strong>>', '&lt;<strong>plugin arguments<\/strong>>') }}<\/code><\/code><\/pre>\n\n\n\n<p>Here,<code> &lt;<strong>plugin<\/strong>><\/code> is the name of the plugin you want to use, and <code>&lt;<strong>plugin arguments<\/strong>><\/code> are any arguments that the plugin requires. The result of the plugin is then returned as a variable.<\/p>\n\n\n\n<p>In this blog post, we will explore how to use the <code>lookup<\/code> plugin in <code>Ansible<\/code>, along with some examples of when and how to use it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Lookup with When Condition<\/h3>\n\n\n\n<p>One of the most common use cases of the <code>lookup<\/code> plugin is to conditionally execute a task based on the value of a variable. You can use the <code>when<\/code> keyword to specify a condition that determines whether or not the task should be executed.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use <code>lookup<\/code> with a <code>when<\/code> condition:<\/p>\n\n\n\n<pre class=\"wp-block-code has-light-green-cyan-background-color has-background has-small-font-size\"><code>- name: Copy file based on OS\n  copy:\n    src: \"{{ lookup('env', 'HOME') }}\/file.{{ 'exe' if ansible_os_family == 'Windows' else 'sh' }}\"\n    dest: \/path\/to\/file\n  when: ansible_os_family == 'Windows' or ansible_os_family == 'Linux'<\/code><\/pre>\n\n\n\n<p>In this example, we use <code>lookup<\/code> to retrieve the value of the <code>HOME<\/code> environment variable, and then concatenate it with the appropriate file extension based on the operating system. We then use this value as the source file for the <code>copy<\/code> module. The task is conditionally executed based on the value of the <code>ansible_os_family<\/code> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Lookup to Read Data from a File<\/h3>\n\n\n\n<p>Another common use case of the <code>lookup<\/code> plugin is to read data from a file and use it in a task. You can use the <code>file<\/code> plugin to read data from a file and store it as a variable.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use <code>lookup<\/code> with the <code>file<\/code> plugin:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background has-small-font-size\"><code>- name: Read data from file\n  set_fact:\n    data: \"{{ lookup('file', '\/path\/to\/file') }}\"<\/code><\/pre>\n\n\n\n<p>In this example, we use <code>lookup<\/code> to read the contents of a file located at <code>\/path\/to\/file<\/code>. The contents of the file are then stored as a variable named <code>data<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Lookup with Loop<\/h3>\n\n\n\n<p>You can also use the <code>lookup<\/code> plugin with a loop to iterate over a list of values and perform a task for each value. The <code>with_items<\/code> keyword is used to specify the list of values.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use <code>lookup<\/code> with a loop:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-pale-cyan-blue-background-color has-text-color has-background has-small-font-size\"><code>- name: Create users\n  user:\n    name: \"{{ item }}\"\n  with_items: \"{{ lookup('file', '\/path\/to\/userlist.txt').split('\\n') }}\"<\/code><\/pre>\n\n\n\n<p>In this example, we use <code>lookup<\/code> to read the contents of a file located at <code>\/path\/to\/userlist.txt<\/code>, split the contents into a list of values, and then iterate over each value using a loop. For each value in the list, we create a user account using the <code>user<\/code> module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Lookup with Templating<\/h3>\n\n\n\n<p>You can also use the <code>lookup<\/code> plugin with templating to dynamically generate values at runtime. You can use variables, filters, and other Jinja2 templating features to customize the output of the <code>lookup<\/code> plugin.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use <code>lookup<\/code> with templating:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-pale-cyan-blue-background-color has-text-color has-background has-small-font-size\"><code>- name: Create directories\n  file:\n    path: \"{{ lookup('env', 'HOME') }}\/{{ item }}\/{{<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ansible&#8217;s lookup plugin allows you to access data from a variety of sources, including files, templates, and external programs. You can use lookup to fetch data dynamically at runtime and use it in your playbooks and roles. The lookup plugin is used in the following format: Here, &lt;plugin> is the name of the plugin you [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[278,292],"class_list":["post-2520","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-ansible","tag-lookup"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2520"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2520\/revisions"}],"predecessor-version":[{"id":2522,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2520\/revisions\/2522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}