cancel
Showing results for 
Search instead for 
Did you mean: 
spurs
Flight Engineer
Flight Engineer
  • 1,035 Views

debug var question

Jump to solution

Hello, in this example. why auth_test.content is used instead of auth_test? is it because of return_content: yes on uri module?

 

- name: connect to web server with basic auth
      uri:
        url: https://serverb.lab.example.com
        validate_certs: no
        force_basic_auth: yes
        user: "{{ web_user }}"
        password: "{{ web_pass }}"
        return_content: yes
        status_code: 200
      register: auth_test
- debug:
        var: auth_test.content

+ what exactly 'force_basic_auth' is used for? 

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
bonnevil
Starfighter Starfighter
Starfighter
  • 992 Views

So, the registered variable auth_test is going to have a more complex structure.  If you try the playbook with that as var in the debug task, you'd see that.  Here's a complete version of your play, with the basic auth stuff yanked:

- name: Let's play with register
gather_facts: false
hosts: localhost
become: false

tasks:
- name: connect to web server
uri:
url: https://www.redhat.com
validate_certs: no
return_content: yes
status_code: 200
register: auth_test

- name: Print out the content of the web page
debug:
var: auth_test['content']

Because you are using the uri module and have return_content: yes set, the auth_test['content'] (same as auth_test.content but safer syntax) variable will contain the content of the web page.  If that had been set to no or false, that variable won't be defined.

If you just ask for auth_test, you'll get a variable that contains as its value a dictionary of a lot of other variables and their values:

TASK [Print out the content of the web page] **********************
ok: [localhost] => {
"auth_test": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"cache_control": "private, no-cache, must-revalidate",
"content_language": "en",
"content": -----a whole bunch of stuff here-----
[...output omitted for this example...]
"server": "Apache",
"status": 200,
"strict-transport-security": "max-age=31536000, max-age=31536000",
[...and so on...]

So, the point here is that if you'd had debug print out the variable auth_test.server or auth_test['server'] you'd get the string "Apache" in my example.

View solution in original post

5 Replies
shura
Flight Engineer
Flight Engineer
  • 1,016 Views

Hello @spurs 

1) You can use auth_test as parent structure in debug module as well. You will see some more info, if you use auth_test.content only the content child element of partent auth_test structure will be shown.

2) force_basic_auth - Use this setting in the following scenarios: You know the webservice endpoint always requires HTTP Basic authentication, and you want to speed up your requests by eliminating the first roundtrip. When this setting is `true', this module will immediately send a Basic authentication header on the first request. Opposite 'false' when this module will first try an unauthenticated request, and when the server replies with an `HTTP 401' error, it will submit the Basic authentication header.

Good luck

spurs
Flight Engineer
Flight Engineer
  • 975 Views

thanks for the details!

0 Kudos
bonnevil
Starfighter Starfighter
Starfighter
  • 993 Views

So, the registered variable auth_test is going to have a more complex structure.  If you try the playbook with that as var in the debug task, you'd see that.  Here's a complete version of your play, with the basic auth stuff yanked:

- name: Let's play with register
gather_facts: false
hosts: localhost
become: false

tasks:
- name: connect to web server
uri:
url: https://www.redhat.com
validate_certs: no
return_content: yes
status_code: 200
register: auth_test

- name: Print out the content of the web page
debug:
var: auth_test['content']

Because you are using the uri module and have return_content: yes set, the auth_test['content'] (same as auth_test.content but safer syntax) variable will contain the content of the web page.  If that had been set to no or false, that variable won't be defined.

If you just ask for auth_test, you'll get a variable that contains as its value a dictionary of a lot of other variables and their values:

TASK [Print out the content of the web page] **********************
ok: [localhost] => {
"auth_test": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"cache_control": "private, no-cache, must-revalidate",
"content_language": "en",
"content": -----a whole bunch of stuff here-----
[...output omitted for this example...]
"server": "Apache",
"status": 200,
"strict-transport-security": "max-age=31536000, max-age=31536000",
[...and so on...]

So, the point here is that if you'd had debug print out the variable auth_test.server or auth_test['server'] you'd get the string "Apache" in my example.

spurs
Flight Engineer
Flight Engineer
  • 975 Views

Ok! I think I got it!

Thank you so much for your answer, now I read the questions again and it said 

[The second task uses the debug module to print the content returned from the web server.]

if it asked to print the server instead of content, then the answer should be like var: auth_test['server']?

0 Kudos
bonnevil
Starfighter Starfighter
Starfighter
  • 957 Views

@spursYep, you've got it! 

Join the discussion
You must log in to join this conversation.