В моей конфигурации Ansible у меня есть следующее:
new_attributes:
tenantID: "4234iugob8y8l3-8809yo3x"
new_attribute2: "value2"
и в моих задачах следующее:
"{{ new_attributes | to_json }}"
и это в моем скрипте Python для доступа к нему:
try:
new_attributes = json.loads(new_attributes)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
sys.exit(1)
но получаю следующую ошибку:
"stderr_lines": [],
"stdout": "Error decoding JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)",
"stdout_lines": ["Error decoding JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"]}
Мне интересно, может ли кто-нибудь помочь это исправить.
обновление:
вывод ошибки:< /p>
Error decoding JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"
и вывод из to_json на стороне ansible:
msg": "{\"tenantID\": \"4234iugob8y8l3-8809yo3x\", \"new_attribute2\": \"value2\"}
Вот как я анализирую Python:
- name: Debug new_attributes as JSON
debug:
msg: "{{ new_attributes | to_json }}"
- name: Insert data into DynamoDB for client "{{ client_name }}"
command: >
python3 dynamodb_insert_or_update.py
"{{ dynamodb_table_name }}"
"{{ client_name }}"
"{{ applicationID }}"
"{{ new_attributes | to_json }}"
"{{ aws_region }}"
"{{ aws_profile }}"
args:
chdir: "/mypath/scripts/"
register: result
loop: "{{ clients }}"
loop_control:
loop_var: item
и мой скрипт Python полностью:
import sys
import boto3
import json
def insert_data(dynamodb_table_name, client_name, item_id, new_attributes, aws_region, aws_profile):
session = boto3.Session(profile_name=aws_profile, region_name=aws_region)
dynamodb = session.resource('dynamodb')
table = dynamodb.Table(dynamodb_table_name)
item = {
'client_name': client_name,
'applicationID': item_id
}
try:
new_attributes = json.loads(new_attributes)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
sys.exit(1)
item.update(new_attributes)
try:
response = table.put_item(Item=item)
print(f"Insert successful: {response}")
except Exception as e:
print(f"Failed to insert item: {e}")
sys.exit(1)
# Command-line arguments
dynamodb_table_name = sys.argv[1]
client_name = sys.argv[2]
item_id = sys.argv[3]
new_attributes = sys.argv[4]
aws_region = sys.argv[5]
aws_profile = sys.argv[6]
# Call the insert function
insert_data(dynamodb_table_name, client_name, item_id, new_attributes, aws_region, aws_profile)
Подробнее здесь: https://stackoverflow.com/questions/789 ... ed-in-doub
Ansible для Python: ошибка декодирования JSON: ожидается имя свойства, заключенное в двойные кавычки: строка 1, столбец ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение