<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create a ansible play that requires user input in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746585#M908</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/54916"&gt;@Claudia de Luna&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thank you so much for your quick reply, let me try this between today or tomorrow and revert to you at that time.&lt;/P&gt;&lt;P&gt;Jerems&lt;/P&gt;</description>
    <pubDate>Thu, 29 Dec 2022 13:09:53 GMT</pubDate>
    <dc:creator>Jerems</dc:creator>
    <dc:date>2022-12-29T13:09:53Z</dc:date>
    <item>
      <title>Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746541#M906</link>
      <description>&lt;P&gt;Hi dear community,&lt;/P&gt;&lt;P&gt;I am currently playing with Ansible and wondered if it is possible to have a play at the top of my playbook that requires a user input.&lt;/P&gt;&lt;P&gt;The user would need to input a device name so all the following plays will rely on that input to achieve different tasks such as :&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Retrieving data from Netbox for that particular device&lt;/LI&gt;&lt;LI&gt;Parse registered data from previous task and generate some json,yaml output files&lt;/LI&gt;&lt;LI&gt;Render Jinja2 templates.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;At the moment, the device name can be found statically at several places in my playbook and want to let the user to input the device name ony once.&lt;/P&gt;&lt;P&gt;Thanks in advance for your kind help, i wish you a happy end of year.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 10:40:00 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746541#M906</guid>
      <dc:creator>Jerems</dc:creator>
      <dc:date>2022-12-29T10:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746575#M907</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/1216081"&gt;@Jerems&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Its been a while since I worked with Ansible but here is an example of an interactive playbook (may need "freshening up" depending on your Ansible version).&amp;nbsp; You can also provide arguments via the command line if that works, depending on how you are executing your playbook.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;###############################################################################
# This interactive playbook queries for information and action and performs
# the selection Tenant action against the DevNet Sandbox APIC
# EXECUTION:
# ansible-playbook -i hosts aci_tenant_prompt.yml
###############################################################################

- name: ACI Playbook to Perform User Prompted Action on Tenant
  hosts: aci_sandbox
  gather_facts: no

  vars:
    avar: some_var

# Prompt for the Tenant Name and Tenant action
  vars_prompt:
    - name: "tenant_name"
      prompt: "Enter Tenant Name"
      default: "LAX_Tenant"
      private: no

    - name: "tenant_state"
      prompt: "Enter Tenant State (absent | present | query)"
      default: "query"
      private: no&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 29 Dec 2022 12:44:16 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746575#M907</guid>
      <dc:creator>Claudia de Luna</dc:creator>
      <dc:date>2022-12-29T12:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746585#M908</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/54916"&gt;@Claudia de Luna&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thank you so much for your quick reply, let me try this between today or tomorrow and revert to you at that time.&lt;/P&gt;&lt;P&gt;Jerems&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 13:09:53 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746585#M908</guid>
      <dc:creator>Jerems</dc:creator>
      <dc:date>2022-12-29T13:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746603#M909</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/54916"&gt;@Claudia de Luna&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works well thank you !&lt;/P&gt;&lt;P&gt;Here is the output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [pause] ********************************************************************************************************************************************************************************************************************
[pause]
Enter Device Name:
jeyniper01^Mok: [localhost]

TASK [set_fact] *****************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************************************************************************
ok: [localhost] =&amp;gt; {
    "dev": "jeyniper01"
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can see from the screenshot below, i gather some parameters from netbox registered as "netbox_devices".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;---
- name: Prompt for the device to configure
  hosts: localhost
  tasks:
    - pause:
        prompt: "Enter Device Name"
        echo: yes
      register: input_dev
    - set_fact:
        dev: "{{ input_dev.user_input }}"
    - debug:
        var: dev

- name: Config Junos context SNMP
  gather_facts: no
  hosts: all
  connection: local
  become: false
  vars:
    server: "https://127.0.0.1"
    endpoint1: "/api/dcim/devices/?limit=1000"
    netbox_token: "123456789"
  tasks:
    # Make a Netbox API call to retrieve devices'components
    - name: Get Device List from Netbox
      uri:
        url: "{{ server }}{{ endpoint1 }}"
        headers:
          Authorization: "Token {{ netbox_token }}"
          Accept: 'application/json'
        body_format: json
        method: GET
        status_code: [200, 201]
        timeout: 30
        validate_certs: false
        return_content: no
      register: netbox_devices
      run_once: yes

    - name: QueryDeviceName
      ansible.builtin.debug:
        msg: "{{ netbox_devices.json.results | to_json | from_json | community.general.json_query(jmesquery) }}"
      vars:
        jmesquery: "[?name== 'dev' ].{Name: name, snmp_loc: custom_fields.snmp_location, IP: primary_ip.address, snmp_com: custom_fields.snmp_community, snmp_srv: custom_fields.snmp_server}"
      register: device_name&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would need the input device "jeyniper01" which is stored as a variable (in var: dev from initial task) to be used and matched in the jmesquery.&lt;/P&gt;&lt;P&gt;I gave it a try like this :&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;jmesquery&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;"[?name== 'dev' ].&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;I even tried like this:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;jmesquery&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;"[?name== {{ dev }} ].&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;But it doesn't as i was expecting.&lt;/P&gt;&lt;P&gt;Can you please advise ?&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;Jerems&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 14:06:12 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746603#M909</guid>
      <dc:creator>Jerems</dc:creator>
      <dc:date>2022-12-29T14:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746630#M910</link>
      <description>&lt;P&gt;So I'm assuming this works?&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;        jmesquery: "[?name=='jeyniper01' ].{Name: name, snmp_loc: custom_fields.snmp_location, IP: primary_ip.address, snmp_com: custom_fields.snmp_community, snmp_srv: custom_fields.snmp_server}"&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;If so, what I would try next is to build out that string explicitly so that no variable interpretation needs to be done in the query string.&amp;nbsp; Basically build out a variable called jmesquery_str or something and then pass that entire variable.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;jmesquery: {{ jmesquery_str }}&lt;/P&gt;&lt;P&gt;Having said that its very possible I have no idea what I'm talking about!!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 15:37:22 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746630#M910</guid>
      <dc:creator>Claudia de Luna</dc:creator>
      <dc:date>2022-12-29T15:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746631#M911</link>
      <description>&lt;P&gt;Yes this one works correctly.&lt;/P&gt;&lt;PRE&gt;        jmesquery: "[?name=='jeyniper01' ].{Name: name, snmp_loc: custom_fields.snmp_location, IP: primary_ip.address, snmp_com: custom_fields.snmp_community, snmp_srv: custom_fields.snmp_server}"&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Dec 2022 15:46:14 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4746631#M911</guid>
      <dc:creator>Jerems</dc:creator>
      <dc:date>2022-12-29T15:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create a ansible play that requires user input</title>
      <link>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4747057#M912</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I finaly found a solution/workaround using a python script :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#!/opt/homebrew/bin/python3
# -*- coding:utf-8 -*-
########################################################################################################################
# This file is a part of Jeyriku.net
#
# Created: 30.12.2022 15:27:57
# Author: Jeremie Rouzet
#
# Last Modified: 30.12.2022 15:35:42
# Modified By: Jeremie Rouzet
#
# Copyright (c) 2022 Jeyriku.net
########################################################################################################################
# Imports from Json to export datas in Json Format
import json
# Imports from Request to create REST API Call
import requests
# Imports from Jmespath to Filter Json data
import jmespath


# Define Netbox object and correct IP to build REST API Request
netbox = {
   "ip": "127.0.0.1"
}
headers = {
      "Accept" : "application/json",
      "Content-Type" : "application/json",
      "Authorization" : "Token XXXXXXXXXXXXXXXXXXXXXXXXXXX",
   }
devices_path = "/api/dcim/devices"
url = f"https://{netbox['ip']}{devices_path}"


devices = requests.get(url, headers=headers, verify=False, timeout=30).json()  # timeout:30sec
with open('./Python/devices.json', 'w', encoding='utf-8') as f:
    json.dump(devices, f, ensure_ascii=False, indent=4)
    data = jmespath.search('results[*].{Name: name, snmp_loc: custom_fields.snmp_location,IP: primary_ip.address, snmp_com: custom_fields.snmp_community, snmp_srv: custom_fields.snmp_server}', devices)

with open('./Python/results.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

input_dev = input("Enter the Hostname of the device to be configured: \n")
print(f"Is this what you just said?\n {input_dev}")

print("Started Reading JSON file")
with open('./Python/results.json', "r" , encoding='utf-8') as check_dev:
    print("Converting JSON encoded data into Python dictionary")
    data = json.load(check_dev)

# Search data based on key and value using filter and list method
result = (list(filter(lambda x:x["Name"]== input_dev, data)))
with open("./Python/target_dev.json", "w" , encoding='utf-8') as outfile:
    json.dump(result, outfile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this help some others &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2022 21:56:24 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/create-a-ansible-play-that-requires-user-input/m-p/4747057#M912</guid>
      <dc:creator>Jerems</dc:creator>
      <dc:date>2022-12-30T21:56:24Z</dc:date>
    </item>
  </channel>
</rss>

