简体   繁体   English

将传递变量传递给主机的Ansible剧本:双引号

[英]Issue passing variable to Ansible playbook for hosts: with double quotes

I'm trying to write a playbook that kicks off role playbooks and pass a list of hosts to it. 我正在尝试编写一本开始扮演角色剧本并将其主机列表传递给它的剧本。 The "master" playbook has some load balancing logic in it that I don't want to repeat in every role playbook and can't put into site.yml. “主”剧本中有一些负载平衡逻辑,我不想在每个角色剧本中都重复,也无法放入site.yml中。

inventory.yml inventory.yml


[webservers]
Web1
Web2
Web3
Web4

master.yml master.yml

---
- name: Split Inventory into Odd/Even
  hosts: all
  gather_facts: false

  tasks:

    - name: Set Group Odd
      set_fact:
        group_type: "odd"
      when: (inventory_hostname.split(".")[0])[-1] | int is odd

    - name: Set Group Even
      set_fact:
        group_type: "even"
      when: (inventory_hostname.split(".")[0])[-1] | int is even 

    - name: Make new groups "odd" or "even"
      group_by:
        key: "{{ group_type }}"

- name: Perform Roles on Odd 

  include: webservers.yml hosts={{ groups['odd'] | join(' ')}}


- name: Perform Roles on Even 

  include: webservers.yml hosts={{ groups['even'] | join(' ')}}

webservers.yml webservers.yml


- name: Perform Tasks on Webservers
  hosts: webservers:&"{{ hosts | replace('\"','')}}"
  roles:
    - pause

The join(' ') flattens the list of hosts into a string with a space separating each one. join('')将主机列表展平为一个字符串,每个字符串之间用空格隔开。 When I run the playbook it passes the list of hosts to webservers.yml, however it adds double quotes to the beginning and end, causing webservers.yml to do nothing since no hosts match. 当我运行剧本时,它将主机列表传递给webservers.yml,但是它在开头和结尾添加了双引号,导致webservers.yml不执行任何操作,因为没有主机匹配。 I would assume the replace('\\"','') would remove the quotes around the string but doesn't seem to be the case. Here's an example output from webservers.yml: 我假设replace('\\“','')会删除字符串周围的引号,但事实并非如此,这是来自webservers.yml的示例输出:

[WARNING]: Could not match supplied host pattern, ignoring: Web4"

[WARNING]: Could not match supplied host pattern, ignoring: "Web2

Any ideas? 有任何想法吗? Does hosts: handle filtering differently? hosts:处理过滤是否不同?

I feel that you use a role and a play in a wrong way. 我觉得您错误地使用了角色和戏剧。 When you do tasks you should not change list of hosts this task or role is been executed upon. 执行任务时,请勿更改主机列表,否则将执行此任务或角色。 Basically, only play (a thing with 'hosts: ..., tasks: ..., roles: ...') can control where to run. 基本上,只有玩(带有“主持人:...,任务:...,角色:...”的东西)才能控制运行位置。

There are few exceptions, fe you can play with delegation and so on. 很少有例外,例如您可以与代表团一起玩等等。 But for your case any attempt to use tasks or roles to control list of the host will only bring misery and hate (toward yourself, toward ansible, etc). 但是对于您而言,任何尝试使用任务或角色来控制主机列表的尝试只会带来痛苦和仇恨(朝着自己,朝旁,等等)。

To do it right, just add yet another play in to your playbook (playbook is a list of plays). 要正确执行此操作,只需将另一个剧本添加到您的剧本中(该剧本是一个剧本列表)。

Here is your code, slightly modified. 这是您的代码,略有修改。

---
- name: Split Inventory into Odd/Even
  hosts: all
  gather_facts: false

  tasks:

    - name: Set Group Odd
      set_fact:
        group_type: "odd"
      when: (inventory_hostname.split(".")[0])[-1] | int is odd

    - name: Set Group Even
      set_fact:
        group_type: "even"
      when: (inventory_hostname.split(".")[0])[-1] | int is even 

    - name: Make new groups "odd" or "even"
      group_by:
        key: "{{ group_type }}"


- name: Doing odd things
  hosts: odd
  gather_facts: false
  tasks:
   - name: Perform Roles
     include: webservers.yml


- name: Doing even things
  hosts: even
  gather_facts: false
  tasks:
   - name: Perform Roles
     include: webservers.yml

You can see, I've just assigned a playbook to two groups ('odd' and 'even'). 可以看到,我刚刚将一个剧本分配给了两个组(“奇数”和“偶数”)。 Dynamic groups are preserved between plays in a playbook, and they are no different from any other group in this matter. 动态组在剧本的剧本之间保留,并且在这一点上与任何其他组都没有区别。

PS Do not use 'include', use 'import_tasks' (includes are dangerous in newer versions of ansible, avoid them if you could.). PS请勿使用“ include”,而应使用“ import_tasks”(在较新版本的ansible中,includes很危险,请尽量避免使用它们。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM