Extra Variable For Specific import_playbook (Isolated)

Sorry if I don’t follow the community guidelines properly :folded_hands:

Im new in Ansible and my english is not good, I want to know if there is any way for provide isolated extra variables (--extra-vars) from CLI when we use feature ansible.builtin.import_playbook: bbo-network.yaml. Why I ask this, because without extra variables from CLI, we can make predefine isolate variable for each playbook when include the playbook in others.

Below is how I compose my playbook in bbo-compute-engine.yaml

---
# includes all dependencies
- name: Manage a Network
  ansible.builtin.import_playbook: bbo-network.yaml
  vars:
    with_output: true
    include_state: "present"
    
- name: Manage a External Compute Address
  ansible.builtin.import_playbook: bbo-compute-address.yaml
  vars:
    with_output: true
    include_state: "present"

- name: Manage a Compute Disk
  ansible.builtin.import_playbook: bbo-compute-disk.yaml
  vars:
    with_output: true
    include_state: "present"
# includes all dependencies

- name: Manage Compute Engine
  hosts: localhost
  gather_facts: false
  
  vars:
    cloud_provider: "gcp"
    cloud_provider_resource_type:
      - "compute-engine"
    cloud_provider_auth:
      project_id: "terpusat"
      auth_kind: "serviceaccount"
      service_acount_token: "../../credential.json"
    cloud_provider_resource_detail:
      compute_instance:
        name: "bbo-instance-01"
        zone: "asia-southeast2-a"
        machine_type: "n1-standard-1"
        can_ip_forward: true
        labels:
          creator: "dpanel"
          environment: "development"
        disks:
        - auto_delete: 'true'
          boot: 'true'
          source: "{{ output_compute_disk['bbo-disk-01'] }}"
        network_interfaces:
        - network: "{{ output_compute_network['default'] }}"
          access_configs:
          - name: External NAT
            nat_ip: "{{ output_compute_address['bbo-external-address-01'] }}"
            type: ONE_TO_ONE_NAT

  roles:
    - role: dpanel.cloud-provider

Then I want to make the variable configurable from CLI, so I try to send variable from CLI with command:

ansible-playbook --extra-vars @/Users/prakasa/Projects/bbo-provision/ansible/extra-variables.json -vv playbooks/staging/bbo-compute-engine.yaml

Unfortunatelly, the method I tried make the variable available globally and replace all variables in included playbook as well. So the imported playbooks lost their isolated condition (all variables totally replaced by --extra-vars).

I expect, when we use --extra-variable, it is only available for the main playbook, but it’s not.

This is exactly how import_playbook works. It statically assembles your playbook from a number of smaller playbooks, let’s call them “snippets” for now. Thus, in memory, there is only a single global playbook and variables are scoped accordingly. There is no per snippet variable scoping.

If you want to scope the variables per snippet, you will have to remove any variable naming overlap and put variable name prefixing or use some other namespacing strategy like putting the variables inside a dict. Anyway, your variable names should be unique so that extra vars do not overlap/hide them.

1 Like

Ic, it still count as single global playbook. Thanks for the answer @bvitnik , appreciate it.. :folded_hands:

Extra vars have the highesst priority whhen it comes to variables. I would refer to the following document which explains it.