"vars:" before "- hosts:"

Hello all,

Can I do a playbook like this ??

I’m not sure exactly what you are asking with regards to the playbook syntax, but it is not valid.

To my knowledge, Ansible will not carry exported variables from one shell script to the next, unless you somehow capture them with some conbination of register: and set_fact

Hello,

I know ansible does not carry env variables from one shell to another but can I use the “environment:” syntax in every shell I need ? Also does “environment:” links to “vars:” written like this ? I’m assking this because I need to run about 20 shell comands that all need about 30-40 environment variables - some of these are like this:

export ACE=“$APPS/default-ace/ACE”
export PIN=“$APPS/default-ace/PIN”
export APPETIZER=“$APPS/default-ace/APPETIZER”

not only static stuff
and I don’t know how to make it work.

Sorry I misread your second question. To answer it properly, I’m fairly certain that you will get the same behaviour with the shell module as you would running a script from an interactive shell, so sourcing scripts and exporting from a sourced script will work just fine.

With the correct playbook yaml structure, your first example should work just fine - building a dictionary of environment variables then passing it to each task with the environment: keyword.

In your example, I’m not sure where $APP is being set, but you can define variables using other variables in this manner.

- hosts: somegroup
  vars:
    apps: something
    myenv:
      ACE: "{{ apps }}/default-ace/ACE"
      PIN="{{ apps }}/default-ace/PIN"
      APPETIZER="{{ apps }}/default-ace/APPETIZER"
  tasks:
    - shell: /path/to/script
      environment: myenv
    - shell: /path/to/other/script
      environment: myenv

Other approaches could be to build your environment variables based on group variables or role variables. This makes sense if you are targeting multiple host groups like the example in your first message.