admin管理员组文章数量:1130349
I am trying to write ansible code first time to handle json.
I have a file in my /home/ which has following json data.
here is the input json
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "windows2011"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
I want to update password of the user in user's object where name is admin
so i that output file has updated password for user admin. the output file should be located in /home/location with _updated.json extension.
the output json should like this
{
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "updatedpassword"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
ansible code which i have written so far-
---
- name: Update admin user password in JSON file
hosts: localhost
gather_facts: no
tasks:
- name: Read the JSON file
ansible.builtinmand:
cmd: cat /home/conf.json
register: json_content
Thank you.
I am trying to write ansible code first time to handle json.
I have a file in my /home/ which has following json data.
here is the input json
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "windows2011"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
I want to update password of the user in user's object where name is admin
so i that output file has updated password for user admin. the output file should be located in /home/location with _updated.json extension.
the output json should like this
{
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "updatedpassword"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
ansible code which i have written so far-
---
- name: Update admin user password in JSON file
hosts: localhost
gather_facts: no
tasks:
- name: Read the JSON file
ansible.builtin.command:
cmd: cat /home/conf.json
register: json_content
Thank you.
Share Improve this question asked Dec 2, 2024 at 21:09 exceed007exceed007 1333 silver badges10 bronze badges 2 |2 Answers
Reset to default 2- Declare the input file
input_file: data.json
and read it into a dictionary
- include_vars:
file: "{{ input_file }}"
name: data
gives
data:
acl:
- browse: true
create: false
- browse: false
create: true
groups:
- location: texas
name: abc
- location: austin
name: def
users:
- description: administrator
name: admin
password: windows2011
- description: guest
name: testuser
password: testpassword
- Declare the updated users
users:
- name: admin
password: updatedpassword
and merge them
update: "{{ [data.users, users] | community.general.lists_mergeby('name')
| community.general.dict_kv('users') }}"
gives
update:
users:
- description: administrator
name: admin
password: updatedpassword
- description: guest
name: testuser
password: testpassword
- Update data
data_update: "{{ [data, update] | combine }}"
gives
data_update:
acl:
- browse: true
create: false
- browse: false
create: true
groups:
- location: texas
name: abc
- location: austin
name: def
users:
- description: administrator
name: admin
password: updatedpassword
- description: guest
name: testuser
password: testpassword
- Declare the output file and write it
- copy:
dest: "{{ output_file }}"
content: |
{{ data_update | to_nice_json }}
vars:
arr: "{{ input_file | splitext | list }}"
output_file: "{{ arr.0 }}_updated{{ arr.1 }}"
gives what you want
shell> diff data.json data_updated.json
26c26
< "password": "windows2011"
---
> "password": "updatedpassword"
Example of a complete playbook for testing
- hosts: localhost
vars:
input_file: data.json
users:
- name: admin
password: updatedpassword
update: "{{ [data.users, users] | community.general.lists_mergeby('name')
| community.general.dict_kv('users') }}"
data_update: "{{ [data, update] | combine }}"
tasks:
- include_vars:
file: "{{ input_file }}"
name: data
- debug:
var: data
- debug:
var: update
- debug:
var: data_update
- copy:
dest: "{{ output_file }}"
content: |
{{ data_update | to_nice_json }}
vars:
arr: "{{ input_file | splitext | list }}"
output_file: "{{ arr.0 }}_updated{{ arr.1 }}"
What you can do is use the template module and use "variable interpolation" to put in the password you wish to use. First update your conf.json file:
{
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "{{ admin_password }}"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
Then update your Ansible script to replace the variable with the new password:
---
- name: Update admin user password in JSON file
hosts: localhost
gather_facts: no
vars:
admin_password: 'updatedpassword'
tasks:
- name: Update the JSON configuration
ansible.builtin.template:
src: /home/conf.json
dest: /home/updated_conf.json
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html
You could also do the same with the testuser account by replacing the password with {{ testuser_password }} and then adding the variable and value to the Ansible script.
I am trying to write ansible code first time to handle json.
I have a file in my /home/ which has following json data.
here is the input json
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "windows2011"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
I want to update password of the user in user's object where name is admin
so i that output file has updated password for user admin. the output file should be located in /home/location with _updated.json extension.
the output json should like this
{
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "updatedpassword"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
ansible code which i have written so far-
---
- name: Update admin user password in JSON file
hosts: localhost
gather_facts: no
tasks:
- name: Read the JSON file
ansible.builtinmand:
cmd: cat /home/conf.json
register: json_content
Thank you.
I am trying to write ansible code first time to handle json.
I have a file in my /home/ which has following json data.
here is the input json
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "windows2011"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
I want to update password of the user in user's object where name is admin
so i that output file has updated password for user admin. the output file should be located in /home/location with _updated.json extension.
the output json should like this
{
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "updatedpassword"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
ansible code which i have written so far-
---
- name: Update admin user password in JSON file
hosts: localhost
gather_facts: no
tasks:
- name: Read the JSON file
ansible.builtin.command:
cmd: cat /home/conf.json
register: json_content
Thank you.
Share Improve this question asked Dec 2, 2024 at 21:09 exceed007exceed007 1333 silver badges10 bronze badges 2-
Are you wanting the Ansible script to do the actual updating of the password? Or are you wanting to change the password in the JSON file and then copy the updated file to
updated_conf.json? Thecopymodule is really nice for copying files, but thetemplatemodule is amazing for substituting values in files. – Magnie Mozios Commented Dec 2, 2024 at 21:29 - I want to update the password using ansible script – exceed007 Commented Dec 2, 2024 at 21:30
2 Answers
Reset to default 2- Declare the input file
input_file: data.json
and read it into a dictionary
- include_vars:
file: "{{ input_file }}"
name: data
gives
data:
acl:
- browse: true
create: false
- browse: false
create: true
groups:
- location: texas
name: abc
- location: austin
name: def
users:
- description: administrator
name: admin
password: windows2011
- description: guest
name: testuser
password: testpassword
- Declare the updated users
users:
- name: admin
password: updatedpassword
and merge them
update: "{{ [data.users, users] | community.general.lists_mergeby('name')
| community.general.dict_kv('users') }}"
gives
update:
users:
- description: administrator
name: admin
password: updatedpassword
- description: guest
name: testuser
password: testpassword
- Update data
data_update: "{{ [data, update] | combine }}"
gives
data_update:
acl:
- browse: true
create: false
- browse: false
create: true
groups:
- location: texas
name: abc
- location: austin
name: def
users:
- description: administrator
name: admin
password: updatedpassword
- description: guest
name: testuser
password: testpassword
- Declare the output file and write it
- copy:
dest: "{{ output_file }}"
content: |
{{ data_update | to_nice_json }}
vars:
arr: "{{ input_file | splitext | list }}"
output_file: "{{ arr.0 }}_updated{{ arr.1 }}"
gives what you want
shell> diff data.json data_updated.json
26c26
< "password": "windows2011"
---
> "password": "updatedpassword"
Example of a complete playbook for testing
- hosts: localhost
vars:
input_file: data.json
users:
- name: admin
password: updatedpassword
update: "{{ [data.users, users] | community.general.lists_mergeby('name')
| community.general.dict_kv('users') }}"
data_update: "{{ [data, update] | combine }}"
tasks:
- include_vars:
file: "{{ input_file }}"
name: data
- debug:
var: data
- debug:
var: update
- debug:
var: data_update
- copy:
dest: "{{ output_file }}"
content: |
{{ data_update | to_nice_json }}
vars:
arr: "{{ input_file | splitext | list }}"
output_file: "{{ arr.0 }}_updated{{ arr.1 }}"
What you can do is use the template module and use "variable interpolation" to put in the password you wish to use. First update your conf.json file:
{
"acl": [
{
"browse": true,
"create": false
},
{
"browse": false,
"create": true
}
],
"groups": [
{
"name": "abc",
"location": "texas"
},
{
"name": "def",
"location": "austin"
}
],
"users": [
{
"name": "admin",
"description": "administrator",
"password": "{{ admin_password }}"
},
{
"name": "testuser",
"description": "guest",
"password": "testpassword"
}
]
}
Then update your Ansible script to replace the variable with the new password:
---
- name: Update admin user password in JSON file
hosts: localhost
gather_facts: no
vars:
admin_password: 'updatedpassword'
tasks:
- name: Update the JSON configuration
ansible.builtin.template:
src: /home/conf.json
dest: /home/updated_conf.json
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html
You could also do the same with the testuser account by replacing the password with {{ testuser_password }} and then adding the variable and value to the Ansible script.
本文标签: ansiblehow to replace and filter json dataStack Overflow
版权声明:本文标题:ansible , how to replace and filter json data? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1736195075a1390824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


updated_conf.json? Thecopymodule is really nice for copying files, but thetemplatemodule is amazing for substituting values in files. – Magnie Mozios Commented Dec 2, 2024 at 21:29