如何在Ansible中遍历库存和分配价值 [英] How to loop through inventory and assign value in Ansible

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

问题描述

我的Ansible剧本中有一个任务,我想遍历我拥有的组中的每个主机,并且我想为每个主机从我在vars文件夹中创建的主机名列表中分配一个名称.

I have a task in my Ansible playbook that I'm wanting iterate over each host in the group that I have and for each host I would like to assign a name from the hostname list that I've created in the vars folder.

我熟悉通过编写循环:"{{groups ['mygroup']}}"来遍历清单,并且我有一个主机名列表,我希望在主机文件中为"mygroup"中的每个IP分配地址.

I'm familiar with looping through inventory already by writing loop: "{{ groups['mygroup'] }}" and I have a list of hostnames I would like to assign each IP in 'mygroup' within the host file.

# In tasks file - roles/company/tasks/main.yml
- name: change hostname
  win_hostname:
    name: "{{ item }}"
  loop: "{{ hostname }}"
  register: res

# In the Inventory file
[company]
10.0.10.128
10.0.10.166
10.0.10.200

# In vars - roles/company/vars/main.yml
hostname:
  - GL-WKS-18
  - GL-WKS-19
  - GL-WKS-20

# site.yml file located under /etc/ansible
- hosts: company
  roles:
    - common
    - company #This is where the loop exists mentioned above.

# Command to run playbook
ansible-playbook -i hosts company.yml

我似乎不知道具体情况,还是知道这件事,但是如何将清单组中的主机进行迭代并分配已在已创建列表(角色vars文件夹中)中拥有的名称呢?

I seem to have the individual pieces down or know about it, but how can I combine iterating over hosts from an inventory group and assign names that I have in an already created list (in roles vars folder) already?

更新 上面提到的任务已更新,以反映答案中提到的更改:

UPDATE the task mentioned above has been updated to reflect changes mentioned in answer:

- name: change hostname
  win_hostname:
    name: "{{ item.1 }}"
  loop: {{ groups.company|zip(hostname)|list }}"
  register: res

但是我得到的输出是不正确的,它不应运行9次,而只能运行3次,对于清单中[company]组中的每个IP一次.此外,列表中只有三个主机名需要分配给清单工作表中的每个主机.

However the output I'm getting is incorrect, this should not run 9 times rather only three times, once per IP in the [company] group in the inventory. Also there are only three hostnames in a list that need to be assigned to each of the hosts in the inventory sheet.

changed: [10.0.10.128] => (item=[u'10.0.10.128', u'GL-WKS-18'])
changed: [10.0.10.166] => (item=[u'10.0.10.128', u'GL-WKS-18'])
changed: [10.0.10.200] => (item=[u'10.0.10.128', u'GL-WKS-18'])
changed: [10.0.10.128] => (item=[u'10.0.10.166', u'GL-WKS-19'])
changed: [10.0.10.166] => (item=[u'10.0.10.166', u'GL-WKS-19'])
changed: [10.0.10.200] => (item=[u'10.0.10.166', u'GL-WKS-19'])
ok: [10.0.10.128] => (item=[u'10.0.10.200', u'GL-WKS-20'])
ok: [10.0.10.166] => (item=[u'10.0.10.200', u'GL-WKS-20'])
ok: [10.0.10.200] => (item=[u'10.0.10.200', u'GL-WKS-20'])

推荐答案

每当我有关于在Ansible中循环播放的问题时,我也会去访问

Whenever I have a question about looping in Ansible I also go visit the Loops documentation. It sounds like you want to iterate over two lists in parallel, pairing an item from the list of hosts in your inventory with an item from the list of hostnames. In previous versions of Ansible that would suggest using the with_together loop, while with more recent versions of Ansible that suggests the zip filter (there's an example in the docs here).

为了针对您的用例进行演示,我首先介绍了包含三个主机的清单文件:

To demonstrate this for your use case, I started with an inventory file that has three hosts:

[mygroup]
hostA ansible_host=localhost
hostB ansible_host=localhost
hostC ansible_host=localhost

以及以下剧本:

---
- hosts: all

- hosts: localhost
  gather_facts: false
  vars:
    hostnames:
      - hostname01
      - hostname02
      - hostname03
  tasks:
    - name: change hostname
      debug:
        msg:
          win_hostname:
            name: "{{ item }}"
      loop: "{{ groups.mygroup|zip(hostnames)|list }}"

在这里,我使用的是debug任务,而不是实际运行win_hostname任务.正在运行的输出:

Here I'm using a debug task instead of actually running the win_hostname task. The output of running:

ansible-playbook -i hosts playbook.yml

看起来像:

TASK [change hostname] ********************************************************************************************************************************
ok: [localhost] => (item=[u'hostA', u'hostname01']) => {
    "msg": {
        "win_hostname": {
            "name": [
                "hostA", 
                "hostname01"
            ]
        }
    }
}
ok: [localhost] => (item=[u'hostB', u'hostname02']) => {
    "msg": {
        "win_hostname": {
            "name": [
                "hostB", 
                "hostname02"
            ]
        }
    }
}
ok: [localhost] => (item=[u'hostC', u'hostname03']) => {
    "msg": {
        "win_hostname": {
            "name": [
                "hostC", 
                "hostname03"
            ]
        }
    }
}

如您所见,它会将清单中的每个主机与hostnames列表中的主机名配对.

As you can see, it's paired each host from the inventory with a hostname from the hostnames list.

更新

根据您提供的其他信息,我认为您 真正想要的是这个:

Based on the additional information you've provided, I think what you actually want is this:

    - name: change hostname
      win_hostname:
        name: "{{ hostnames[group.company.index(inventory_hostname) }}"

这将从hostname中为您主机中的每个主机分配一个值 存货.我们正在寻找当前的位置 您组中的inventory_hostname,然后使用该索引将其编入 hostnames列表.

This will assign one value from hostname to each host in your inventory. We're looking up the position of the current inventory_hostname in your group, and then using that to index into the hostnames list.

这篇关于如何在Ansible中遍历库存和分配价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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