Environment variable do not work for python script ?

306 views
Skip to first unread message

Minh Khang VU

unread,
Jun 23, 2015, 11:42:17 AM6/23/15
to cloudif...@googlegroups.com
Hello,

I'm not being able to access to environment variable in python script. What I tried to do:

- In the blueprint:

source_interfaces:
cloudify.interfaces.relationship_lifecycle:
preconfigure:
implementation: webAppConnectedToLoadBalancer.py
inputs:
process:
env:
LOAD_BALANCER_IP: { get_attribute: [LoadBalancerCompute, ip] }

- In the script:

from cloudify import ctx

import os

ctx.logger.info('Just logging the database IP: _____{0}_____'.format(os.environ.get('LOAD_BALANCER_IP')))

I'm not at all python expert so perhaps i'm doing something wrong. The same thing is working in shell script:

ctx logger info "Just logging the database IP: ${LOAD_BALANCER_IP}"

Thank you for your help.

Regards,

Minh Khang VU

trammell scruggs

unread,
Jun 29, 2015, 8:09:23 AM6/29/15
to cloudif...@googlegroups.com
Hi Ming,

Have you confirmed that a node instance of the  "LoadBalancerCompute" node template has been created and its ip attribute has been populated before this operation runs?

Also, you might attempt something like this, using TARGET:

source_interfaces:
cloudify.interfaces.relationship_lifecycle:
preconfigure:
implementation: webAppConnectedToLoadBalancer.py
inputs:
process:
env:
            LOAD_BALANCER_IP: { get_attribute: [ TARGET, ip ] }

Minh Khang VU

unread,
Jul 5, 2015, 10:32:34 PM7/5/15
to cloudif...@googlegroups.com
Hello Trammel,

As I mentioned, it works in shell script and not in python, I read the code of the script plugin  https://github.com/cloudify-cosmo/cloudify-script-plugin/blob/master/script_runner/tasks.py

It seems that in eval python mode, all information that I put in process/env etc are not considered as it will use the method 

def eval_script(script_path, ctx, process=None):


With my little knowledge in python perhaps i'm mistaken, but the argument process is just ignored and environment variable is not set.

Regards,

Minh Khang VU

Dan Kilman

unread,
Jul 5, 2015, 11:14:50 PM7/5/15
to cloudif...@googlegroups.com, minh.kha...@gmail.com
They are indeed not exposed as environment variables but are readily available as inputs by placing them in the top level inputs instead of the nested env.process
and then doing something like

from cloudify.state import ctx_parameters as inputs

afterward, you can access these like this 

# inputs in this case is just a sugared dict that allows top level key attribute-like access to the operation inputs
# you could just as well do inputs['LOAD_BALANCER_IP']
ctx.logger.info(inputs.LOAD_BALANCER_IP)


another option you have is to pass eval_python: false to env.process

but then you will need to call ctx in a similar fashion to how you would call it in a bash script (i.e. as an external process and not using "from cloudify import ctx")

Minh Khang VU

unread,
Jul 5, 2015, 11:24:55 PM7/5/15
to cloudif...@googlegroups.com, minh.kha...@gmail.com
Thank you very much, the inputs option is exactly what I need.

Regards,

Minh Khang VU

Minh Khang VU

unread,
Jul 6, 2015, 2:34:47 AM7/6/15
to cloudif...@googlegroups.com, minh.kha...@gmail.com
Hello Dan

I did as you advised and test on an execute_operation workflow, but it seems that inputs is empty.

I put the key/value dict in the operation_kwargs

Is it the right thing to do ?

Regards,

Minh Khang VU

On Monday, July 6, 2015 at 5:14:50 AM UTC+2, Dan Kilman wrote:

Minh Khang VU

unread,
Jul 6, 2015, 2:37:21 AM7/6/15
to cloudif...@googlegroups.com
Sorry, it was my mistake, please do not take into account, it seems working.

Regards,

Minh Khang VU

Chandu

unread,
Jan 14, 2016, 12:14:50 AM1/14/16
to cloudify-users
Hi,
I am having similar issue (environment variables not available in my python script executed using script plugin).
We are using cloudify 3.1.

My blueprint looks like:

inputs:
  sql_server_username:
    type: string
    default: ''
    description: >
      Enter SQL Server User Name

node_templates:  
  my_install:
      type: my.nodes.Root
      relationships:
        - target: win2012r2
          type: cloudify.relationships.contained_in
      interfaces:
        cloudify.interfaces.lifecycle:
          start:
            implementation: scripts/my/installer.py
            inputs:
              env:
                SQL_USERNAME: { get_input: sql_server_username }
    

and the installer.py

ctx logger info "Username is  ${SQL_USERNAME}"

I even tried passing a hardcoded value to the env.SQL_USERNAME but it was not available from the script.

Am I missing something from my blueprint?
We are stuck with 3.1 for now and I would like to know what am I missing to pass the parameters as environment variables to the script.
Any pointers?

Chandu

unread,
Jan 14, 2016, 12:33:53 AM1/14/16
to cloudify-users
I just realized that I am not using the process key to assign env.
I will try the following structure and see if it works

inputs:
    process:
         env:
             SQL_USERNAME: { get_input: sql_server_username }

Chandu

unread,
Jan 14, 2016, 9:44:52 AM1/14/16
to cloudify-users
Hi,
Tried with the fix (added the env to the process key), but no luck.
The environment variables are not passed to the pyhton script.
I am trying to access the environment variable using os.environ.get("<KEY>", "DEFAULT").
Any help on what is the cause?

Chandu

unread,
Jan 14, 2016, 6:28:59 PM1/14/16
to cloudify-users

Found out that if the script is a python script and eval_script is not set to false in the Process configuration options, the environment variables are not injected (atleast for v 3.1).

Thanks to Dan Kilman for his inputs about the cloudify.state import ctx_parameters


https://groups.google.com/d/msg/cloudify-users/mEAI9x9ivXQ/39Rg6KgKP4cJ
Reply all
Reply to author
Forward
0 new messages