如何将这两个ansible剧本连接起来,这两个剧本解决了一个与JIRA自动化相关的问题,并使用了他的RESTAPI
我试图解决的问题是使用JIRARESTAPI向JIRA添加用户。我想在createUser.yml文件中替换的行如下如何将这两个ansible剧本连接起来,这两个剧本解决了一个与JIRA自动化相关的问题,并使用了他的RESTAPI,ansible,yaml,jira,Ansible,Yaml,Jira,我试图解决的问题是使用JIRARESTAPI向JIRA添加用户。我想在createUser.yml文件中替换的行如下 loop: "{{ users }}") 我想用描述为JSON文件的用户形成的列表来代替它(继续阅读关于如何实现用户和列表的详细信息) 我在这里给你发邮件 createUser.yml文件 - hosts: localhost gather_facts: false vars_files: - vars.yml tasks: - name: Cr
loop: "{{ users }}")
我想用描述为JSON文件的用户形成的列表来代替它(继续阅读关于如何实现用户和列表的详细信息)
我在这里给你发邮件
createUser.yml文件
- hosts: localhost
gather_facts: false
vars_files:
- vars.yml
tasks:
- name: Create Jira Project
uri:
url: "{{ jira_url }}/rest/api/2/project"
method: POST
user: "{{ jira_username }}"
password: "{{ jira_password }}"
return_content: yes
force_basic_auth: yes
body_format: json
headers:
Accept: 'application/json'
Content-Type: 'application/json'
body: "{ 'key': '{{ app_code + kapp }}',
'name': '{{ project_name }}',
'projectTypeKey': 'software',
'projectTemplateKey': 'com.example.plugins.tutorial.my-project-template:my-project-template',
'description': 'Project created using Ansible playbook',
'lead': '{{ project_lead }}',
'assigneeType': 'PROJECT_LEAD',
}"
status_code: 201
register: result
tags:
- project
- name: Project Creation Details
debug:
msg: "{{ result.json }}"
- name: Add user
uri:
url: "{{ jira_url }}/rest/api/2/user?groupname=jira-software-users"
method: POST
user: "{{ jira_username }}"
password: "{{ jira_password }}"
return_content: yes
force_basic_auth: yes
body_format: json
headers:
Accept: 'application/json'
Content-Type: 'application/json'
body: '{ "name": "{{ item }}" }'
register: userResult
loop: "{{ users }}"
tags:
- adduser
- name: User details
debug:
msg: "{{ userResult.json }}"
这是实现算法背后的主要思想,基本上是拥有一个JSON用户列表,并循环该列表,并使用JIRA API迭代添加每个用户
为了对用户建模,我使用了这个约定,为每个用户创建一个YAML文件。例子:
jsmith.yml
jsmith:
project_role: administrators
full_name: John Smith
email: johnsmith@mail.com
pmorrison:
project_role: developer
full_name: Paul Morrison
email: paulmorrison@mail.com
- hosts: localhost
tasks:
- include_vars:
dir: user.d
name: users
- debug:
var: users
- set_fact:
users_list: "{{ users_list|d([]) +
[{'username': item.0}|combine(item.1)] }}"
with_together:
- "{{ users.keys()|list }}"
- "{{ users.values()|list }}"
- debug:
var: users_list
其他例子:
pmorrison.yml
jsmith:
project_role: administrators
full_name: John Smith
email: johnsmith@mail.com
pmorrison:
project_role: developer
full_name: Paul Morrison
email: paulmorrison@mail.com
- hosts: localhost
tasks:
- include_vars:
dir: user.d
name: users
- debug:
var: users
- set_fact:
users_list: "{{ users_list|d([]) +
[{'username': item.0}|combine(item.1)] }}"
with_together:
- "{{ users.keys()|list }}"
- "{{ users.values()|list }}"
- debug:
var: users_list
我还使用此文件:
playbook.yml
jsmith:
project_role: administrators
full_name: John Smith
email: johnsmith@mail.com
pmorrison:
project_role: developer
full_name: Paul Morrison
email: paulmorrison@mail.com
- hosts: localhost
tasks:
- include_vars:
dir: user.d
name: users
- debug:
var: users
- set_fact:
users_list: "{{ users_list|d([]) +
[{'username': item.0}|combine(item.1)] }}"
with_together:
- "{{ users.keys()|list }}"
- "{{ users.values()|list }}"
- debug:
var: users_list
另一个文件playbook.yml给出了这个结果,即我想要的JSON文件的用户列表:
"users_list": [
{
"email": "johnsmith@mail.com",
"full_name": "John Smith",
"project_role": "administrators",
"username": "jsmith"
},
{
"email": "paulmorrison@mail.com",
"full_name": "Paul Morrison",
"project_role": "developer",
"username": "pmorrison"
}
]
我想解决的主要问题是让playbook.yml中的用户列表循环,而不是createUser.yml中的“用户”列表
jsmith:
project_role: administrators
full_name: John Smith
email: johnsmith@mail.com
pmorrison:
project_role: developer
full_name: Paul Morrison
email: paulmorrison@mail.com
- hosts: localhost
tasks:
- include_vars:
dir: user.d
name: users
- debug:
var: users
- set_fact:
users_list: "{{ users_list|d([]) +
[{'username': item.0}|combine(item.1)] }}"
with_together:
- "{{ users.keys()|list }}"
- "{{ users.values()|list }}"
- debug:
var: users_list
我通过修改createProject.yml文件并在其中插入playbook.yml文件的代码来尝试此解决方案,因此我不再使用playbook.yml文件,而只使用包含其逻辑的createProject.yml文件,但它不起作用:
- hosts: localhost
gather_facts: false
vars_files:
-users.d/pmorrison.yml
-users.d/jsmith.yml
tasks:
- name: Create Jira Project
uri:
url: "{{ jira_url }}/rest/api/2/project"
method: POST
user: "{{ jira_username }}"
password: "{{ jira_password }}"
return_content: yes
force_basic_auth: yes
body_format: json
headers:
Accept: 'application/json'
Content-Type: 'application/json'
body: "{ 'key': '{{ app_code + kapp }}',
'name': '{{ project_name }}',
'projectTypeKey': 'software',
'projectTemplateKey': 'com.example.plugins.tutorial.my-project-template:my-project-template',
'description': 'Project created using Ansible playbook',
'lead': '{{ project_lead }}',
'assigneeType': 'PROJECT_LEAD',
}"
status_code: 201
register: result
tags:
- project
- name: Project Creation Details
debug:
msg: "{{ result.json }}"
- include_vars:
dir: user.d
name: users
- debug:
var: users
- set_fact:
users_list: "{{ users_list|d([]) +
[{'username': item.0}|combine(item.1)] }}"
with_together:
- "{{ users.keys()|list }}"
- "{{ users.values()|list }}"
- debug:
var: users_list
- name: Add user
uri:
url: "{{ jira_url }}/rest/api/2/user?groupname=jira-software-users"
method: POST
user: "{{ jira_username }}"
password: "{{ jira_password }}"
return_content: yes
force_basic_auth: yes
body_format: json
headers:
Accept: 'application/json'
Content-Type: 'application/json'
body: '{ "{{ item }}" }'
register: userResult
给出了错误:
ERROR! vars file -users.d/pmorrison.yml -users.d/jsmith.yml was not found
Could not find file on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option
我怀疑存在输入错误(您能确认一下吗?),您能告诉我,我构建解决问题逻辑的方式是否正确,以及如何着手更好地设计最终解决方案吗?谢谢这是缩进错误。写如下,将解决问题
- users.d/pmorrison.yml