Home > Software engineering >  Ansible - passing vars from a playbook to another
Ansible - passing vars from a playbook to another

Time:10-27

I am working with Ansible and I have defined a var (varx) in inventory file (inventory1.yml) like this (only for env2):

env1:
      hosts:
        env1.domain.com:
env2:
          hosts:
            env2.domain.com:
          vars:
            varx: 'valuex'

I am running playbook1 and calling playbook2 like this:

ansible-playbook -i inventory/inventory1.yml playbooks/playbook1.yml --extra-vars "env=env1"

playbook1.yml:

---
  roles:
   - role1

role1 - main.yml:

---
- name: role1
  command: bash script.sh {{ env }} {{ 'varx='~varx if varx is defined else '' }}

Inside script.sh (linux bash) I am calling playbook2:

#!/bin/bash
ENV=$1
varx=$2

do_stuff() {

local arguments= "env=$ENV { 'varx='~varx if varx is defined else '' }}"

ansible-playbook -i inventory/inventory2.yml playbook2.yml --extra-vars "$arguments"

}

do_stuff

The idea is to pass varx from inventory1.yml if it exists in inventory1.yml to script.sh when it runs playbook2.yml. If it doesn't exist in inventory1.yml it will look for it in inventory2.yml. The code as it is its looking for it in inventory2.yml, so I am not being able to retrieve it from inventory1.yml. I believe that there is some issue in script.sh.

How can I achieve to bring varx from inventory1.yml and if doesn't exist to capture it from inventory2.yml?

I want to make some changes in role1 so that I can pass the vars to script.sh (in a string for example,or in a better way - using ansible-playbook and passing the vars) and than capturing the vars in script.sh and launching playbook2. Any ideas how I can acomplish that?

Basically what I want to do is something like this:

bash script.sh args

and the arguments should all go inside args variable (args= env varx).

What i have done is:

role1 - main.yml:

---
- name: role1
  command: bash script.sh arguments
  arguments: "{{ env }} {{ 'varx='~varx if varx is defined else '' }}"

But when It reaches script.sh the varx is not being read.

CodePudding user response:

Minimal reproducible example

Given the inventory

shell> cat inventory1
env1:
  hosts:
    env1.domain.com:
      ansible_host: 10.1.0.61
      ansible_user: admin
env2:
  hosts:
    env2.domain.com:
      ansible_host: 10.1.0.62
      ansible_user: admin
  vars:
    varX: valueX

, the playbook

shell> cat pb1.yml
- hosts: all
  gather_facts: false
  tasks:
    - command: "sh -c '/tmp/script.sh {{ env }} {{ _varX }}'"
      register: out
      vars:
        _varX: "varX={{ varX|default('') }}"
    - debug:
        var: out.stdout

, and the scripts at the remote hosts

shell> ssh [email protected] cat /tmp/script.sh
echo ansible-playbook -i inventory2 pb2.yml -e "$1 $2"

shell> ssh [email protected] cat /tmp/script.sh
echo ansible-playbook -i inventory2 pb2.yml -e "$1 $2"

The playbook gives

shell> ansible-playbook -i inventory1 pb1.yml -e "env=env1"

PLAY [all] ************************************************************************************

TASK [command] ********************************************************************************
changed: [env2.domain.com]
changed: [env1.domain.com]

TASK [debug] **********************************************************************************
ok: [env1.domain.com] => 
  out.stdout: ansible-playbook -i inventory2 pb2.yml -e env1 varX=
ok: [env2.domain.com] => 
  out.stdout: ansible-playbook -i inventory2 pb2.yml -e env1 varX=valueX

PLAY RECAP ************************************************************************************
env1.domain.com            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
env2.domain.com            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

CodePudding user response:

Fo me it worked like this:

---
- name: role1
  command: bash script.sh 
    env={{env}}
    {{ 'varx='~varx if varx is defined else '' }}

In the shell script I capture vars like this:

#!/bin/bash

for arg in "$@"; do
    arguments ="$arg "
done

do_stuff() {
    
ansible-playbook -i inventory/inventory2.yml playbook2.yml --extra-vars "$arguments"

}

do_stuff
  • Related