本文介绍了Ansible:遍历库存组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组tgt-cluster包括3个主机.我已写下角色来部署在tgt-cluster组上执行的容器.我正在控制要使用with_sequence部署的容器的数量.我的任务看起来像这样.

I have group tgt-cluster includes 3 hosts. I have written down role to deploy container which is executing on tgt-cluster group. I am controlling the number of containers to deploy with with_sequence. My tasks looks like this.

- 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个容器.因此,在这种情况下,我应如何创建嵌套循环来以循环方式控制主机上的任务执行.

If I want to deploy one container, currently playbook is executing on all 3 hosts in tgt-cluster group and I end up with 3 containers. So How should I created nested loop in this case to control the task execution on hosts on round robin fashion.

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

Say if we want to deploy 4 container..1st container should be deployed on 1st host in group,2nd container should be deployed on 2nd container in group,3rd should be deployed on 3rd hosts in group and 4th container should be deployed back to the 1st host in group.

我使用以下更新了我的变量文件,并且count_per_hosts变量已分配给with_sequence.在执行过程中,我收到错误消息,指出无法将arg count = u解析为整数".所以我更新了with_sequence: count="{{ count_per_hosts | int }}",它没有抛出错误,但是它没有执行任务而不跳过它.

I have updated my variable file with following, and count_per_hosts variable is assigned to with_sequence. While executing I was getting error stating "can't parse arg count=u'' as integer". So I updated with_sequence: count="{{ count_per_hosts | int }}" and it's not throwing error but It is not executing tasking not skipping it.

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_hostsnum_hosts移入count_per_hosts内,语法与答案中指示的相同,但是执行不会溢出任何输出.

Currently my execution looks like this. deploy_container : Deploy Process tasks should create container but we don't see any logs over there.Also, I tried to move the_hosts and num_hosts inside count_per_hosts same syntax as indicated in answers but execution does not spill any output.

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

Also, I tried default(0) but ansible is throwing error.{ "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"}

当前的剧本看起来像这样,

Current playbook looks like this,

- 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以查看内容,发现它无法从组中选择主机.

I tried to register the output and check and ran debug on that to see the content and found it's not able to pick the host from group.

ok: [br1.lab] => { "my_content": { "changed": false, "results": [], "skipped": true, "skipped_reason": "No items in the list" }}

ok: [br1.lab] => { "my_content": { "changed": false, "results": [], "skipped": true, "skipped_reason": "No items in the list" }}

推荐答案

只要自己完成计数分配,您就会更加快乐:

You will be much happier just doing the count assignment by yourself:

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:作为每个主机的计数,其中一些可能为零:

Then in the actual docker_container: task, you are now able to have the with_sequence: be a count per host, and some of them may be zero:

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

这篇关于Ansible:遍历库存组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:43