无法在循环中访问寄存器变量
Can't access register variable in a loop
我一直在按照这个示例手册使用 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
这可以很好地创建服务器,但是当我尝试使用 link 中的方法将其添加到部署组时,出现错误,正如预期的那样,现在有一个 'results'关键我已经尝试了各种方法来尝试以我认为文档暗示的方式来定位它:
- 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
(我也尝试过很多其他方法来解决这个问题)
但是我得到“一个或多个未定义的变量:‘列表对象’没有属性‘rax_accessipv4”
这是我通过调试从 rax 返回的对象的精简版本。这些服务器不再存在。
http://pastebin.com/NRvM7anS
谁能告诉我哪里出错了我开始有点生气
我刚刚在这个星期五挣扎。这是我的解决方案:
---
- 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
如果您注意到 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": []
},
......
}
我一直在按照这个示例手册使用 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
这可以很好地创建服务器,但是当我尝试使用 link 中的方法将其添加到部署组时,出现错误,正如预期的那样,现在有一个 'results'关键我已经尝试了各种方法来尝试以我认为文档暗示的方式来定位它:
- 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
(我也尝试过很多其他方法来解决这个问题) 但是我得到“一个或多个未定义的变量:‘列表对象’没有属性‘rax_accessipv4”
这是我通过调试从 rax 返回的对象的精简版本。这些服务器不再存在。 http://pastebin.com/NRvM7anS
谁能告诉我哪里出错了我开始有点生气
我刚刚在这个星期五挣扎。这是我的解决方案:
---
- 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
如果您注意到 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": []
},
......
}