如何使用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
最满意答案
我几乎不做任何补丁,但我相信你要修补得太晚,或者说错了。
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 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 insmtplib
anymore. Instead, you'd need to patch that reference. Let's use a more realistic example, in which you havemodule.py
andtest_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 importedsmtplib.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 ...
-
Python 3 unittest不能在函数内使用模拟方法(Python 3 unittest cannot mock method used inside functions)[2022-07-15]
您必须修补被测试函数使用的绑定。 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 ...
-
如何使用unittest.mock从代码中删除副作用?(How can I use unittest.mock to remove side effects from code?)[2022-10-23]
假设你的异常是从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运行测试时,unittest.mock不起作用(unittest.mock doesn't work when tests are run with pytest)[2022-04-21]
好的,我显然对此做了一个糟糕的研究。 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客户端(Tornado non-blocking SMTP client)[2021-03-19]
我写了基于线程和队列的解决方案。 每个龙卷风过程一个线程。 该线程是一名工作人员,从队列中获取电子邮件,然后通过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.
-
使用Django SMTP(Using Django SMTP)[2022-01-06]
向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 ...