Re: [ansible-project] Accessing environment variables created inside shell task
Custom module for gathering facts for the rescue! http://docs.ansible.com/ansible/developing_modules.html#module-provided-facts http://blog.toast38coza.me/custom-ansible-module-hello-world/ http://mcsrainbow.github.io/articles/create-an-ansible-module-and-then-use-module-provided-facts.html 2016. január 29., péntek 18:30:13 UTC+1 időpontban PixelDrift.NET Sam a következőt írta: > > To add to that, I understand I can use something similar to the following > to access a single value: > > --- > > - hosts: all > > tasks: > >- name: Export environment variable > > shell: /usr/local/bin/blackbox && env | grep ^ANIMAL > > register: output > > > But the real scenario is that I may have 5-10 environment variables > generated from blackbox and I am looking for a structured way to access > them. > > > Thanks again. > > > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cb6296b6-0073-46e9-94a8-2a93548d9732%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [ansible-project] Accessing environment variables created inside shell task
use shell to get the var, register the output, use that output to set environment: directive for the rest of the play. -- Brian Coca -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8m%3D747mUQ5c37q819LDLNShR9H2QveCoR%3DvJKjnfit7mg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: [ansible-project] Accessing environment variables created inside shell task
To add to that, I understand I can use something similar to the following to access a single value: --- - hosts: all tasks: - name: Export environment variable shell: /usr/local/bin/blackbox && env | grep ^ANIMAL register: output But the real scenario is that I may have 5-10 environment variables generated from blackbox and I am looking for a structured way to access them. Thanks again. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f200717b-1bbf-435c-bbaa-9af058e8d24c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [ansible-project] Accessing environment variables created inside shell task
Brian, Thanks for the response, I acknowledge all of these points and accept that this is the situation. In the example at point 3. the environment variable is provided to multiple shell tasks, but in this scenario the value of the environment variable being passed is known prior to execution. Perhaps I should have better described my constraints, as the example of setting the environment variable in the shell using 'export' was only to to show I couldn't access the value outside the shell. In my example replace the 'export ANIMAL=droppear' to a binary called 'blackbox', this 'blackbox' binary generates results in the form of setting environment variables that I need to access, one of them named 'ANIMAL'. ie. --- - hosts: all tasks: - name: Export environment variable shell: /usr/local/bin/blackbox My question is still the same, is there a method to access the 'ANIMAL' environment variable generated by the 'blackbox' binary I call using a shell task? ie. I don't have a value I want to pass into the environment, I wan't to extract the environment variable out of the result of a shell task that is run and make it available to other tasks. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2c964310-e7f8-4651-b521-0141a5d38ea8%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [ansible-project] Accessing environment variables created inside shell task
A few misconceptions here: #1 what you set in the shell: task is not available to other tasks as they each reconnect to the server and open a new ssh session, they don't all execute in the same shell process. #2 ansible_env is not reset every time you run a task, it reflects the environment from when facts where gathered, it can even reflect a differnt user when remote_user/become_user are set in different places. #3 to set the environment for task execution we have the 'environment' directive, which can be set at play, block and task levels: - hosts: all environment: ANIMAL: dropbear tasks: - shell: env |grep ANIMAL register: output - debug: var=output -- Brian Coca -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8k%3DMMw-xJJRjh6LtWXggNjgz3bL52Nzz%2BcAzhdDk770oQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
[ansible-project] Accessing environment variables created inside shell task
I am currently working through replacing a legacy orchestration tool with Ansible while maintaining the existing orchestration scripts in place. I have hit an interesting problem with environment variables that I haven't yet been able to solve. Essentially, I want to be able to access environment variables that are created as part of a shell/comand task. Ideally I would like to extract these variables from the shell task and populate facts with the values and/or pass the values to subsequent tasks. The issue is that I can only access the environment variables in that task (ie. subsequent tasks can't 'see' them) so I need some way of exporting/importing them for other tasks. The problem is displayed in the following playbook: --- - hosts: all tasks: - name: Export environment variable shell: export ANIMAL=dropbear - debug: msg="{{ ansible_env.ANIMAL }}" Unless 'ANIMAL' already exists in the environment (ie. outside of the first shell task), the debug message won't be able to access the value of ANIMAL. I have investigated several different methods such as: 1. Registering the shell command and printing 'env' to stdout and parsing 2. Dumping to file on filesystem and using wrapper to load/source file from disk in subsequent tasks 3. Parsing a file dumped to disk with include_vars / lookup with ini plugin All methods seem a little too 'hacky' and I was wondering if anyone had a more elegant solution that didn't involve storing the information on disk? I was hoping there was a variable exposed when registering the shell task so you can access environment variables eg. 'registered_var.env_vars.ANIMAL', similar to how you access stdout/stderr etc. Is there something this obvious that I am missing? It appears there has been similar discussion about environment variables set in Ansible shell tasks not persisting across tasks here, but it looks to end up with a similar conclusion: http://stackoverflow.com/questions/27733511/how-to-set-linux-environment-variables-with-ansible All suggestions for making this process more streamlined will be much appreciated! (passing values through etcd at the moment). -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To post to this group, send email to ansible-project@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/668f42b3-31f8-4fff-bc7e-c5cdc6354835%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.