Search code examples
apache-sparkvariablesansibleansible-facts

Variables declared in one file in another Ansible


I have an ansible playbook called sparkmaster.yml and these are the following tasks:

  1. To launch an ec2 instance
  2. To ssh into it
  3. To store the private_dns_name into a variable

I need the private_dns_name of the instance stored in the variable in another file sparkslaves.yml to launch spark slaves. I searched online but didn't find anything, please help.

Sparkmaster.yml:

- name: Launch EC2 Instance
  hosts: localhost
  connection: local
  #gather_facts: False
  tasks:
    - name: Launch EC2 instance
      include_role:
        name: launch_ec2

    - name: Add new instance to host
      add_host:
        hostname: "{{ item.public_ip_address }}"
        groupname: launched
      loop: "{{ ec2.instances }}"

    - name: Wait for SSH to come up
      delegate_to: "{{ item.public_ip_address }}"
      wait_for_connection:
        delay: 60
        timeout: 320
      loop: "{{ ec2.instances }}"

    - name: Set private ipv4 dns
        set_fact:
          private_dns_name: "{{ item.private_dns_name }}"
        loop: "{{ ec2.instances }}"

Sparkslaves.yml:

- name: Add scripts/start-slave.sh
      ansible.builtin.blockinfile:
        path: /home/ubuntu/scripts/start-slave.sh
        create: yes
        block: |
          #/bin/sh
          SPARK_MASTER_URL=spark://{{ private_dns_name }}:7077
        insertbefore: BOF

Solution

  • in my sample i have 2 playbooks in different files:

    playbook1: i set the var private_dns

    - name: playbook1.0
      hosts: localhost
    
      tasks:
      - name: set var
        set_fact:
          private_dns: toto.com
    

    playbook2: i get the var private_dns from hostvars

    - name: playbook2.0
      hosts: localhost
    
      tasks:
      - name: get var
        set_fact:
          private_dns: "{{ hostvars['localhost']['private_dns'] }}"
      
      - name: print var
        debug:
          var: private_dns
    

    and combine.yml, which launches the both playbook sequentially

    # Combine multiple playbooks
      - import_playbook: play1.yml
      - import_playbook: play2.yml
    

    result:

    PLAY [playbook1.0] *************************
    
    TASK [Gathering Facts] ********************************************
    Wednesday 03 November 2021  10:23:52 +0000 (0:00:00.009)       0:00:00.009 **** 
    ok: [localhost]
    
    TASK [set var] *******************************************
    Wednesday 03 November 2021  10:23:53 +0000 (0:00:00.714)       0:00:00.724 **** 
    ok: [localhost]
    
    PLAY [playbook2.0] **************************************
    
    TASK [get var] ********************************************************
    Wednesday 03 November 2021  10:23:53 +0000 (0:00:00.029)       0:00:00.753 **** 
    ok: [localhost]
    
    TASK [print var] ****************************************************
    Wednesday 03 November 2021  10:23:53 +0000 (0:00:00.036)       0:00:00.789 **** 
    ok: [localhost] => 
      private_dns: toto.com