Home > Software engineering >  Checking the key value in a JSON file
Checking the key value in a JSON file

Time:04-12

I'm having trouble verifying the value of a json file on a remote server. I have to overwrite the file once on the remote machine from the template (j2). After that, I start a service that writes additional values to this file. But when restarting ansible-playbook, this file is overwritten because it differs from the template. Before starting the task of writing a file from a template, I want to check the file for unique values.

For testing on a local machine, I do this and everything works:

- name: Check file
  hosts: localhost
  vars:
    config: "{{ lookup('file','config.json') | from_json }}"

tasks:
  - name: Check info 
    set_fact:
      info: "{{ config.Settings.TimeStartUP }}"

  - name: Print info
    debug:
      var: info

  - name: Create directory
    when: interfaces | length != 0
    ansible.builtin.file:
      ...

But when I try to do the same in a task on a remote machine, for some reason ansible is looking for a file on the local machine

all.yml

---
config_file: "{{ lookup('file','/opt/my_project/config.json') | from_json }}"

site.yml

---
- name: Install My_project
  hosts: server
  tasks:
    - name: Checking if a value exists
      set_fact:
        info: "{{ config_file.Settings.TimeStartUP }}"
    - name: Print info
      debug:
        var: info

Error:

fatal: [server]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('file','/opt/my_project/config.json') | from_json }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /opt/my_project/config.json. could not locate file in lookup: /opt/my_project/config.json"}

Please tell me how to correctly check the key value in a JSON file on a remote server?

CodePudding user response:

fetch the files from the remote hosts first. For example, given the files below for testing

shell> ssh admin@test_11 cat /tmp/config.json
{"Settings": {"TimeStartUP": "today"}}
shell> ssh admin@test_12 cat /tmp/config.json
{"Settings": {"TimeStartUP": "yesterday"}}

The playbook below

- hosts: test_11,test_12
  gather_facts: false
  tasks:
    - file:
        state: directory
        path: "{{ playbook_dir }}/configs"
      delegate_to: localhost
      run_once: true
    - fetch:
        src: /tmp/config.json
        dest: "{{ playbook_dir }}/configs"
    - include_vars:
        file: "{{ config_path }}"
        name: config
      vars:
        config_path: "{{ playbook_dir }}/configs/{{ inventory_hostname }}/tmp/config.json"
    - debug:
        var: config.Settings.TimeStartUP

will create the directory configs in playbook_dir on the controller and will fetch the files from the remote hosts into this directory. See the parameter dest on how the path will be created

shell> cat configs/test_11/tmp/config.json
{"Settings": {"TimeStartUP": "today"}}
shell> cat configs/test_12/tmp/config.json
{"Settings": {"TimeStartUP": "yesterday"}}

Then include_vars and store the dictionary into the variable config

ok: [test_11] => 
  config.Settings.TimeStartUP: today
ok: [test_12] => 
  config.Settings.TimeStartUP: yesterday
  • Related