1

I'm working on passing parameters/vars to an ansible role dynamically by reading them from a JSON file.

Consider the following role call (with optional parameters like phone, address, email, city)

- name: Ansible role | Create
  include_role:
    name: ansible-role-create-user
  vars:
    persons:
    - name: "john"
      phone:
      - name: "home"
        number: 9999
      - name: "mobile"
        number: "9898"
- name: Ansible role | Create
  include_role:
    name: ansible-role-create-user
  vars:
    persons:
    - name: "Doe"
      email: "johndoe@home.com"
      city: "Skyland"

Above mentioned code is two variants of calling the role. With its optional params, I'm trying to read that data from a JSON and feed it to the role

JSON eg: for call 1:

{
  "name": "John",
  "phone": 
  {
    {"name": "home",   number: "9999"},
    {"name": "mobile",   number: "9898"}
  }  
}

for call 2:

{
  "name": "Doe",
  "email": "johndoe@home.com",
  "city": "skyland"
}

As stated earlier, I would like to read the example JSON files and pass them as params to the role dynamically. Have tried an approach of reading the file to a register and sending the var to the role, but I'm facing the error of

{ "msg" "'unicode object' has no attribute 'name'"}

sample of how I'm sending it

- name: cat json to file
  shell: cat jsonfile.json
  register: register_with_json
- name: Ansible role | Create
  include_role:
    name: ansible-role-create-user
  vars: "{{ register_with_json.stdout | from_json }}"
r0r0n0a
  • 173
  • 2
  • 2
  • 10
  • In your example, the dictionaries `persons` are not the same structure in YAML and JSON. You'might want to decide which structure you'd like to use and fix the data. – Vladimir Botka Aug 06 '20 at 14:39
  • The data in call 1 is not valid JSON. You might want to validate the data before posting here. – Vladimir Botka Aug 06 '20 at 14:54

1 Answers1

0

There are more options. For example, use include_vars. The role and the files

shell> cat roles/ar_create_user/tasks/main.yml
- debug:
    var: persons
shell> cat call1.yml 
persons:
  - name: "john"
    phone:
      - name: "home"
        number: 9999
      - name: "mobile"
        number: "9898"

shell> cat call2.yml 
persons:
  - name: "Doe"
    email: "johndoe@home.com"
    city: "Skyland"

with the playbook

shell> cat playbook.yml
- hosts: localhost
  tasks:

    - include_vars: call1.yml
    - name: Ansible role | Create
      include_role:
        name: ar_create_user

    - include_vars: call2.yml
    - name: Ansible role | Create
      include_role:
        name: ar_create_user

give (abridged)

shell> ansible-playbook playbook.yml

ok: [localhost] => 
  persons:
  - name: john
    phone:
    - name: home
      number: 9999
    - name: mobile
      number: '9898'

ok: [localhost] => 
  persons:
  - city: Skyland
    email: johndoe@home.com
    name: Doe

Q: "I need to get the var(persons) from JSON."

A: The files below in JSON give the same results

shell> cat call1.yml
{"persons": [
   {"name": "john",
    "phone": [
      {"name": "home",
       "number": 9999},
      {"name": "mobile",
       "number": "9898"}]
       }]
    }

shell> cat call2.yml
{"persons": [
   {"name": "Doe",
    "email": "johndoe@home.com",
    "city": "Skyland"}]
    }
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Sorry If I wasn't clear. But first, I need to get the var(persons) from JSON. I currently am doing a cat and storing that to register. But it's throwing the mentioned error, so wanted to try out alternate ways for that. I have also tried lookup with set_fact to get the var(persons). – r0r0n0a Aug 06 '20 at 14:22
  • YAML and JSON are not the same structures in your question. I've updated the answer with the correct JSON format. [Use shell only when shell functionality is required](https://github.com/ansible/ansible-lint/blob/master/lib/ansiblelint/rules/UseCommandInsteadOfShellRule.py). – Vladimir Botka Aug 06 '20 at 14:34
  • Thanks, @Vladimir Botka, your answer helped me get a way to read the vars. I was having issues parsing some escape characters in my script and was able to find the error checking things out from start. – r0r0n0a Aug 28 '20 at 04:41