首页 \ 问答 \ 如何使用Ansible Playbook与特定群组一起玩(How to Play with particular group using Ansible Playbook)

如何使用Ansible Playbook与特定群组一起玩(How to Play with particular group using Ansible Playbook)

Ansible版本:2.1.0

我的ansible主机文件是:

[PM]
xyz.example.com ansible_connection=ssh

[ND]
pqr.example.com ansible_connection=ssh

[CM]
xyz.example.com ansible_connection=ssh
pqr.example.com ansible_connection=ssh

而剧本是:

- hosts: PM:ND:CM
   remote_user: root
   tasks:
    {some thing}

- hosts: PM
   remote_user: root
   tasks:
    {some thing}

 - hosts: ND
   remote_user: root
   tasks:
    {some thing}

- hosts: CM
   remote_user: root
   tasks:
    {some thing}

我使用以下命令运行playbook:

ansible-playbook --limit 'PM' akana-installation.yml

但仍然是剧本正在与所有主持人一起玩,这意味着

Play 'PM:ND:CM'
Play 'PM'
Play 'ND'
Play 'CM'

那些戏都在玩。 请帮我解决这个问题。

我需要的是: 执行剧本时,我会给团体名称,只有团体应该播放 ,所以请告诉我有没有其他办法。


Ansible version: 2.1.0

My ansible hosts file is:

[PM]
xyz.example.com ansible_connection=ssh

[ND]
pqr.example.com ansible_connection=ssh

[CM]
xyz.example.com ansible_connection=ssh
pqr.example.com ansible_connection=ssh

And playbook is:

- hosts: PM:ND:CM
   remote_user: root
   tasks:
    {some thing}

- hosts: PM
   remote_user: root
   tasks:
    {some thing}

 - hosts: ND
   remote_user: root
   tasks:
    {some thing}

- hosts: CM
   remote_user: root
   tasks:
    {some thing}

And I am running playbook with the following command:

ansible-playbook --limit 'PM' akana-installation.yml

But still the playbook is playing with all hosts, it means

Play 'PM:ND:CM'
Play 'PM'
Play 'ND'
Play 'CM'

those all plays are playing. Please help me to resolve this.

What I need is: While executing playbook I will give group name, that only group should play, so please let me know is there any other way.


原文:https://stackoverflow.com/questions/38324714
更新时间:2022-03-12 19:03

最满意答案

我几乎不做任何补丁,但我相信你要修补得太晚,或者说错了。 SMTP已经导入,导致直接引用原始类 - 它将不再在smtplib查找。 相反,您需要修补该引用。 让我们使用一个更现实的例子,其中有module.pytest_module.py

module.py

import smtplib
from smtplib import SMTP # Basically a local variable

def get_smtp_unqualified():
    return SMTP # Doing a lookup in this module

def get_smtp_qualified():
    return smtplib.SMTP # Doing a lookup in smtplib

test_module.py

import unittest
from unittest import patch
from module import get_smtp_unqualified, get_smtp_qualified

class ModuleTest(unittest.TestCase):
    def test_get_smtp_unqualified(self):
        with patch('module.SMTP') as smtp:
            self.assertIs(smtp, get_smtp_unqualified())

    def test_get_smtp_qualified_local(self):
        with patch('module.smtplib.SMTP') as smtp:
            self.assertIs(smtp, get_smtp_qualified())

    def test_get_smtp_qualified_global(self):
        with patch('smtplib.SMTP') as smtp:
            self.assertIs(smtp, get_smtp_qualified())

只要你在查找之前及时修补,它就会做你想要的 - 3次通过测试。 最早的时间是在导入除unittest之外的任何其他模块之前。 那些模块还没有导入smtplib.SMTP 。 更多关于这一点 。 但是,当您的测试分为多个模块时,它会变得棘手。

修补本质上是脏的。 你正在搞乱另一个人的内部。 为了使它工作,你必须在里面看。 如果内部发生变化,测试将会中断。 这就是为什么你应该把它作为最后的手段,并喜欢不同的手段,如依赖注入。 这是一个完全不同的主题,但无论如何,不​​要依赖修补来防止消息外出 - 也改变配置!


I hardly do any patching, but I believe you're patching either too late, or the wrong thing. SMTP has already been imported, resulting in a direct reference to the original class—it will not be looked up in smtplib anymore. Instead, you'd need to patch that reference. Let's use a more realistic example, in which you have module.py and test_module.py.

module.py:

import smtplib
from smtplib import SMTP # Basically a local variable

def get_smtp_unqualified():
    return SMTP # Doing a lookup in this module

def get_smtp_qualified():
    return smtplib.SMTP # Doing a lookup in smtplib

test_module.py

import unittest
from unittest import patch
from module import get_smtp_unqualified, get_smtp_qualified

class ModuleTest(unittest.TestCase):
    def test_get_smtp_unqualified(self):
        with patch('module.SMTP') as smtp:
            self.assertIs(smtp, get_smtp_unqualified())

    def test_get_smtp_qualified_local(self):
        with patch('module.smtplib.SMTP') as smtp:
            self.assertIs(smtp, get_smtp_qualified())

    def test_get_smtp_qualified_global(self):
        with patch('smtplib.SMTP') as smtp:
            self.assertIs(smtp, get_smtp_qualified())

As long as you patch in time before a lookup, it does what you want—3 passing tests. The very earliest time would be before importing any other modules than unittest. Then those modules will not have imported smtplib.SMTP yet. More on that here. It gets tricky though, when your tests are split over multiple modules.

Patching is inherently dirty. You're messing with another's internals. To make it work, you have to look on the inside. If the inside changes, tests will break. That's why you should consider it a last resort and prefer different means, such as dependency injection. That's a whole different topic, but in any case, don't rely on patching to prevent messages from going out—also change the configuration!

相关问答

更多
  • 我几乎不做任何补丁,但我相信你要修补得太晚,或者说错了。 SMTP已经导入,导致直接引用原始类 - 它将不再在smtplib查找。 相反,您需要修补该引用。 让我们使用一个更现实的例子,其中有module.py和test_module.py 。 module.py : import smtplib from smtplib import SMTP # Basically a local variable def get_smtp_unqualified(): return SMTP # Doing ...
  • 您必须修补被测试函数使用的绑定。 from os import urandom # in file.py 将名称urandom绑定到file模块中的函数os.urandom 。 Foo.b通过file.urandom绑定Foo.b访问函数。 所以对Foo.b的测试必须修补file.urandom ,而不是os.urandom 。 You must patch the binding used by the function being tested. from os import urandom # ...
  • 您似乎正在修补错误的位置。 在decorators.py您使用的是全局名称get_authenticated_user() ,但您正在修补api.accounts.helpers的名称。 您可能导入了get_authenticated_user : from api.accounts.helpers import get_authenticated_user 这意味着修补原始位置不会更改decorators的引用 。 修补全局decorators : @patch('decorators.get_auth ...
  • 由于我不知道你的查询逻辑是什么,我修改了query ,直接通过tables_and_columns_to_select参数接受一个sentinel值。 # b_and_ex_q.py def build_and_execute_query(tables_and_columns_to_select): """Build and execute a query. Args: tablesAndColumnsToSelect (dict) - keys are table ...
  • 假设你的异常是从foo.authenticate()提出的,那么你想要实现的是,数据在测试中是否真的有效并不一定重要。 你想说的是这个: 当这个外部方法引发某些东西时,我的代码应该根据这个东西做出相应的行为。 因此,考虑到这一点,您要做的是使用不同的测试方法来传递应该是有效数据的内容,并让您的代码做出相应的反应。 数据本身并不重要,但它提供了一种文档化的方式来显示代码应该如何处理以这种方式传递的数据。 最终,您不应该关心nova客户端如何处理您提供的数据(nova客户端已经过测试,您不应该关心它)。 无论你 ...
  • 传播一个布尔“mock_if_true”,并在最后一刻不要发送电子邮件。 只要你没有很多这样的布尔值,这是简单/愚蠢的。 或者 :覆盖python类的方法(如果有的话)来嘲笑它 或者 :考虑将构建电子邮件对象的代码与发送它的代码分开。 因此,您可以轻松测试该对象是否已正确构建。 Propagate a boolean "mock_if_true" and at the very last moment do not send the email. This is simple/stupid and OK a ...
  • 好的,我显然对此做了一个糟糕的研究。 pytest模块没有问题。 为了解决这个问题,我需要修补app.models.Talk ,而不是奇怪的app.models.Talk.query.all 。 在我修补课程后,我只是添加了我需要的属性: @patch('app.models.Talk') def test_send_schedule(self, talk_class_mock): talk_mocks = [] for talk_id in range(1, 6): ...
  • 我写了基于线程和队列的解决方案。 每个龙卷风过程一个线程。 该线程是一名工作人员,从队列中获取电子邮件,然后通过SMTP发送。 您通过将其添加到队列中来发送来自龙卷风应用程序的电子邮件。 简单而简单。 以下是GitHub上的示例代码: 链接 I wrote solution based on threads and queue. One thread per tornado process. This thread is a worker, gets email from queue and then se ...
  • 不建议使用模拟ClientSession 。 推荐的方法是创建虚假服务器并向其发送实际请求。 看看aiohttp的例子 。 Mocking ClientSession is discouraged. Recommended way is creation fake server and sending real requests to it. Take a look on aiohttp example.
  • 向SMTP服务团队提出请求,允许使用有效的发件人电子邮件ID从您的IP /域名发送电子邮件。 批准后,从smtp服务团队获取EMAIL_HOST和EMAIL_PORT的详细信息。 并将其添加到settings.py,如下所示: EMAIL_USE_TLS = True EMAIL_HOST = 'business.com' EMAIL_PORT = '25' #or 587 or any others EMAIL_HOST_USER = 'mailer@business.com' #Same as the ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)