Skip to content

Example 7

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

bucket.json

{
  "list1": [
    "value1", "value2", "value3"
  ],
  "dict1": {
    "key1": "value1",
    "key2": "value2"
  },
  "var1": "var1_value"
}

playbook.yml

- name: Example playbook
  hosts: centos1
  gather_facts: false
  vars_files:
    - bucket.json
  tasks:
    - name: An external variable stored in JSON format
      debug:
        msg: "{{ var1 }}"
    - name: An external dictionary stored in JSON format
      debug:
        msg: "{{ dict1 }}"
    - name: Accessing the dictionary using the Python syntax
      debug:
        msg: "{{ dict1['key1'] }}"
    - name: Accessing the dictionary using the dot notation
      debug:
        msg: "{{ dict1.key1 }}"
    - name: An external list stored in JSON format
      debug:
        msg: "{{ list1 }}"
    - name: Accessing the values using the Python syntax
      debug:
        msg: "{{ list1[0] }}"
    - name: Acccesing the values using the dot notation
      debug:
        msg: "{{ list1.0 }}"

Tip

The vars_file directive also accepts thje JSON file format.

Execute the following command

ansible-playbook playbook.yml