Ansible:迭代库存组 [英] Ansible : iterate over inventory groups

查看:27
本文介绍了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 }}"  

如果我想部署一个容器,当前 playbook 正在 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": "无法解析 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\" 作为整数"}

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) }}" 我们get fatal: [br1.lab]: 失败!=>{"msg": "'ansible_hostname' 未定义"}

output with with_sequence: count="{{ count_per_hosts }}" if we specify with_sequence: count="{{ count_per_hostsget(ansible_hostname, 0) }}" we get 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] =>{我的内容":{改变":假,结果": [],跳过":真的,"skiped_reason": "列表中没有项目"}}

推荐答案

自己做计数作业会更快乐:

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:迭代库存组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆