简体   繁体   中英

Ansible “set_fact” repository url from json file using filters like “from_json”

Using Ansible "set_fact" module, I need to get repository url from json file using filters like "from_json". I tried in couple ways, and still doesn't get it how is should work.

- name: initial validation
  tags: bundle
  hosts: localhost
  connection: local

  tasks:
  - name: register bundle version_file
    include_vars:
      file: '/ansible/playbook/workbench-bundle/bundle.json'
    register: bundle

  - name: debug registered bundle file
    debug:
      msg: '{{ bundle }}'

I get json that I wanted:

TASK [debug registered bundle file] ************************************************
ok: [127.0.0.1] => {
    "msg": {
        "ansible_facts": {
            "engine-config": "git@bitbucket.org/engine-config.git",
            "engine-monitor": "git@bitbucket.org/engine-monitor.git",
            "engine-server": "git@bitbucket.org/engine-server.git",
            "engine-worker": "git@bitbucket.org/engine-worker.git"        
    },
        "changed": false
    }
}

And then I'm trying to select each value by key name to use this value as URL to "npm install" each package in separate instances.

  - name: set_fact some paramater
    set_fact:
      engine_url: "{{ bundle.('engine-server') | from_json }}"

And then I get error:

fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "template error while templating string: expected name or number. String: {{ bundle.('engine-server') }}"}

I many others ways like this loopkup , and it still fails with others errors. Can someone help to understand, how I can find each parameter and store him as "set_fact"? Thanks

Here is a sample working code to set a variable like in the question (although I don't see much sense in it):

- name: initial validation
  tags: bundle
  hosts: localhost
  connection: local

  tasks:
  - name: register bundle version_file
    include_vars:
      file: '/ansible/playbook/workbench-bundle/bundle.json'
      name: bundle

  - debug:
      var: bundle

  - debug:
      var: bundle['engine-server']

  - name: set_fact some paramater
    set_fact:
      engine_url: "{{ bundle['engine-server'] }}"

The above assumes your input data (which you did not include) is:

{
  "engine-config": "git@bitbucket.org/engine-config.git",
  "engine-monitor": "git@bitbucket.org/engine-monitor.git",
  "engine-server": "git@bitbucket.org/engine-server.git",
  "engine-worker": "git@bitbucket.org/engine-worker.git"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM