使用 ansible 将递增的变量分配给主机列表
Assign incremented variables to list of hosts with ansible
我想要的应该很简单,但我似乎找不到答案。我想 运行 不同输入的数学模拟。我可以访问 40 个服务器的列表(我没有 root 权限)所以我的想法是同时测试 40 个不同的系数集。目前尚不清楚我如何在 ansible 中使用类似 for loop
的语句设置变量并将它们中的每一个分配给不同的服务器。理想情况下,我希望变量列表是类似于 a = numpy.arange(.0,1.,.1)
给我的浮点数。有什么想法吗?
服务器组设置如下:
[computingCluster]
cic[1:40]
让我们创建一个包含两个剧本的剧本:
- 首先
Play
将从每个主机的模板生成主机变量文件。
- 第二个
Play
将在主机上执行实际任务。
目录结构:
/tmp/ansible
├── [host_vars]
├── inventory
├── my_template.yml.j2
└── playbook.yml
库存文件:
[computingCluster]
cic[1:10]
用于加载主机变量 (http://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data) 的模板 my_template.yml.j2
:
coefficient: {{ item.0 }}
有两部剧本的剧本:
---
- hosts: localhost
connection: local
tasks:
- name: Create Variable File for Each host
template: src=my_template.yml.j2 dest=host_vars/{{ item.1 }}
with_indexed_items: "{{ groups['computingCluster'] }}"
- hosts: computingCluster
pre_tasks:
- name: Load Host Specific Generated Variables
include_vars: host_vars/{{ inventory_hostname }}
tasks:
- debug: var=coefficient
这是结果 ansible-playbook -i inventory playbook.yml
:
PLAY [localhost] **************************************************************
TASK: [Create Variable File for Each host] ************************************
ok: [localhost] => (item=(0, 'cic1'))
ok: [localhost] => (item=(1, 'cic2'))
ok: [localhost] => (item=(2, 'cic3'))
ok: [localhost] => (item=(3, 'cic4'))
ok: [localhost] => (item=(4, 'cic5'))
ok: [localhost] => (item=(5, 'cic6'))
ok: [localhost] => (item=(6, 'cic7'))
ok: [localhost] => (item=(7, 'cic8'))
ok: [localhost] => (item=(8, 'cic9'))
ok: [localhost] => (item=(9, 'cic10'))
PLAY [computingCluster] *******************************************************
GATHERING FACTS ***************************************************************
ok: [cic1]
ok: [cic5]
ok: [cic4]
ok: [cic2]
ok: [cic3]
ok: [cic6]
ok: [cic9]
ok: [cic10]
ok: [cic8]
ok: [cic7]
TASK: [debug var=coefficient] *************************************************
ok: [cic1] => {
"var": {
"coefficient": "0"
}
}
ok: [cic2] => {
"var": {
"coefficient": "1"
}
}
ok: [cic3] => {
"var": {
"coefficient": "2"
}
}
ok: [cic4] => {
"var": {
"coefficient": "3"
}
}
ok: [cic6] => {
"var": {
"coefficient": "5"
}
}
ok: [cic5] => {
"var": {
"coefficient": "4"
}
}
ok: [cic7] => {
"var": {
"coefficient": "6"
}
}
ok: [cic8] => {
"var": {
"coefficient": "7"
}
}
ok: [cic9] => {
"var": {
"coefficient": "8"
}
}
ok: [cic10] => {
"var": {
"coefficient": "9"
}
}
PLAY RECAP ********************************************************************
cic1 : ok=2 changed=0 unreachable=0 failed=0
cic10 : ok=2 changed=0 unreachable=0 failed=0
cic2 : ok=2 changed=0 unreachable=0 failed=0
cic3 : ok=2 changed=0 unreachable=0 failed=0
cic4 : ok=2 changed=0 unreachable=0 failed=0
cic5 : ok=2 changed=0 unreachable=0 failed=0
cic6 : ok=2 changed=0 unreachable=0 failed=0
cic7 : ok=2 changed=0 unreachable=0 failed=0
cic8 : ok=2 changed=0 unreachable=0 failed=0
cic9 : ok=2 changed=0 unreachable=0 failed=0
localhost : ok=1 changed=0 unreachable=0 failed=0
您始终可以使用 jinja 模板来使用 coefficient
变量...
参考文献:
me@pc:~/Ansible/playbooks/test_iterate$ ansible-playbook test0.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [srv-db1]
ok: [srv-db-temp]
TASK [debug] *******************************************************************
ok: [srv-db-temp] => {
"msg": "101"
}
ok: [srv-db1] => {
"msg": "102"
}
PLAY RECAP *********************************************************************
srv-db-temp : ok=2 changed=0 unreachable=0 failed=0
srv-db1 : ok=2 changed=0 unreachable=0 failed=0
test0.yml:
hosts: srv-dbs
vars:
- priority_start: 100
tasks:
- debug: msg="{% for thishost in play_hosts %}{% if inventory_hostname==thishost %}{{ priority_start + loop.index }}{% endif %}{% endfor %}"
我想要的应该很简单,但我似乎找不到答案。我想 运行 不同输入的数学模拟。我可以访问 40 个服务器的列表(我没有 root 权限)所以我的想法是同时测试 40 个不同的系数集。目前尚不清楚我如何在 ansible 中使用类似 for loop
的语句设置变量并将它们中的每一个分配给不同的服务器。理想情况下,我希望变量列表是类似于 a = numpy.arange(.0,1.,.1)
给我的浮点数。有什么想法吗?
服务器组设置如下:
[computingCluster]
cic[1:40]
让我们创建一个包含两个剧本的剧本:
- 首先
Play
将从每个主机的模板生成主机变量文件。 - 第二个
Play
将在主机上执行实际任务。
目录结构:
/tmp/ansible
├── [host_vars]
├── inventory
├── my_template.yml.j2
└── playbook.yml
库存文件:
[computingCluster]
cic[1:10]
用于加载主机变量 (http://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data) 的模板 my_template.yml.j2
:
coefficient: {{ item.0 }}
有两部剧本的剧本:
---
- hosts: localhost
connection: local
tasks:
- name: Create Variable File for Each host
template: src=my_template.yml.j2 dest=host_vars/{{ item.1 }}
with_indexed_items: "{{ groups['computingCluster'] }}"
- hosts: computingCluster
pre_tasks:
- name: Load Host Specific Generated Variables
include_vars: host_vars/{{ inventory_hostname }}
tasks:
- debug: var=coefficient
这是结果 ansible-playbook -i inventory playbook.yml
:
PLAY [localhost] **************************************************************
TASK: [Create Variable File for Each host] ************************************
ok: [localhost] => (item=(0, 'cic1'))
ok: [localhost] => (item=(1, 'cic2'))
ok: [localhost] => (item=(2, 'cic3'))
ok: [localhost] => (item=(3, 'cic4'))
ok: [localhost] => (item=(4, 'cic5'))
ok: [localhost] => (item=(5, 'cic6'))
ok: [localhost] => (item=(6, 'cic7'))
ok: [localhost] => (item=(7, 'cic8'))
ok: [localhost] => (item=(8, 'cic9'))
ok: [localhost] => (item=(9, 'cic10'))
PLAY [computingCluster] *******************************************************
GATHERING FACTS ***************************************************************
ok: [cic1]
ok: [cic5]
ok: [cic4]
ok: [cic2]
ok: [cic3]
ok: [cic6]
ok: [cic9]
ok: [cic10]
ok: [cic8]
ok: [cic7]
TASK: [debug var=coefficient] *************************************************
ok: [cic1] => {
"var": {
"coefficient": "0"
}
}
ok: [cic2] => {
"var": {
"coefficient": "1"
}
}
ok: [cic3] => {
"var": {
"coefficient": "2"
}
}
ok: [cic4] => {
"var": {
"coefficient": "3"
}
}
ok: [cic6] => {
"var": {
"coefficient": "5"
}
}
ok: [cic5] => {
"var": {
"coefficient": "4"
}
}
ok: [cic7] => {
"var": {
"coefficient": "6"
}
}
ok: [cic8] => {
"var": {
"coefficient": "7"
}
}
ok: [cic9] => {
"var": {
"coefficient": "8"
}
}
ok: [cic10] => {
"var": {
"coefficient": "9"
}
}
PLAY RECAP ********************************************************************
cic1 : ok=2 changed=0 unreachable=0 failed=0
cic10 : ok=2 changed=0 unreachable=0 failed=0
cic2 : ok=2 changed=0 unreachable=0 failed=0
cic3 : ok=2 changed=0 unreachable=0 failed=0
cic4 : ok=2 changed=0 unreachable=0 failed=0
cic5 : ok=2 changed=0 unreachable=0 failed=0
cic6 : ok=2 changed=0 unreachable=0 failed=0
cic7 : ok=2 changed=0 unreachable=0 failed=0
cic8 : ok=2 changed=0 unreachable=0 failed=0
cic9 : ok=2 changed=0 unreachable=0 failed=0
localhost : ok=1 changed=0 unreachable=0 failed=0
您始终可以使用 jinja 模板来使用 coefficient
变量...
参考文献:
me@pc:~/Ansible/playbooks/test_iterate$ ansible-playbook test0.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [srv-db1] ok: [srv-db-temp] TASK [debug] ******************************************************************* ok: [srv-db-temp] => { "msg": "101" } ok: [srv-db1] => { "msg": "102" } PLAY RECAP ********************************************************************* srv-db-temp : ok=2 changed=0 unreachable=0 failed=0 srv-db1 : ok=2 changed=0 unreachable=0 failed=0
test0.yml:
hosts: srv-dbs vars: - priority_start: 100 tasks: - debug: msg="{% for thishost in play_hosts %}{% if inventory_hostname==thishost %}{{ priority_start + loop.index }}{% endif %}{% endfor %}"