我有tgt-cluster
组,其中包括3位主机。我已写下角色来部署在tgt-cluster
组上执行的容器。我正在控制要使用with_sequence
进行部署的容器的数量。我的任务看起来像这样。
- name: Deploy Container
docker_container:
name: "C{{ item }}"
image: "{{ image_name }}:{{ image_tag }}"
recreate: yes
detach: yes
tty: yes
interactive: yes
with_sequence: count="{{ number_of_container_to_deploy }}"
如果我要部署一个容器,则当前在tgt-cluster
组中的所有3台主机上执行剧本,最后得到3个容器。因此,在这种情况下,我应该如何创建嵌套循环来以循环方式控制主机上的任务执行。
说,如果我们要部署4个容器。 第一个容器应部署在组中的第一个主机上, 第2个容器应分组部署在第2个容器上, 第三台应部署在组中的第三台主机上,第四台容器应部署回组中的第一台主机。
我已使用以下更新了我的变量文件,并且count_per_hosts
变量已分配给with_sequence
。在执行时,我收到错误消息,指出“无法将arg count = u解析为整数”。所以我更新了with_sequence: count="{{ count_per_hosts | int }}"
,它没有抛出错误,但是没有执行任务而不跳过它。
the_hosts: "{{ groups['tgt-cluster']}}"
num_hosts: "{{ the_hosts | length }}"
count_per_hosts: >
"{% for x in range(number_of_container_to_deploy) %}
- set idx = x % num_hosts
set cc = assignment.get(the_hosts[idx], 0)
set _ = assignment.update({the_hosts[idx]: cc + 1})
{% endfor %}"
目前,我的处决看起来像这样。 deploy_container : Deploy Process
个任务应该创建容器,但是在那儿看不到任何日志。
另外,我尝试将the_hosts
和num_hosts
移到count_per_hosts
内,语法与答案中指示的相同,但是执行不会溢出任何输出。
PLAY [bin installation] **************************************************************************************************************************************************************************************
META: ran handlers
TASK [deploy_container : Deploy Process] *************************************************************************************************************************************************************
task path: /home/tg/Documents/playbooks/roles/deploy_container/tasks/main.yml:3
META: ran handlers
META: ran handlers
PLAY RECAP *****************************************************************************************************************************************************************************************************
br1.lab : ok=0 changed=0 unreachable=0 failed=0
br2.lab : ok=0 changed=0 unreachable=0 failed=0
br3.lab : ok=0 changed=0 unreachable=0 failed=0
此外,我尝试了default(0)
,但是ansible抛出错误。
{
"msg": "can't parse arg count=u\"#' # ' idx = x % num_hosts\\ncc = assignment.get(the_hosts[idx], 0)\\n_ = assignment.update({the_hosts[idx]: cc + 1})\\nidx = x % num_hosts\\ncc = assignment.get(the_hosts[idx], 0)\\n_ = assignment.update({the_hosts[idx]: cc + 1})\\n'\\n\" as integer"
}
当前的剧本看起来像这样
- name: bin installation
hosts: tgt-cluster
user: "{{ user }}"
gather_facts: no
become: yes
vars:
count_per_hosts: |
{% set the_hosts = groups["tgt-cluster"] %}
{% set num_hosts = the_hosts | length %}
{% set result = {} %}
{% for x in range(number_of_process_to_deploy) %}
{% set idx = x % num_hosts %}
{% set h = the_hosts[idx] %}
{% set cc = result.get(h, 0) %}
{% set _ = result.update({h: cc + 1}) %}
{% endfor %}
{{ result }}
roles:
- deploy_container
使用with_sequence: count="{{ count_per_hosts }}"
输出,如果我们指定with_sequence: count="{{ count_per_hostsget(ansible_hostname, 0) }}"
,我们得到fatal: [br1.lab]: FAILED! => {"msg": "'ansible_hostname' is undefined"}
TASK [deploy_container : Deploy Process] ***************************************************************************
fatal: [br1.lab]: FAILED! => {"msg": "can't parse arg count=u\"{u'br1.lab': 1, u'br2.lab': 1}\" as integer"}
fatal: [br2.lab]: FAILED! => {"msg": "can't parse arg count=u\"{u'br1.lab': 1, u'br2.lab': 1}\" as integer"}
fatal: [br3.lab]: FAILED! => {"msg": "can't parse arg count=u\"{u'br1.lab': 1, u'br2.lab': 1}\" as integer"}
我尝试注册输出并检查并运行debug
以查看内容,发现它无法从组中选择主机。
ok: [br1.lab] => {
"my_content": {
"changed": false,
"results": [],
"skipped": true,
"skipped_reason": "No items in the list"
}
}
答案 0 :(得分:1)
仅由您自己进行计数分配,您就会更加快乐:
vars:
the_hosts: "{{ groups['tgt-cluster']}}"
num_hosts: "{{ the_hosts | length }}"
count_per_hosts: |
{% set result = {} %}
{% for x in range(number_of_container_to_deploy) %}
{% set idx = x % num_hosts %}
{% set h = the_hosts[idx] %}
{% set cc = result.get(h, 0) %}
{% set _ = result.update({h: cc + 1}) %}
{% endfor %}
{{ result }}
然后,在实际的docker_container:
任务中,您现在可以将with_sequence:
作为每个主机的计数,其中一些可能为零:
- name: Deploy Container
docker_container:
name: "C{{ item }}"
image: "{{ image_name }}:{{ image_tag }}"
recreate: yes
detach: yes
tty: yes
interactive: yes
with_sequence:
count: "{{ count_per_hosts.get(ansible_hostname, 0) }}"