Docker Ansible:迭代库存组

Docker Ansible:迭代库存组,docker,ansible,nested-loops,ansible-2.x,Docker,Ansible,Nested Loops,Ansible 2.x,我有一个组tgt集群包括3个主机。我已经写下了在tgt集群上执行的部署容器的角色。我正在控制使用和\u序列部署的容器数量。我的任务是这样的 - name: Deploy Container docker_container: name: "C{{ item }}" image: "{{ image_name }}:{{ image_tag }}" recreate: yes detach: yes tty: yes interactive:

我有一个组
tgt集群
包括3个主机。我已经写下了在
tgt集群上执行的部署容器的角色。我正在控制使用
和\u序列部署的容器数量。我的任务是这样的

- 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 }}"  
如果我想部署一个容器,那么当前playbook正在
tgt集群
组的所有3台主机上执行,最后我得到了3个容器。那么,在这种情况下,我应该如何创建嵌套循环,以循环方式控制主机上的任务执行

如果我们想部署4个容器。。 第一个容器应部署在组中的第一台主机上, 第二个容器应部署在组中的第二个容器上, 第三台应部署在组中的第三台主机上,第四个容器应部署回组中的第一台主机

我已经用以下内容更新了我的变量文件,
count\u per\u hosts
变量被分配给
with\u sequence
。执行时,我收到一个错误,声明“无法将arg count=u“”解析为整数”。所以我用_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 %}"
目前我的执行情况是这样的<代码>部署容器:部署过程
任务应该创建容器,但我们在那里没有看到任何日志。 此外,我还尝试将
主机
num\u主机
移动到
count\u per\u主机
中,与答案中所示的语法相同,但执行不会溢出任何输出

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”:“无法解析arg count=u\”#“#”idx=x%num\u hosts\\ncc=assignment.get(the_hosts[idx],0)\\n\u=assignment.update({the_hosts[idx]:cc+1})\\nidx=x%num\u hosts\\ncc=assignment.get(the_hosts[idx],0)\\n\u=assignment.update({the_hosts[idx]:cc+1})\\n'\\n\”作为整数”
}

现在的剧本是这样的

- 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
如果我们用_sequence:count=“{count\u per\u hosts}}}”指定
,则输出为:count=“{count\u per\u hostsget(ansible\u hostname,0)}”我们得到
致命:[br1.lab]:失败!=>{“msg”:“'ansible_hostname'未定义”}

我试图注册输出并进行检查,然后运行
debug
,查看内容,发现无法从组中选择主机

ok:[br1.lab]=>{
“我的内容”:{
“更改”:错误,
“结果”:[],
“跳过”:正确,
“跳过原因”:“列表中没有项目”
}

}

您只需自己完成计数任务就会更快乐:

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\u容器:
任务中,您现在可以让
with\u序列:
成为每个主机的计数,其中一些可能为零:

- 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) }}"

我在变量文件中添加了如下循环,
the_hosts:{{groups['tgt-cluster']}}num_hosts:{{the_hosts | length}}count_per_hosts:>“{%for x在范围内(要部署的_容器的数量)%}-set idx=x%num_hosts set cc=assignment.get(the_hosts[idx],0)set=assignment.update({the_hosts[idx]:cc+1}){%endfor%}“
count\u per\u hosts
变量被分配给
with\u sequence
,但是我收到一个错误,声明
无法将arg count=u“”解析为整数”
我看不懂你的格式;你能用它更新你的问题吗?但是,根据你提供的错误消息,序列中的
count=
似乎正在解析为空字符串,如果当前主机不在分配中,可能会这样做;
count={{不管你有什么|默认值(0)}
可以解决这个问题,或者你可以更新你的jinja代码,然后将所有主机初始化为零计数。我想我掉进了一个我经常遇到的陷阱:我想你对jinja2很熟悉,只需要轻轻推一下。我会更新更具体的答案,这会更有帮助。!嗨,Matthew,谢谢你的帮助详细的约会答案。我已经更新了变量文件,并且按照更新的答案的顺序
,ansible抛出了这个奇怪的错误。
fatal:[br1.lab]:失败!=>{“msg”:“'count\u per\u hosts'未定义”}
如果我只保留
count:{count\u per\u hosts}”
那么我们也会遇到同样的错误。
- 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) }}"