Using app engine interactive console with jinja2 template - debugging

from jinja2 import Template
template = Template('Hello {{ name }}!')
template.render(name='John Doe')
I have entered the above into app engine's interactive console and get no output. How can I get output?
I have tried adding the code I found at the following link to the console, but still no output.
Debug Jinja2 in Google App Engine
Thanks,
Brian in Atlanta

You forgot to add a print. Try this:
from jinja2 import Template
template = Template('Hello {{ name }}!')
print template.render(name='John Doe')

Related

How do nested variables within the .env file work in CodeIgniter 4

Under the "Nesting Variables" section in Codeigniter4 site:
"To save on typing, you can reuse variables that you’ve already specified in the file by wrapping the variable name within ${...}"
link to CI nesting Variables section
example in the documentation:
BASE_DIR="/var/webroot/project-root"
CACHE_DIR="${BASE_DIR}/cache"
TMP_DIR="${BASE_DIR}/tmp"
I was trying to use the following
app.baseURL = 'http://localhost:8080/'
google.redirect = ${app.baseURL}Google
However, it's assigning it as a literal when print_r($_ENV)
[google.redirect] => ${app.baseURL}Google
I've tried using non-namespaced keys including BASE_DIR (per the example) and it keeps printing as a literal.
What's strange - When I use the following:
CI_ENVIRONMENT = development
google.redirect = ${CI_ENVIRONMENT}Google
The result when print_r is:
[CI_ENVIRONMENT] => development
[google.redirect] => developmentGoogle
My question is - What am I doing incorrectly and/or how should these be set/used correctly?
According to the documentation, I should be able to use any key within the .env file that was already assigned using
${somekeyinthisfile}
After a bit of looking, there is a more recent file up at
https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Config/DotEnv.php
with all the "other" changes...
This was a Bug Fix. So get that file and you will be good to go.
I am pretty sure that the intention wasn't to allow app.xxx settings to be used as variables as the documentation clearly shows, by not
showing them being used. ( yes its 6am now ...)
BUT it is your code to do with as you please...So if you want to use app.xxx as variables...
The Only Thing missing is the DOT (.) in the regex
If you look on Line 272 - system/Config/DotEnv.php inside method resolveNestedVariables() and add a . (dot) into the regex, that will make all your app.things work.
$value = preg_replace_callback(
'/\${([a-zA-Z0-9_.]+)}/',
function ($matchedPatterns) use ($loader) {
I have added a dot (.) at the end of the [a-zA-Z0-9_
So
'/\${([a-zA-Z0-9_]+)}/',
becomes
'/\${([a-zA-Z0-9_.]+)}/',

Transform ansible yml files "with vars templating" to yml files "without templating"

I have this yml files inside an ansible project with templating and vars :
custom_values:
postgresql:
postgresqlDatabase: "{{ secrets.db_name }}"
postgresqlPassword: "{{ secrets.postgres_password }}"
I search a solution to generate the same yml file without the templating like :
custom_values:
postgresql:
postgresqlDatabase: "mydatabase"
postgresqlPassword: "mypassword"
Do you know an existing software to do that automatically ?
You already have a set of ansible templates, that are rendered by a render engine like Jinja2.
The easiest way to convert them would be to actually use the render engine to render the templates, supplying the correct values to it.
You'll end up with a bunch of templates that have the {{ something }} blocks, replaced with the values you want.
Since this looks to be simple Jinja2 templating, please refer to: https://jinja.palletsprojects.com/en/2.10.x/
You'll end up with something like this:
>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
Please also refer to this stackoverflow post: How to load jinja template directly from filesystem
That explains how to load templates from files
This python code is OK for me :
#import necessary functions from Jinja2 module
from jinja2 import Environment, FileSystemLoader
#Import YAML module
import yaml
#Load data from YAML into Python dictionary
config_data = yaml.load(open('./my_vars.txt'))
#Load Jinja2 template
env = Environment(loader = FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template('my_template.yml')
#Render the template with data and print the output
print(template.render(config_data))
thanks :)

How can I make verbose output for specific task or module in Ansible

I am using AWS ec2 module and I would like to log what userdata is sent to AWS with every command, but I don't want verbose output from all other useless tasks.
Is there any way I can enable verbosity of the ec2 module?
I agree with #techraf that there is no out of the box way to do this.
But Ansible is easily tuneable with plugins!
Drop this code as <playbook_dir>/callback_plugins/verbose_tasks.py:
from ansible.plugins.callback import CallbackBase
import json
try:
from __main__ import display
except ImportError:
display = None
class CallbackModule(CallbackBase):
def v2_runner_on_any(self, result, ignore_errors=False):
if (result._task.action in ['file','stat']):
print '####### DEBUG ########'
print json.dumps(result._result,indent=4)
print '####### DEBUG ########'
v2_runner_on_ok = v2_runner_on_any
v2_runner_on_failed = v2_runner_on_any
You can tune what modules' results you want to print by changing ['file','stat'] list.
If you need only ec2, replace it with ['ec2'].
I don't think there is a straightforward way.
As a workaround you could run the whole play with no_log: true and add no_log: false explicitly to your task calling ec2 action. Then run the playbook with -v.

Ansible: Extracting IP address from a CIDR( 192.168.21.x/24)

I have a variable name something like this defined in json format.
"zxc_address" : "192.168.21.x/24"
and i need to extract the IP address part(192.168.21.x) using ansible(yaml code)
what is the simple solution for that? can it be done using ansible filters. if yes then how?
Thanks,
VM
If you are using Ansible >=1.9 it is possible to use a filter for this:
{{ '192.0.2.1/24' | ipaddr('address') }}
Have a look at the documentation.
This should work:
{{ zxc_address.split("/")[0] }}

Jekyll Liquid - accessing _config.yml dynamically

For internationalizing my app I need to be able to dynamically access entries in a YAML file.
It is best explained with an example:
Page:
---
layout: default
title: title_homepage
---
This will then allow access to the title_homepage variable in the Default Layout Template:
Default Layout:
page.title = "title_homepage"
Now normally I would access my _config.yml file like this:
{{ site.locales[site.default_locale].variable }}
However, now for this to work, I need to access the _config.yml with the value of page.title. This will not work:
{{ site.locales[site.default_locale].page.title }}
I need the following (pseudo code):
{{ site.locales[site.default_locale].#{value of page.title}}
With the way your vars are set, it would be something alog the lines of
{{ site.locales[site.default_locale][page.title] }}
The thing is, ... I don't really see the point of doing this. Let's say your page is the english page. The locale should then be defined within the page, and so should your title!
---
locale: en
title: My Wonderful Page
---
Which you can retrieve with {{ page.title }} ...
What could be the point of putting the title into the _config.yml file?
(edit) well unless you want to access page.title when in another page/post, in this case you have no choice but to put it into _config.yml.

Resources