在ansible中将字符串转换为整数
converting string to integer in ansible
在 ansible 中,我是 运行 为某些项目创建保留规则的角色。使用 PUT 方法我将结果存储在寄存器模块中,从那里我试图读取 retention_id: "59"
和 project_id: "80"
我需要它作为整数,如 59 and 80
,但它正在作为字段的字符串但是我需要它作为整数。我试图通过使用 "{{ item.project_id | int }}"
来改变它
和 {{ item.metadata.retention_id | int}}
它没有更改为整数,为了运气,我尝试将 yaml 正文转换为 JSON。我仍然收到错误。
一周以来我就被这个任务打动了。请提供任何解决方案来解决这个问题。
您的回复将不胜感激。谢谢
实际剧本:
---
# List of projects to check retention and project ID's
- name: get projects
uri:
url: "https://{{ ansible_host }}/api/v2.0/projects"
method: GET
headers:
Authorization: "{{ admin_passwd }}"
Accept: application/json
validate_certs: no
register: projects_result
- debug:
var: projects_result.json | flatten(levels=1)
# Create retention rules for the projects
- name: creating retention rules
uri:
url: "https://{{ ansible_host }}/api/v2.0/retentions/{{ item.metadata.retention_id | int}}"
method: PUT
body_format: json
headers:
Content-Type: application/json
Authorization: "{{ admin_passwd }}"
Accept: 'application/json, text/plain, */*'
body:
algorithm: "or"
id: "{{ item.metadata.retention_id | int }}"
rules:
- action: "retain"
params:
latestPushedK: 3
scope_selectors:
repository:
- decoration: "repoMatches"
kind: "doublestar"
pattern: "**"
tag_selectors:
- decoration: "matches"
extras: "{\"untagged\":true}"
kind: "doublestar"
pattern: "**"
template: latestPushedK
scope:
level: "project"
ref: "{{ item.project_id | int }}"
trigger:
kind: "Schedule"
settings:
cron: "0 0 * * * *"
validate_certs: no
status_code: 200
when: "item.registry_id is defined"
loop: "{{ projects_result.json | flatten(levels=1) }}"
ignore_errors: yes
有了这个我得到了这个错误:
$ ansible-playbook -i ansible/inventories/shiplab ansible/playbooks/harbor-configurations.yaml --ask-vault-pass
Vault password:
PLAY [harbor] ***********************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [harbor_shiplab_test1e]
TASK [../roles/harbor-retention-rules : get projects] *******************************************************************************************************
ok: [harbor_shiplab_test1e]
TASK [../roles/harbor-retention-rules : debug] **************************************************************************************************************
ok: [harbor_shiplab_test1e] => {
"projects_result.json | flatten(levels=1)": [
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:28.228Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 56,
"items": [],
"project_id": 75,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "54"
},
"name": "harbor-esl-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 75,
"registry_id": 101,
"repo_count": 0,
"update_time": "2021-10-21T07:35:28.228Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:43.098Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 58,
"items": [],
"project_id": 77,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "56"
},
"name": "harbor-library-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 77,
"registry_id": 103,
"repo_count": 0,
"update_time": "2021-10-21T07:35:43.098Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:13.930Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 54,
"items": [],
"project_id": 73,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "52"
},
"name": "harbor-megatron-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 73,
"registry_id": 104,
"repo_count": 0,
"update_time": "2021-10-21T07:35:13.930Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:50.271Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 59,
"items": [],
"project_id": 78,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "57"
},
"name": "harbor-migration-poc-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 78,
"registry_id": 105,
"repo_count": 0,
"update_time": "2021-10-21T07:35:50.271Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:57.671Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 60,
"items": [],
"project_id": 79,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "58"
},
"name": "harbor-payment-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 79,
"registry_id": 106,
"repo_count": 0,
"update_time": "2021-10-21T07:35:57.671Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:36:05.861Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 61,
"items": [],
"project_id": 80,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "59"
},
"name": "harbor-platform-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 80,
"registry_id": 107,
"repo_count": 0,
"update_time": "2021-10-21T07:36:05.861Z"
},
{
"chart_count": 1,
"creation_time": "2021-08-19T15:50:23.772Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 1,
"items": [],
"project_id": 1,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true"
},
"name": "library",
"owner_id": 1,
"owner_name": "admin",
"project_id": 1,
"repo_count": 3,
"update_time": "2021-08-19T15:50:23.772Z"
},
{
"chart_count": 2,
"creation_time": "2021-08-24T16:45:19.230Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 2,
"items": [],
"project_id": 2,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true"
},
"name": "platform",
"owner_id": 1,
"owner_name": "admin",
"project_id": 2,
"repo_count": 11,
"update_time": "2021-08-24T16:45:19.230Z"
}
]
}
TASK [../roles/harbor-retention-rules : creating retention rules] *******************************************************************************************
failed: [harbor_shiplab_test1e] (item={'chart_count': 0, 'creation_time': '2021-10-21T07:35:28.228Z',
'current_user_role_id': 1, 'current_user_role_ids': [1], 'cve_allowlist': {'creation_time': '0001-01-01T00:00:00.000Z',
'id': 56, 'items': [], 'project_id': 75, 'update_time': '0001-01-01T00:00:00.000Z'},
'metadata': {'public': 'true', 'retention_id': '54'}, 'name': 'harbor-esl-proxy', 'owner_id': 1, 'owner_name': 'admin', 'project_id': 75,
'registry_id': 101, 'repo_count': 0, 'update_time': '2021-10-21T07:35:28.228Z'}) => {"ansible_loop_var": "item", "changed": false,
"connection": "close", "content": "{\"errors\":[{\"code\":\"UNPROCESSABLE_ENTITY\",\"message\":\"validation failure
list:\nparsing policy body from \\"\\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64\"}]}\n",
"content_length": "213", "content_type": "application/json; charset=utf-8", "date": "Fri, 22 Oct 2021 12:26:56 GMT", "elapsed": 1,
"item": {"chart_count": 0, "creation_time": "2021-10-21T07:35:28.228Z", "current_user_role_id": 1, "current_user_role_ids": [1],
"cve_allowlist": {"creation_time": "0001-01-01T00:00:00.000Z", "id": 56, "items": [], "project_id": 75, "update_time": "0001-01-01T00:00:00.000Z"},
"metadata": {"public": "true", "retention_id": "54"}, "name": "harbor-esl-proxy", "owner_id": 1, "owner_name": "admin", "project_id": 75,
"registry_id": 101, "repo_count": 0, "update_time": "2021-10-21T07:35:28.228Z"}, "json": {"errors": [{"code": "UNPROCESSABLE_ENTITY",
"message": "validation failure list:\nparsing policy body from \"\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64"}]},
"msg": "Status code was 422 and not [200]: HTTP Error 422: Unprocessable Entity", "redirected": false, "set_cookie": "sid=2e5f911867c675528b5e23c74f803240; Path=/; HttpOnly",
"status": 422, "url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/retentions/54", "vary": "Accept-Encoding", "x_request_id": "334cb06f-c161-4085-8df2-a448b7776819"}
之后我将正文更改为 json :
json 转换:
- name: creating retention rules
uri:
url: "https://{{ ansible_host }}/api/v2.0/retentions/{{ item.metadata.retention_id | int }}"
method: PUT
body_format: json
headers:
Content-Type: application/json
Authorization: "{{ admin_passwd }}"
Accept: 'application/json, text/plain, */*'
body:
'{ "algorithm": "or",
"id": {{ item.metadata.retention_id | int }},
"rules": [{
"action": "retain",
"params": {
"latestPushedK": 3 },
"scope_selectors":{
"repository": [{
"decoration": "repoMatches",
"kind": "doublestar",
"pattern": "**" } ] },
"tag_selectors": [{
"decoration": "matches",
"extras": "{\"untagged\":true}",
"kind": "doublestar",
"pattern": "**" }],
"template": latestPushedK } ],
"scope": {
"level": "project",
"ref": {{ item.project_id | int }} },
"trigger": {
"kind": "Schedule",
"settings":{
"cron": "0 0 * * * *" }}}'
validate_certs: no
status_code: 200
when: "item.registry_id is defined"
loop: "{{ projects_result.json | flatten(levels=1) }}"
ignore_errors: yes
我也遇到了同样的错误。请帮我解决这个问题。
详细信息:
ansible-playbook -i ansible/inventories/shiplab ansible/playbooks/harbor-configurations.yaml --ask-vault-pass -vvv
ansible-playbook -i ansible/inventories/shiplab ansible/playbooks/harbor-configurations.yaml --ask-vault-pass -vvv
ansible-playbook 2.8.4
config file = /home/user1/repos/harbor/konvoy/ansible.cfg
configured module search path = ['/home/user1/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.7.10 (default, May 5 2021, 11:43:58) [GCC 10.2.0]
Using /home/user1/repos/harbor/konvoy/ansible.cfg as config file
Vault password:
host_list declined parsing /home/user1/repos/harbor/konvoy/ansible/inventories/shiplab/test1e as it did not pass it's verify_file() method
auto declined parsing /home/user1/repos/harbor/konvoy/ansible/inventories/shiplab/test1e as it did not pass it's verify_file() method
Parsed /home/user1/repos/harbor/konvoy/ansible/inventories/shiplab/test1e inventory source with ini plugin
PLAYBOOK: harbor-configurations.yaml ************************************************************************************************************************
1 plays in ansible/playbooks/harbor-configurations.yaml
PLAY [harbor] ***********************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
task path: /home/user1/repos/harbor/konvoy/ansible/playbooks/harbor-configurations.yaml:2
<harbor.tst1e.k8s.shiplab.ss.acl.com> ESTABLISH LOCAL CONNECTION FOR USER: MEA+user1
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo '"'"'~MEA+user1'"'"' && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129 `" && echo ansible-tmp-1634916091.044559-272844769197129="` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129 `" ) && sleep 0'
<harbor_shiplab_test1e> Attempting python interpreter discovery
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<harbor_shiplab_test1e> Python interpreter discovery fallback (unsupported platform for extended discovery: cygwin_nt-10.0)
Using module file /usr/lib/python3.7/site-packages/ansible/modules/system/setup.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> PUT /home/user1/.ansible/tmp/ansible-local-1736l28mza02/tmp5ucaxth_ TO /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/AnsiballZ_setup.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'chmod u+x /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/ /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/AnsiballZ_setup.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '/usr/bin/python /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/AnsiballZ_setup.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'rm -f -r /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/ > /dev/null 2>&1 && sleep 0'
ok: [harbor_shiplab_test1e]
META: ran handlers
TASK [../roles/harbor-retention-rules : get projects] *******************************************************************************************************
task path: /home/user1/repos/harbor/konvoy/ansible/roles/harbor-retention-rules/tasks/main.yml:3
<harbor.tst1e.k8s.shiplab.ss.acl.com> ESTABLISH LOCAL CONNECTION FOR USER: MEA+user1
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo '"'"'~MEA+user1'"'"' && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630 `" && echo ansible-tmp-1634916112.1313658-277975019551630="` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630 `" ) && sleep 0'
Using module file /usr/lib/python3.7/site-packages/ansible/modules/net_tools/basics/uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> PUT /home/user1/.ansible/tmp/ansible-local-1736l28mza02/tmpsm6sxu8_ TO /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/AnsiballZ_uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'chmod u+x /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/ /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '/usr/bin/python /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'rm -f -r /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/ > /dev/null 2>&1 && sleep 0'
ok: [harbor_shiplab_test1e] => {
"changed": false,
"connection": "close",
"content_type": "application/json",
"cookies": {
"sid": "033af4ae913d69a8314a4edb84512aa6"
},
"cookies_string": "sid=033af4ae913d69a8314a4edb84512aa6",
"date": "Fri, 22 Oct 2021 15:22:01 GMT",
"elapsed": 1,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {
"Accept": "application/json",
"Authorization": "Basic YWRtaW46SGFyYm9yMTIzNDU="
},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 30,
"unix_socket": null,
"unsafe_writes": null,
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/projects",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": false
}
},
"json": [
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:28.228Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 56,
"items": [],
"project_id": 75,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "54"
},
"name": "harbor-esl-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 75,
"registry_id": 101,
"repo_count": 0,
"update_time": "2021-10-21T07:35:28.228Z"
}
],
"msg": "OK (unknown bytes)",
"redirected": false,
"set_cookie": "sid=033af4ae913d69a8314a4edb84512aa6; Path=/; HttpOnly",
"status": 200,
"transfer_encoding": "chunked",
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/projects",
"vary": "Accept-Encoding",
"x_request_id": "c1d907c3-e324-49a5-a582-82f9672d1f8d",
"x_total_count": "8"
}
TASK [../roles/harbor-retention-rules : creating retention rules] *******************************************************************************************
task path: /home/user1/repos/harbor/konvoy/ansible/roles/harbor-retention-rules/tasks/main.yml:36
<harbor.tst1e.k8s.shiplab.ss.acl.com> ESTABLISH LOCAL CONNECTION FOR USER: MEA+user1
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo '"'"'~MEA+user1'"'"' && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702 `" && echo ansible-tmp-1634916125.1009495-142461137289702="` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702 `" ) && sleep 0'
Using module file /usr/lib/python3.7/site-packages/ansible/modules/net_tools/basics/uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> PUT /home/user1/.ansible/tmp/ansible-local-1736l28mza02/tmpy38u0lww TO /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/AnsiballZ_uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'chmod u+x /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/ /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '/usr/bin/python /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'rm -f -r /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/ > /dev/null 2>&1 && sleep 0'
failed: [harbor_shiplab_test1e] (item={'chart_count': 0, 'creation_time': '2021-10-21T07:35:28.228Z', 'current_user_role_id': 1, 'current_user_role_ids': [1], 'cve_allowlist': {'creation_time': '0001-01-01T00:00:00.000Z', 'id': 56, 'items': [], 'project_id': 75, 'update_time': '0001-01-01T00:00:00.000Z'}, 'metadata': {'public': 'true', 'retention_id': '54'}, 'name': 'harbor-esl-proxy', 'owner_id': 1, 'owner_name': 'admin', 'project_id': 75, 'registry_id': 101, 'repo_count': 0, 'update_time': '2021-10-21T07:35:28.228Z'}) => {
"ansible_loop_var": "item",
"changed": false,
"connection": "close",
"content": "{\"errors\":[{\"code\":\"UNPROCESSABLE_ENTITY\",\"message\":\"validation failure list:\nparsing policy body from \\"\\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64\"}]}\n",
"content_length": "213",
"content_type": "application/json; charset=utf-8",
"date": "Fri, 22 Oct 2021 15:22:17 GMT",
"elapsed": 3,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": {
"algorithm": "or",
"id": "54",
"rules": [
{
"action": "retain",
"params": {
"latestPushedK": 3
},
"scope_selectors": {
"repository": [
{
"decoration": "repoMatches",
"kind": "doublestar",
"pattern": "**"
}
]
},
"tag_selectors": [
{
"decoration": "matches",
"extras": "{\"untagged\":true}",
"kind": "doublestar",
"pattern": "**"
}
],
"template": "latestPushedK"
}
],
"scope": {
"level": "project",
"ref": "75"
},
"trigger": {
"kind": "Schedule",
"settings": {
"cron": "0 0 * * * *"
}
}
},
"body_format": "json",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {
"Accept": "application/json, text/plain, */*",
"Authorization": "Basic YWRtaW46SGFyYm9yMTIzNDU=",
"Content-Type": "application/json"
},
"http_agent": "ansible-httpget",
"method": "PUT",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
"200"
],
"timeout": 30,
"unix_socket": null,
"unsafe_writes": null,
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/retentions/54",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": false
}
},
"item": {
"chart_count": 0,
"creation_time": "2021-10-21T07:35:28.228Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 56,
"items": [],
"project_id": 75,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "54"
},
"name": "harbor-esl-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 75,
"registry_id": 101,
"repo_count": 0,
"update_time": "2021-10-21T07:35:28.228Z"
},
"json": {
"errors": [
{
"code": "UNPROCESSABLE_ENTITY",
"message": "validation failure list:\nparsing policy body from \"\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64"
}
]
},
"msg": "Status code was 422 and not [200]: HTTP Error 422: Unprocessable Entity",
"redirected": false,
"set_cookie": "sid=5ffe84980b5cb188e7821ae838e4fc33; Path=/; HttpOnly",
"status": 422,
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/retentions/54",
"vary": "Accept-Encoding",
"x_request_id": "d7a855cf-3de6-47ab-8b47-d275cda5274b"
}
"skip_reason": "Conditional result was False"
}
...ignoring
META: ran handlers
META: ran handlers
PLAY RECAP **************************************************************************************************************************************************
harbor_shiplab_test1e : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
哇,多么迷人的兔子洞:-(
因此,an existing issue 报告了该行为,但它已关闭“按设计工作”,以及一些关于以某种不兼容的方式配置 jinja2 的废话
我从 运行 的 一堆 实验中可以看出,手动制作 JSON 有效载荷是唯一安全的方法:
body_format: json
body: >-
{{
(
{
"algorithm": "or",
"id": item.metadata.retention_id | int,
"rules": [
{
"action": "retain",
"params": {
"latestPushedK": 3
},
"scope_selectors": {
"repository": [
{
"decoration": "repoMatches",
"kind": "doublestar",
"pattern": "**"
}
]
},
"tag_selectors": [
{
"decoration": "matches",
"kind": "doublestar",
"pattern": "**"
}
],
"template": "latestPushedK"
}
],
"scope": {
"level": "project",
"ref": item.project_id | int
},
"trigger": {
"kind": "Schedule",
"settings": {
"cron": "0 0 * * * *"
}
}
}
) | to_json }}
when: "item.registry_id is defined"
我希望我对发生的事情有更好的解释,但考虑到对这个问题的回应,我不确定他们是否认为你的经历是一个错误
但是为了清楚起见,这里发生的是 ()
中的那些前导大括号字符是 python dict 文字,这就是为什么 "id": item.metadata.retention_id | int,
不需要任何特殊考虑——两边都是 python(错误,jinja2,但相同)
()
可能不是绝对必要的,但要让 | to_json
过滤器
中输入的内容变得非常明显
然后,外部 {{
是您习惯的 jinja2 分隔符,以便拥有所有内部 jinja2 代码 运行,body: >-
是为了获得我们摆脱了 yaml 引用地狱
在 ansible 中,我是 运行 为某些项目创建保留规则的角色。使用 PUT 方法我将结果存储在寄存器模块中,从那里我试图读取 retention_id: "59"
和 project_id: "80"
我需要它作为整数,如 59 and 80
,但它正在作为字段的字符串但是我需要它作为整数。我试图通过使用 "{{ item.project_id | int }}"
来改变它
和 {{ item.metadata.retention_id | int}}
它没有更改为整数,为了运气,我尝试将 yaml 正文转换为 JSON。我仍然收到错误。
一周以来我就被这个任务打动了。请提供任何解决方案来解决这个问题。
您的回复将不胜感激。谢谢
实际剧本:
---
# List of projects to check retention and project ID's
- name: get projects
uri:
url: "https://{{ ansible_host }}/api/v2.0/projects"
method: GET
headers:
Authorization: "{{ admin_passwd }}"
Accept: application/json
validate_certs: no
register: projects_result
- debug:
var: projects_result.json | flatten(levels=1)
# Create retention rules for the projects
- name: creating retention rules
uri:
url: "https://{{ ansible_host }}/api/v2.0/retentions/{{ item.metadata.retention_id | int}}"
method: PUT
body_format: json
headers:
Content-Type: application/json
Authorization: "{{ admin_passwd }}"
Accept: 'application/json, text/plain, */*'
body:
algorithm: "or"
id: "{{ item.metadata.retention_id | int }}"
rules:
- action: "retain"
params:
latestPushedK: 3
scope_selectors:
repository:
- decoration: "repoMatches"
kind: "doublestar"
pattern: "**"
tag_selectors:
- decoration: "matches"
extras: "{\"untagged\":true}"
kind: "doublestar"
pattern: "**"
template: latestPushedK
scope:
level: "project"
ref: "{{ item.project_id | int }}"
trigger:
kind: "Schedule"
settings:
cron: "0 0 * * * *"
validate_certs: no
status_code: 200
when: "item.registry_id is defined"
loop: "{{ projects_result.json | flatten(levels=1) }}"
ignore_errors: yes
有了这个我得到了这个错误:
$ ansible-playbook -i ansible/inventories/shiplab ansible/playbooks/harbor-configurations.yaml --ask-vault-pass
Vault password:
PLAY [harbor] ***********************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [harbor_shiplab_test1e]
TASK [../roles/harbor-retention-rules : get projects] *******************************************************************************************************
ok: [harbor_shiplab_test1e]
TASK [../roles/harbor-retention-rules : debug] **************************************************************************************************************
ok: [harbor_shiplab_test1e] => {
"projects_result.json | flatten(levels=1)": [
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:28.228Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 56,
"items": [],
"project_id": 75,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "54"
},
"name": "harbor-esl-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 75,
"registry_id": 101,
"repo_count": 0,
"update_time": "2021-10-21T07:35:28.228Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:43.098Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 58,
"items": [],
"project_id": 77,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "56"
},
"name": "harbor-library-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 77,
"registry_id": 103,
"repo_count": 0,
"update_time": "2021-10-21T07:35:43.098Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:13.930Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 54,
"items": [],
"project_id": 73,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "52"
},
"name": "harbor-megatron-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 73,
"registry_id": 104,
"repo_count": 0,
"update_time": "2021-10-21T07:35:13.930Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:50.271Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 59,
"items": [],
"project_id": 78,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "57"
},
"name": "harbor-migration-poc-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 78,
"registry_id": 105,
"repo_count": 0,
"update_time": "2021-10-21T07:35:50.271Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:57.671Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 60,
"items": [],
"project_id": 79,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "58"
},
"name": "harbor-payment-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 79,
"registry_id": 106,
"repo_count": 0,
"update_time": "2021-10-21T07:35:57.671Z"
},
{
"chart_count": 0,
"creation_time": "2021-10-21T07:36:05.861Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 61,
"items": [],
"project_id": 80,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "59"
},
"name": "harbor-platform-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 80,
"registry_id": 107,
"repo_count": 0,
"update_time": "2021-10-21T07:36:05.861Z"
},
{
"chart_count": 1,
"creation_time": "2021-08-19T15:50:23.772Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 1,
"items": [],
"project_id": 1,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true"
},
"name": "library",
"owner_id": 1,
"owner_name": "admin",
"project_id": 1,
"repo_count": 3,
"update_time": "2021-08-19T15:50:23.772Z"
},
{
"chart_count": 2,
"creation_time": "2021-08-24T16:45:19.230Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 2,
"items": [],
"project_id": 2,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true"
},
"name": "platform",
"owner_id": 1,
"owner_name": "admin",
"project_id": 2,
"repo_count": 11,
"update_time": "2021-08-24T16:45:19.230Z"
}
]
}
TASK [../roles/harbor-retention-rules : creating retention rules] *******************************************************************************************
failed: [harbor_shiplab_test1e] (item={'chart_count': 0, 'creation_time': '2021-10-21T07:35:28.228Z',
'current_user_role_id': 1, 'current_user_role_ids': [1], 'cve_allowlist': {'creation_time': '0001-01-01T00:00:00.000Z',
'id': 56, 'items': [], 'project_id': 75, 'update_time': '0001-01-01T00:00:00.000Z'},
'metadata': {'public': 'true', 'retention_id': '54'}, 'name': 'harbor-esl-proxy', 'owner_id': 1, 'owner_name': 'admin', 'project_id': 75,
'registry_id': 101, 'repo_count': 0, 'update_time': '2021-10-21T07:35:28.228Z'}) => {"ansible_loop_var": "item", "changed": false,
"connection": "close", "content": "{\"errors\":[{\"code\":\"UNPROCESSABLE_ENTITY\",\"message\":\"validation failure
list:\nparsing policy body from \\"\\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64\"}]}\n",
"content_length": "213", "content_type": "application/json; charset=utf-8", "date": "Fri, 22 Oct 2021 12:26:56 GMT", "elapsed": 1,
"item": {"chart_count": 0, "creation_time": "2021-10-21T07:35:28.228Z", "current_user_role_id": 1, "current_user_role_ids": [1],
"cve_allowlist": {"creation_time": "0001-01-01T00:00:00.000Z", "id": 56, "items": [], "project_id": 75, "update_time": "0001-01-01T00:00:00.000Z"},
"metadata": {"public": "true", "retention_id": "54"}, "name": "harbor-esl-proxy", "owner_id": 1, "owner_name": "admin", "project_id": 75,
"registry_id": 101, "repo_count": 0, "update_time": "2021-10-21T07:35:28.228Z"}, "json": {"errors": [{"code": "UNPROCESSABLE_ENTITY",
"message": "validation failure list:\nparsing policy body from \"\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64"}]},
"msg": "Status code was 422 and not [200]: HTTP Error 422: Unprocessable Entity", "redirected": false, "set_cookie": "sid=2e5f911867c675528b5e23c74f803240; Path=/; HttpOnly",
"status": 422, "url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/retentions/54", "vary": "Accept-Encoding", "x_request_id": "334cb06f-c161-4085-8df2-a448b7776819"}
之后我将正文更改为 json :
json 转换:
- name: creating retention rules
uri:
url: "https://{{ ansible_host }}/api/v2.0/retentions/{{ item.metadata.retention_id | int }}"
method: PUT
body_format: json
headers:
Content-Type: application/json
Authorization: "{{ admin_passwd }}"
Accept: 'application/json, text/plain, */*'
body:
'{ "algorithm": "or",
"id": {{ item.metadata.retention_id | int }},
"rules": [{
"action": "retain",
"params": {
"latestPushedK": 3 },
"scope_selectors":{
"repository": [{
"decoration": "repoMatches",
"kind": "doublestar",
"pattern": "**" } ] },
"tag_selectors": [{
"decoration": "matches",
"extras": "{\"untagged\":true}",
"kind": "doublestar",
"pattern": "**" }],
"template": latestPushedK } ],
"scope": {
"level": "project",
"ref": {{ item.project_id | int }} },
"trigger": {
"kind": "Schedule",
"settings":{
"cron": "0 0 * * * *" }}}'
validate_certs: no
status_code: 200
when: "item.registry_id is defined"
loop: "{{ projects_result.json | flatten(levels=1) }}"
ignore_errors: yes
我也遇到了同样的错误。请帮我解决这个问题。
详细信息:
ansible-playbook -i ansible/inventories/shiplab ansible/playbooks/harbor-configurations.yaml --ask-vault-pass -vvv
ansible-playbook -i ansible/inventories/shiplab ansible/playbooks/harbor-configurations.yaml --ask-vault-pass -vvv
ansible-playbook 2.8.4
config file = /home/user1/repos/harbor/konvoy/ansible.cfg
configured module search path = ['/home/user1/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.7.10 (default, May 5 2021, 11:43:58) [GCC 10.2.0]
Using /home/user1/repos/harbor/konvoy/ansible.cfg as config file
Vault password:
host_list declined parsing /home/user1/repos/harbor/konvoy/ansible/inventories/shiplab/test1e as it did not pass it's verify_file() method
auto declined parsing /home/user1/repos/harbor/konvoy/ansible/inventories/shiplab/test1e as it did not pass it's verify_file() method
Parsed /home/user1/repos/harbor/konvoy/ansible/inventories/shiplab/test1e inventory source with ini plugin
PLAYBOOK: harbor-configurations.yaml ************************************************************************************************************************
1 plays in ansible/playbooks/harbor-configurations.yaml
PLAY [harbor] ***********************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
task path: /home/user1/repos/harbor/konvoy/ansible/playbooks/harbor-configurations.yaml:2
<harbor.tst1e.k8s.shiplab.ss.acl.com> ESTABLISH LOCAL CONNECTION FOR USER: MEA+user1
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo '"'"'~MEA+user1'"'"' && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129 `" && echo ansible-tmp-1634916091.044559-272844769197129="` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129 `" ) && sleep 0'
<harbor_shiplab_test1e> Attempting python interpreter discovery
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<harbor_shiplab_test1e> Python interpreter discovery fallback (unsupported platform for extended discovery: cygwin_nt-10.0)
Using module file /usr/lib/python3.7/site-packages/ansible/modules/system/setup.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> PUT /home/user1/.ansible/tmp/ansible-local-1736l28mza02/tmp5ucaxth_ TO /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/AnsiballZ_setup.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'chmod u+x /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/ /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/AnsiballZ_setup.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '/usr/bin/python /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/AnsiballZ_setup.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'rm -f -r /home/user1/.ansible/tmp/ansible-tmp-1634916091.044559-272844769197129/ > /dev/null 2>&1 && sleep 0'
ok: [harbor_shiplab_test1e]
META: ran handlers
TASK [../roles/harbor-retention-rules : get projects] *******************************************************************************************************
task path: /home/user1/repos/harbor/konvoy/ansible/roles/harbor-retention-rules/tasks/main.yml:3
<harbor.tst1e.k8s.shiplab.ss.acl.com> ESTABLISH LOCAL CONNECTION FOR USER: MEA+user1
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo '"'"'~MEA+user1'"'"' && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630 `" && echo ansible-tmp-1634916112.1313658-277975019551630="` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630 `" ) && sleep 0'
Using module file /usr/lib/python3.7/site-packages/ansible/modules/net_tools/basics/uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> PUT /home/user1/.ansible/tmp/ansible-local-1736l28mza02/tmpsm6sxu8_ TO /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/AnsiballZ_uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'chmod u+x /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/ /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '/usr/bin/python /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'rm -f -r /home/user1/.ansible/tmp/ansible-tmp-1634916112.1313658-277975019551630/ > /dev/null 2>&1 && sleep 0'
ok: [harbor_shiplab_test1e] => {
"changed": false,
"connection": "close",
"content_type": "application/json",
"cookies": {
"sid": "033af4ae913d69a8314a4edb84512aa6"
},
"cookies_string": "sid=033af4ae913d69a8314a4edb84512aa6",
"date": "Fri, 22 Oct 2021 15:22:01 GMT",
"elapsed": 1,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {
"Accept": "application/json",
"Authorization": "Basic YWRtaW46SGFyYm9yMTIzNDU="
},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 30,
"unix_socket": null,
"unsafe_writes": null,
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/projects",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": false
}
},
"json": [
{
"chart_count": 0,
"creation_time": "2021-10-21T07:35:28.228Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 56,
"items": [],
"project_id": 75,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "54"
},
"name": "harbor-esl-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 75,
"registry_id": 101,
"repo_count": 0,
"update_time": "2021-10-21T07:35:28.228Z"
}
],
"msg": "OK (unknown bytes)",
"redirected": false,
"set_cookie": "sid=033af4ae913d69a8314a4edb84512aa6; Path=/; HttpOnly",
"status": 200,
"transfer_encoding": "chunked",
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/projects",
"vary": "Accept-Encoding",
"x_request_id": "c1d907c3-e324-49a5-a582-82f9672d1f8d",
"x_total_count": "8"
}
TASK [../roles/harbor-retention-rules : creating retention rules] *******************************************************************************************
task path: /home/user1/repos/harbor/konvoy/ansible/roles/harbor-retention-rules/tasks/main.yml:36
<harbor.tst1e.k8s.shiplab.ss.acl.com> ESTABLISH LOCAL CONNECTION FOR USER: MEA+user1
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'echo '"'"'~MEA+user1'"'"' && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702 `" && echo ansible-tmp-1634916125.1009495-142461137289702="` echo ~MEA+user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702 `" ) && sleep 0'
Using module file /usr/lib/python3.7/site-packages/ansible/modules/net_tools/basics/uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> PUT /home/user1/.ansible/tmp/ansible-local-1736l28mza02/tmpy38u0lww TO /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/AnsiballZ_uri.py
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'chmod u+x /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/ /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c '/usr/bin/python /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/AnsiballZ_uri.py && sleep 0'
<harbor.tst1e.k8s.shiplab.ss.acl.com> EXEC /bin/sh -c 'rm -f -r /home/user1/.ansible/tmp/ansible-tmp-1634916125.1009495-142461137289702/ > /dev/null 2>&1 && sleep 0'
failed: [harbor_shiplab_test1e] (item={'chart_count': 0, 'creation_time': '2021-10-21T07:35:28.228Z', 'current_user_role_id': 1, 'current_user_role_ids': [1], 'cve_allowlist': {'creation_time': '0001-01-01T00:00:00.000Z', 'id': 56, 'items': [], 'project_id': 75, 'update_time': '0001-01-01T00:00:00.000Z'}, 'metadata': {'public': 'true', 'retention_id': '54'}, 'name': 'harbor-esl-proxy', 'owner_id': 1, 'owner_name': 'admin', 'project_id': 75, 'registry_id': 101, 'repo_count': 0, 'update_time': '2021-10-21T07:35:28.228Z'}) => {
"ansible_loop_var": "item",
"changed": false,
"connection": "close",
"content": "{\"errors\":[{\"code\":\"UNPROCESSABLE_ENTITY\",\"message\":\"validation failure list:\nparsing policy body from \\"\\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64\"}]}\n",
"content_length": "213",
"content_type": "application/json; charset=utf-8",
"date": "Fri, 22 Oct 2021 15:22:17 GMT",
"elapsed": 3,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": {
"algorithm": "or",
"id": "54",
"rules": [
{
"action": "retain",
"params": {
"latestPushedK": 3
},
"scope_selectors": {
"repository": [
{
"decoration": "repoMatches",
"kind": "doublestar",
"pattern": "**"
}
]
},
"tag_selectors": [
{
"decoration": "matches",
"extras": "{\"untagged\":true}",
"kind": "doublestar",
"pattern": "**"
}
],
"template": "latestPushedK"
}
],
"scope": {
"level": "project",
"ref": "75"
},
"trigger": {
"kind": "Schedule",
"settings": {
"cron": "0 0 * * * *"
}
}
},
"body_format": "json",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {
"Accept": "application/json, text/plain, */*",
"Authorization": "Basic YWRtaW46SGFyYm9yMTIzNDU=",
"Content-Type": "application/json"
},
"http_agent": "ansible-httpget",
"method": "PUT",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
"200"
],
"timeout": 30,
"unix_socket": null,
"unsafe_writes": null,
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/retentions/54",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": false
}
},
"item": {
"chart_count": 0,
"creation_time": "2021-10-21T07:35:28.228Z",
"current_user_role_id": 1,
"current_user_role_ids": [
1
],
"cve_allowlist": {
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 56,
"items": [],
"project_id": 75,
"update_time": "0001-01-01T00:00:00.000Z"
},
"metadata": {
"public": "true",
"retention_id": "54"
},
"name": "harbor-esl-proxy",
"owner_id": 1,
"owner_name": "admin",
"project_id": 75,
"registry_id": 101,
"repo_count": 0,
"update_time": "2021-10-21T07:35:28.228Z"
},
"json": {
"errors": [
{
"code": "UNPROCESSABLE_ENTITY",
"message": "validation failure list:\nparsing policy body from \"\" failed, because json: cannot unmarshal string into Go struct field RetentionPolicy.id of type int64"
}
]
},
"msg": "Status code was 422 and not [200]: HTTP Error 422: Unprocessable Entity",
"redirected": false,
"set_cookie": "sid=5ffe84980b5cb188e7821ae838e4fc33; Path=/; HttpOnly",
"status": 422,
"url": "https://harbor.tst1e.k8s.shiplab.ss.acl.com/api/v2.0/retentions/54",
"vary": "Accept-Encoding",
"x_request_id": "d7a855cf-3de6-47ab-8b47-d275cda5274b"
}
"skip_reason": "Conditional result was False"
}
...ignoring
META: ran handlers
META: ran handlers
PLAY RECAP **************************************************************************************************************************************************
harbor_shiplab_test1e : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
哇,多么迷人的兔子洞:-(
因此,an existing issue 报告了该行为,但它已关闭“按设计工作”,以及一些关于以某种不兼容的方式配置 jinja2 的废话
我从 运行 的 一堆 实验中可以看出,手动制作 JSON 有效载荷是唯一安全的方法:
body_format: json
body: >-
{{
(
{
"algorithm": "or",
"id": item.metadata.retention_id | int,
"rules": [
{
"action": "retain",
"params": {
"latestPushedK": 3
},
"scope_selectors": {
"repository": [
{
"decoration": "repoMatches",
"kind": "doublestar",
"pattern": "**"
}
]
},
"tag_selectors": [
{
"decoration": "matches",
"kind": "doublestar",
"pattern": "**"
}
],
"template": "latestPushedK"
}
],
"scope": {
"level": "project",
"ref": item.project_id | int
},
"trigger": {
"kind": "Schedule",
"settings": {
"cron": "0 0 * * * *"
}
}
}
) | to_json }}
when: "item.registry_id is defined"
我希望我对发生的事情有更好的解释,但考虑到对这个问题的回应,我不确定他们是否认为你的经历是一个错误
但是为了清楚起见,这里发生的是 ()
中的那些前导大括号字符是 python dict 文字,这就是为什么 "id": item.metadata.retention_id | int,
不需要任何特殊考虑——两边都是 python(错误,jinja2,但相同)
()
可能不是绝对必要的,但要让 | to_json
过滤器
然后,外部 {{
是您习惯的 jinja2 分隔符,以便拥有所有内部 jinja2 代码 运行,body: >-
是为了获得我们摆脱了 yaml 引用地狱