cancel
Showing results for 
Search instead for 
Did you mean: 
jacuzi
Mission Specialist
Mission Specialist
  • 870 Views

Ansible register variable

Jump to solution

Could someone help me out with this:

Lets say I ran a task in playbook that registers the route of the Openshift deployment.

In the next task I will check if the application is reachable and will use the uri -module.

uri:

  url: "https://{{ blabla.resource[0].bla.bla }}"

 

What does the resource[0] indicate here?

And is the syntax correct for the variable?

If I run debug module and msg/var the register variable it shows resource as one of the attributes, but what does it mean. 

If I run the whole playbook it says that the variable 0/n/n is not found or something similar.

Unfortunately I dont have the playbook nor the full example with me so I cant provide the full playbook or show the full error.

Labels (1)
2 Solutions

Accepted Solutions
Travis
Moderator
Moderator
  • 824 Views

@jacuzi -

I think we would need the real playbook and error log messages to better answer this. From looking at it, I suspect it is how things are "referenced".

In the next task I will check if the application is reachable and will use the uri -module.

uri:

  url: "https://{{ blabla.resource[0].bla.bla }}"

I think some of the issue here might be the URL. Only a single part of the URL would be variablized. Since you are using the "." notation here it is possible that the array (list elements) aren't being accessed properly.

I would figure out what the registered variable is, and use a debug with var to dump everything on how the contents of the variable are registered.

Then use debug with the msg to isolate the portion of the registered output you need via the "." notation or the "[]" notations.

Once you know how everything is captured and displayed, you can leverage that variable for the desired outcome. It might be possible as @Chetan_Tiwary_ suggested that it isn't in list form maybe it is a dictionary, maybe it is something else. Once you see what the registered output looks like, you can then apply filters like  {{ var_out | list }} or something to get it in the correct format.

 

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training

View solution in original post

tnishiok
Flight Engineer
Flight Engineer
  • 757 Views

Hi @jacuzi,

I guess you tried to use the 'k8s_info' module to retrieve the information of the route resource. It returns the list of found objects as the resources key. The example below demonstrates two ways to access the application URL. The first case uses the 'openshift_route' module, and the second one uses the 'k8s_info' module.

sc1.jpg

Either way can reach out the application URL.

sc2.jpg

And the contents of the resources[0] is what you can retrieve using the 'oc get -o yaml' command.

sc3.jpg

Regards,
Toshi

View solution in original post

4 Replies
Chetan_Tiwary_
Moderator
Moderator
  • 839 Views

Hello @jacuzi !

I will try ( but not sure what is causing this issue ).

Here, resource[0] refers to the first item in the list of resources returned by the task which is registering the route. 

If you are getting the error : "variable 0/n/n is not found "  this probably means that task is not returning a list of resources. 

Use the debug module with msg and see what is being returned by the register task (variable ) , if it is not a list then you need to fix it as per your scenario.

Tagging @Tracy_Baker & @Travis if they can add something. 

Travis
Moderator
Moderator
  • 825 Views

@jacuzi -

I think we would need the real playbook and error log messages to better answer this. From looking at it, I suspect it is how things are "referenced".

In the next task I will check if the application is reachable and will use the uri -module.

uri:

  url: "https://{{ blabla.resource[0].bla.bla }}"

I think some of the issue here might be the URL. Only a single part of the URL would be variablized. Since you are using the "." notation here it is possible that the array (list elements) aren't being accessed properly.

I would figure out what the registered variable is, and use a debug with var to dump everything on how the contents of the variable are registered.

Then use debug with the msg to isolate the portion of the registered output you need via the "." notation or the "[]" notations.

Once you know how everything is captured and displayed, you can leverage that variable for the desired outcome. It might be possible as @Chetan_Tiwary_ suggested that it isn't in list form maybe it is a dictionary, maybe it is something else. Once you see what the registered output looks like, you can then apply filters like  {{ var_out | list }} or something to get it in the correct format.

 

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
tnishiok
Flight Engineer
Flight Engineer
  • 758 Views

Hi @jacuzi,

I guess you tried to use the 'k8s_info' module to retrieve the information of the route resource. It returns the list of found objects as the resources key. The example below demonstrates two ways to access the application URL. The first case uses the 'openshift_route' module, and the second one uses the 'k8s_info' module.

sc1.jpg

Either way can reach out the application URL.

sc2.jpg

And the contents of the resources[0] is what you can retrieve using the 'oc get -o yaml' command.

sc3.jpg

Regards,
Toshi

Trevor
Starfighter Starfighter
Starfighter
  • 685 Views

Hello Jacuzi,

Are you able to provide any output from the execution of the playbook?

 

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Join the discussion
You must log in to join this conversation.