Ansible

Ansible Pipe Lookup

In this article, we will cover how to utilize the pipe command in the lookup plugin, which is a feature of the Ansible software. We will go over the playbook’s use of the pipe in the lookup plugin, its purpose, why we are using it, and how it operates in Ansible.

In Ansible, the lookup module performs nothing on the target remote device because it is usually run on the Ansible controller side. The Ansible lookup module has a default path, which must contain the Programming language python pipe.py script in order to use the pipe component. The pipe component of lookup is a kind of redirecting that transmits specified output to some other address.

When two or more commands are combined via a pipe component, the outcome of one command can be utilized as the input for a further command, whose result can be utilized as the input for another command, and so on. Another way to think about it is as a transitory connection that connects two or more commands in the Ansible playbook. In contrast to having to transmit information via transitory text documents or through the display module, a such direct relation between commands enables components to run concurrently and enables information to be transferred between them continually. The flow of information in the Ansible pipe lookup module will be from left to right.

Parameters of Ansible Pipe Lookup

In the Ansible pipe lookup module, we need an input string that can be stored in any document or any variable so that we can use these documented strings to execute the commands.

Prerequisites of Ansible Pipe Lookup:

In this guide, we will presume that we have fulfilled the following requirements so that we can easily configure the pipe lookup module in Ansible:

  • First, we must have to install the Ansible tool in the system so that we can work in an Ansible environment.
  • Next, we need a main server that will configure and manage the plugins on the target server, and in this guide, the Ansible controller works as a main server in the whole functionality of the pipe lookup plugin.
  • We also need a target server where we implement and configure the pipe lookup module and here, we have targeted the local host as a remote host.
  • In the end, the pipe lookup module contains the pipe.py script at the default location so that we can work on the pipe which is based on the python language.

Let us start with a brief practical demonstration of the Ansible Playbook’s pipe lookup module in action. We will discuss the pipe’s usage instructions as well as the implementation of the Ansible pipe lookup module.

Example: Utilizing Pipe and Date in the Lookup Plugin

Here is the very first and simple example of Ansible lookup where we are utilizing the pipe command that is based on python programming. In Ansible, to write the commands and tasks and implement the plugin lookup, we will need a playbook to write all these. Below is the statement we will use to create the playbook in the Ansible terminal.

[root@master ansible]# nano pipe.yml

After writing the command above, the playbook will be created of .yml extension and it will automatically be launched in the new window of the Ansible tool of name pipe.yml. Now, let us start writing the required commands so that we can implement the pipe lookup module. First, we will provide the target remote host in the playbook so that we will manage the lookup module of the remote host device. And we are using the local host for configuration.

In the next line, we have used the gather fact option of Ansible because this option is used to get the information related to the local host if we pass the true Boolean value to the option. But we have passed the false Boolean value so that when we execute the playbook, we do not get the information of the local host in the output terminal, except the name of the remote host. After providing the required information of the local host, we will write the tasks that we want to implement in the pipe playbook.

In the first task, we are trying to get the outcome by executing the date command in the lookup plugin. And then we have to use the debugger option so that we can showcase the desired results in the terminal. In the 2nd task, we will list the text documents that were present in the /home/ansible directory. So, for that, we have used the pipe and the ls command to get the list of the text documents.

- hosts: localhost

  gather_facts: false
  tasks:
    - name: raw result of running date command
      debug:
        msg: "{{ lookup('pipe', 'date') }}"
   
    - name: list txt files in a /home/ansible directory
      debug:
        msg: "{{ lookup('pipe', 'ls -ls /home/ansible/*.txt') }}"

After writing all the tasks we want to do in the playbook, we will execute the playbook. So, we will first terminate the playbook. After terminating, we will write the following command to get the desired output in the terminal.

[root@master ansible]# ansible-playbook pipe.yml

Below is the output we have to get after writing the command above in the Ansible terminal. As show below, the output of the two tasks are executed successfully. In the first task, a message is displayed in a green signal with the date. And in the second task, the task is executed and it is showing text documents that are available in the /home/ansible/test.txt.

Conclusion

We have discussed one of the components of the lookup plugin which is the pipe in Ansible. We have learned in which language the script of pipe is developed. We have learned how many parameters we will require to perform the pipe lookup plugin in Ansible.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content