我们如何在Ansible剧本中使用when条件来检查Artifactory Repository是否存在,它是否存在,更新Repository配置,如果没有创建Repository。对于所有的人,创建和更新我使用的REST。
在这里,我使用来自JFrog Artifactory的REST,我不想编写任何Python或Shell脚本。我需要通过使用条件词来完成Ansible的所有操作。
我试过:
- name: Update Repository Configuration
uri:
url: (url)/api/repositories/docker-remote
method: POST
headers:
Content-Type: application/json
body_format: json
body: "{{ lookup('file','/docker-remote.json') }}"
follow_redirects: all
url_username: "user"
url_password: "pass"
force_basic_auth: yes
status_code: 200
return_content: yes
delegate_to: localhost
when: status_code == 200
register: result
- name: Create Repository
uri:
url: (url)/api/repositories/docker-remote
method: PUT
headers:
Content-Type: application/json
body_format: json
body: "{{ lookup('file','docker-remote.json') }}"
follow_redirects: all
url_username: "user"
url_password: "pass"
force_basic_auth: yes
status_code: 200
return_content: yes
delegate_to: localhost
register: result
when: status_code == 400发布于 2022-10-25 13:32:55
你几乎实现了你的目标。由于Ansible是配置管理的工具,因此只需确保配置了所需的状态即可。
..。检查是否存在Artifactory Repository,如果它存在,更新Repository配置,如果没有创建Repository .
换句话说,没有必要在检查之前或做类似的事情,如果是那样的话。只需要声明最后的状态应该是什么样子。基于您已经给出的示例,您可以继续进行以下工作..。
JSON表示中的一个示例存储库配置集,TEST.json
{
"key" : "TEST",
"rclass" : "local",
"repoLayoutRef": "simple-default",
"description" : "Local generic test repository",
"url" : "https://<yourRepositoryURL>/artifactory/TEST",
"packageType": "Generic"
}和一个示例测试剧本create_repository.yml
---
- hosts: localhost
become: no
gather_facts: no
vars:
API_URL: "<yourRepositoryURL>/artifactory/api"
REST_API_ENDPOINT: "repositories" # since there are also access and security ...
RKEY: "TEST" # aka REPOSITORY_KEY
tasks:
- name: Read in repository configuration
include_vars:
file: "{{ RKEY }}.json"
name: CONFIG
when: not ansible_check_mode
- name: Make sure repository is created
local_action:
module: uri
url: "https://{{ API_URL }}/{{ REST_API_ENDPOINT }}/{{ RKEY }}"
method: PUT
url_username: "{{ ansible_user }}"
url_password: "{{ ansible_password }}"
validate_certs: yes
body_format: json
body: "{{ CONFIG }}"
status_code: 200,400
register: PUT
failed_when: false
when: not ansible_check_mode
- name: Make sure repository is in desired state
local_action:
module: uri
url: "https://{{ API_URL }}/{{ REST_API_ENDPOINT }}/{{ RKEY }}"
method: POST
url_username: "{{ ansible_user }}"
url_password: "{{ ansible_password }}"
validate_certs: yes
body_format: json
body: "{{ CONFIG }}"
status_code: 200
when: not ansible_check_mode and PUT.status == 400
- name: Gather current repository configuration, if there is one
local_action:
module: uri
url: "https://{{ API_URL }}/{{ REST_API_ENDPOINT }}/{{ RKEY }}"
method: GET
url_username: "{{ ansible_user }}"
url_password: "{{ ansible_password }}"
validate_certs: yes
return_content: yes
status_code: 200,400
body_format: json
register: GET
check_mode: false
- name: Show repository configuration, if there is one
debug:
msg: "{{ GET.json }}"
check_mode: false打电话
sshpass -p ${PASSWORD} ansible-playbook --user ${ACCOUNT} --ask-pass create_repository.yml --check
sshpass -p ${PASSWORD} ansible-playbook --user ${ACCOUNT} --ask-pass create_repository.yml如果给定的存储库配置在Artifactory中不可用,那么第一个REST调用将导致创建它和200的状态代码。在此之后,就不需要更新了。因此,when: PUT.status == 400.
如果给定的存储库配置已在Artifactory中可用,则第一个REST调用将生成400的状态代码,然后不将其视为错误。之后,另一个REST调用确保配置处于所需状态。
https://stackoverflow.com/questions/74163877
复制相似问题