"vars:" before "- hosts:"

39 views
Skip to first unread message

Adrian Paraschiv

unread,
Jan 16, 2015, 7:03:38 AM1/16/15
to ansible...@googlegroups.com
Hello all,

Can I do a playbook like this ??
---
vars:
  group_vars:
          DV_LBL_01: "192.168.1.155"
          DV_LBL_02: "192.168.1.166"
          DV_LD_01: "192.168.1.144"
          DV_LM_01: "192.168.1.123"
- hosts: webservers, devservers
  shell: /path/to/some/script.sh
environment: group_vars
- hosts: devservers shell: /path/to/some/other/script.sh
environment: group_vars
- hosts: webservers shell: /path/script.sh
environment: group_vars

Another question: If a first script runs another script (second) that only exports variables, ansible will interpret the exported variables (from the second script) and they will be available for the rest of the first script ?

Thank you.
Adrian

Tom Bamford

unread,
Jan 16, 2015, 9:02:16 AM1/16/15
to ansible...@googlegroups.com

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

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4035a18a-aaef-41ab-ab27-e408ebb47d83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adrian Paraschiv

unread,
Jan 16, 2015, 9:16:09 AM1/16/15
to ansible...@googlegroups.com
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.


Tom Bamford

unread,
Jan 16, 2015, 1:52:28 PM1/16/15
to ansible...@googlegroups.com

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.

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages