JPNagarajan
JPNagarajan

Reputation: 928

How to write varibles/hard code values in nested json in ansible?

I'm trying to create a json file with hard codes valuesas a output in nested json.But the second play is overwriting the first play value.So do we have any best option to do this?

I have tried with to_nice_json template to copy the variable to json file.But not able to keep multiple variable values in imported_var to copy to json file

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
  - name: load var from file
    include_vars:
      file: /tmp/var.json
      name: imported_var

  - name: Checking mysqld status
    shell: service mysqld status
    register: mysqld_stat
    ignore_errors: true

  - name: Checking mysqld status
    shell: service httpd status
    register: httpd_stat
    ignore_errors: true

  - name: append mysqld status to output json
    set_fact:
     imported_var: "{{ imported_var | combine({ 'status_checks':[{'mysqld_status': (mysqld_stat.rc == 0)|ternary('good', 'bad') }]})}}"

# - name: write var to file
 #   copy:
  #    content: "{{ imported_var | to_nice_json }}"
   #   dest: /tmp/final.json

  - name: append httpd status to output json
    set_fact:
      imported_var: "{{ imported_var| combine({ 'status_checks':[{'httpd_status': (httpd_stat.rc == 0)|ternary('good', 'bad') }]})}}"

 # - debug:
  #    var: imported_var

  - name: write var to file
    copy:
      content: "{{ imported_var | to_nice_json }}"
      dest: /tmp/final.json

Expected result:

{
    "status_checks": [
        {
            "mysqld_status": "good"
            "httpd_status": "good"
        }
    ]
}

Actual result:

{
    "status_checks": [
        {
            "httpd_status": "good"
        }
    ]
}

Upvotes: 0

Views: 1173

Answers (1)

larsks
larsks

Reputation: 311606

You're trying to perform the sort of data manipulation that Ansible really isn't all that good at. Any time you attempt to modify an existing variable -- especially if you're trying to set a nested value -- you're making life complicated. Having said that, it is possible to do what you want. For example:

---
- hosts: localhost
  gather_facts: false
  vars:
    imported_var: {}

  tasks:
    - name: Checking sshd status
      command: systemctl is-active sshd
      register: sshd_stat
      ignore_errors: true

    - name: Checking httpd status
      command: systemctl is-active httpd
      register: httpd_stat
      ignore_errors: true

    - set_fact:
        imported_var: "{{ imported_var|combine({'status_checks': []}) }}"

    - set_fact:
        imported_var: >-
          {{ imported_var|combine({'status_checks':
          imported_var.status_checks + [{'sshd_status': (sshd_stat.rc == 0)|ternary('good', 'bad')}]}) }}

    - set_fact:
        imported_var: >-
          {{ imported_var|combine({'status_checks':
          imported_var.status_checks + [{'httpd_status': (httpd_stat.rc == 0)|ternary('good', 'bad')}]}) }}

    - debug:
        var: imported_var

On my system (which is running sshd but is not running httpd, this will output:

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "imported_var": {
        "status_checks": [
            {
                "sshd_status": "good"
            }, 
            {
                "httpd_status": "bad"
            }
        ]
    }
}

You could dramatically simplify the playbook by restructuring your data. Make status_checks a top level variable, and instead of having it be a list, have it be a dictionary that maps a service name to the corresponding status. Combine this with some loops and you end up with something that is dramatically simpler:

---
- hosts: localhost
  gather_facts: false

  tasks:

    # We can use a loop here instead of writing a separate task
    # for each service.
    - name: Checking service status
      command: systemctl is-active {{ item }}
      register: services
      ignore_errors: true
      loop:
        - sshd
        - httpd

    # Using a loop in the previous task means we can use a loop
    # when creating the status_checks variable, which again removes
    # a bunch of duplicate code.
    - name: set status_checks variable
      set_fact:
        status_checks: "{{ status_checks|default({})|combine({item.item: (item.rc == 0)|ternary('good', 'bad')}) }}"
      loop: "{{ services.results }}"

    - debug:
        var: status_checks

The above will output:

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "status_checks": {
        "httpd": "bad", 
        "sshd": "good"
    }
}

If you really want to add this information to your imported_var, you can do that in a single task:

- set_fact:
    imported_var: "{{ imported_var|combine({'status_checks': status_checks}) }}"

Upvotes: 1

Related Questions