[英]Ansible Dynamic Variables in Playbook
I'm provisioning a bunch of systems (VMs) on a physical host. 我正在物理主机上配置一堆系统(VM)。 I'm up to the step where the VM's are running. 我已经做好了运行VM的步骤。 Now I need to ssh into the VM's via their DHCP addresses. 现在,我需要通过其DHCP地址进入VM。 I can pull the IP addresses from the server but I need a way to set these to host_vars. 我可以从服务器提取IP地址,但是我需要一种将它们设置为host_vars的方法。 Here are my groups: 这是我的小组:
ok: [kvm01] => {
"msg": {
"all": [
"kvm01",
"dcos-master-1",
"dcos-agent-1",
"dcos-agent-2",
"dcos_bootstrap"
],
"dcos": [
"dcos-master-1",
"dcos-agent-1",
"dcos-agent-2",
"dcos_bootstrap"
],
"dcos-agents": [
"dcos-agent-1",
"dcos-agent-2"
],
"dcos-bootstraps": [
"dcos_bootstrap"
],
"dcos-masters": [
"dcos-master-1"
],
"kvm": [
"kvm01"
],
"ungrouped": []
}
}
Here's my command: 这是我的命令:
- name: Get the IP of the VM (DHCP)
command: "/getip.sh {{ item }}"
register: "result"
with_items: "{{ groups['dcos'] }}"
- name: List the output of the variables
debug: msg="{{item.stdout}}"
with_items: "{{result.results}}"
When I run the playbook I get results but they are the FULL JSON result of the command rather than the stdout. 当我运行剧本时,会得到结果,但它们是命令的完整JSON结果,而不是标准输出。 There's probably a way to pull out the stdout and assign it to a fact but it's a complex hash. 可能有一种方法可以提取标准输出并将其分配给事实,但这是一个复杂的哈希。 Here's the last result: 这是最后的结果:
TASK [vm_install : Get the IP of the VM (DHCP)] ***************************************************************************
changed: [kvm01] => (item=dcos-master-1)
changed: [kvm01] => (item=dcos-agent-1)
changed: [kvm01] => (item=dcos-agent-2)
changed: [kvm01] => (item=dcos_bootstrap)
TASK [vm_install : List the output of the variables] **********************************************************************
......
ok: [kvm01] => (item={'_ansible_parsed': True, 'stderr_lines': [u'] => {
"item": {
"changed": false,
"cmd": [
"/getip.sh",
"dcos_bootstrap"
],
"delta": "0:00:00.056193",
"end": "2017-09-18 15:45:45.409693",
"invocation": {
"module_args": {
"_raw_params": "/getip.sh dcos_bootstrap",
"_uses_shell": false,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
}
},
"item": "dcos_bootstrap",
"rc": 0,
"start": "2017-09-18 15:45:45.353500",
"stderr": " ",
"stdout": "192.168.1.130",
"stdout_lines": [
"192.168.1.130"
]
},
"msg": "192.168.1.130"
}
How can I put the output of the command into an array so that I can use it later in my playbook? 如何将命令的输出放入数组中,以便以后在剧本中使用它?
So, like I said in my comment, you've already managed to extract the information you want into an array. 因此,就像我在评论中说的那样,您已经设法将所需的信息提取到数组中。 You can iterate over those items using with_items
as in the follow task that will create an ip_address
for each host: 您可以使用with_items
遍历那些项目,如以下任务所示,它将为每个主机创建一个ip_address
:
- set_fact:
ip_address: "{{ item.stdout }}"
with_items: "{{ results.results }}"
delegate_to: "{{ item.item }}"
delegate_facts: true
Or you can create a single array containing all of the addresses using Jinja filters: 或者,您可以使用Jinja过滤器创建一个包含所有地址的单个数组:
- set_fact:
all_ip_addresses: "{{ results.results|map(attribute='stdout')|list }}"
Or you could create a dictionary with the same information: 或者,您可以创建具有相同信息的字典:
- set_fact:
all_ip_addresses: >
{{ all_ip_addresses
|default({})
|combine({ item.item: item.stdout })}}
with_items: "{{ results.results }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.