1

I have a json like below

{
"nodes":[
  {
     "node_values":[
        "[test1]",
        "10.33.11.189",
        "10.33.11.185"
     ]
  },
  {
     "node_values":[
        "[test2]",
        "10.33.11.189",
        "10.33.11.185"
     ]
  }
]
}

I am trying to read only the node values and put it in the text files. I am using below ansible code

  hosts: localhost
  vars:
    tmpdata1: "{{ lookup('file','test.json')|from_json }}"

  tasks:
    - name: Add mappings to /etc/hosts
      blockinfile:
        path: /home/s57232/Ansible-Install/Install_Inventory.txt
        content:  item
        marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.node_values[0] }}"
      loop: "{{ tmpdata1 |json_query('nodes[*].node_values[*]') }}"

i am getting below error

**TASK [Add mappings to /etc/hosts] **********************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'node_values'\n\nThe error appears to have been in '/home/s57232/Ansible-Install/prepare_inventory.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Add mappings to /etc/hosts\n      ^ here\n"}**

When i am trying to read with with items and file format with out the blockinfile, if the same IP is there in multiple places, it is not writing, because it is looking for unique values. I am not able to proceed further. Can anyone please help me?

when i am using

- name: Add mappings to /etc/hosts
  blockinfile:
    path: /home/s57232/Ansible-Install/Install_Inventory.txt
    content:  "{{ item.node_values }}"
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.node_values[0] }}"
  loop: "{{ tmpdata1 |json_query('nodes[*]') }}"

I am getting

# BEGIN ANSIBLE MANAGED BLOCK [test1]
['[test1]', '10.33.11.189', '10.33.11.185']
# END ANSIBLE MANAGED BLOCK [test1]
# BEGIN ANSIBLE MANAGED BLOCK [test2]
['[test2]', '10.33.11.189', '10.33.11.185']
# END ANSIBLE MANAGED BLOCK [test2]

my expectation is

# BEGIN ANSIBLE MANAGED BLOCK [test1]
[test1]
10.33.11.189
10.33.11.185
# END ANSIBLE MANAGED BLOCK [test1]
# BEGIN ANSIBLE MANAGED BLOCK [test2]
[test2]
10.33.11.189
10.33.11.185
# END ANSIBLE MANAGED BLOCK [test2]
2
  • What is your expectation? ・ Also, do not add code in comments, use edit link to change your question. Commented Sep 5, 2018 at 19:46
  • i just mentioned what is my expectation. Please help me Commented Sep 5, 2018 at 19:49

1 Answer 1

1

Here you are:

- name: Add mappings to /etc/hosts
  blockinfile:
    path: /home/s57232/Ansible-Install/Install_Inventory.txt
    content:  "{{ item.node_values | join('\n') }}"
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.node_values.0 }}"
  loop: "{{ tmpdata1.nodes }}"

You don't need to use JMESPath unless you wanted to filter some values. Other than that, you have two lists: one to loop over, the other to join the elements with a newline character.

Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful. you made my day. Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.