我一直在使用这个示例playbook来使用Ansible创建机架空间服务器
http://nicholaskuechler.com/2015/01/09/build-rackspace-cloud-servers-ansible-virtualenv/
哪个好用,但一次只能在一台服务器上运行,所以我试图让它更具动态性,使用with_items循环遍历我想要构建的服务器数量
tasks:
- name: Rackspace cloud server build request
local_action:
module: rax
credentials: "{{ credentials }}"
name: "{{ item }}"
flavor: "{{ flavor }}"
image: "{{ image }}"
region: "{{ region }}"
files: "{{ files }}"
wait: yes
state: present
networks:
- private
- public
with_items:
- server-app-01
- server-app-02
register: rax
这样可以很好地创建服务器,但是当我尝试使用链接中的方法将其添加到部署组时,我收到错误,正如预期的那样,现在有一个“结果”键我已经尝试了各种各样的尝试以我认为文档提及的方式来定位的方法:
- name: Add new cloud server to host group
local_action:
module: add_host
hostname: "{{ item.success.name }}"
ansible_ssh_host: "{{ item.success.rax_accessipv4 }}"
ansible_ssh_user: root
groupname: deploy
with_items: rax.results
(我也尝试了许多其他方法来定位) 但我得到'一个或多个未定义的变量:'list object'没有属性'rax_accessipv4“
这是我从rax通过调试返回的对象的精简版本。这些服务器不再存在。 http://pastebin.com/NRvM7anS
任何人都可以告诉我我哪里出错了我开始有点生气了
答案 0 :(得分:2)
如果您发现rax.results.success
的类型为list
。
所以:hostname: "{{ item.success.name }}"
应该是
hostname: "{{ item.success[0].name }}"
或hostname: "{{ item['success'][0]['name'] }}"
{
"changed": true,
"msg": "All items completed",
"results": [
{
"instances": [
{
"name": "server-app-01",
"rax_accessipv4": "134.213.51.171",
"rax_accessipv6": "2a00:1a48:7808:101:be76:4eff:fe08:5251",
}
],
"item": "server-app-01",
"success": [
{
"name": "server-app-01",
"rax_accessipv4": "134.213.51.171",
"rax_accessipv6": "2a00:1a48:7808:101:be76:4eff:fe08:5251",
}
],
"timeout": []
},
......
}
答案 1 :(得分:0)
我本周五只是在摔跤。这是我的解决方案:
---
- name: Provision rackspace webheads
hosts: localhost
gather_facts: false
max_fail_percentage: 10
tasks:
- name: Provision a set of instances
local_action:
group: servers
count: 5
exact_count: yes
credentials: cred.ini
flavor: <FLAVOR ID>
group: raxhosts
image: <IMAGE ID>
key_name: <SSH KEYNAME>
module: rax
name: webheads
state: present
wait: yes
register: rax
- name: Add new instances to the group 'raxhosts'
local_action:
module: add_host
hostname: "{{ item.name }}"
ansible_ssh_host: "{{ item.rax_accessipv4 }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
groupname: raxhosts
with_items: rax.success
when: rax.action == 'create'
- name: Wait for hosts
local_action: wait_for host={{ item.rax_accessipv4 }} port=22 delay=60 timeout=600 state=started
with_items: rax.success
这是我的cred.ini的样子:
[rackspace_cloud]
username =
api_key =
像这样运行:
RAX_CREDS_FILE=cred.ini RAX_REGION=DFW ansible-playbook <playbook>.yml