首页 \ 问答 \ Ansible相当于Puppet的“除非”属性(Ansible equivalent of Puppet's “unless” attribute)

Ansible相当于Puppet的“除非”属性(Ansible equivalent of Puppet's “unless” attribute)

在Puppet中,使用exec资源时,可以指定“ 除非 ”属性。 除非“except”命令以状态0退出,否则Puppet exec将不会运行。

木偶的例子:

exec { 'Make sure frob is installed':
  command => 'apt-get install -y frob',
  unless  => 'frob --version',
}

Ansible的命令模块有一个“创建”选项,用于查找文件,但我没有看到“除非”选项。

如何在Ansible中指定Puppet风格的“除非”属性?


In Puppet, when using the exec resource, you can specify an "unless" attribute. The Puppet exec will not run unless the "unless" command exits with status 0.

Puppet example:

exec { 'Make sure frob is installed':
  command => 'apt-get install -y frob',
  unless  => 'frob --version',
}

Ansible's command module has a "creates" option that looks for a file, but I don't see an "unless" option.

How can I specify a Puppet-style "unless" attribute in Ansible?


原文:https://stackoverflow.com/questions/36754736
更新时间:2022-07-23 13:07

最满意答案

在这里你应该记住几件事:

  1. 它存在一个Thread.Suspend机制, 这里描述但它已经过时,并且有充分的理由,因为:

因为Thread.Suspend和Thread.Resume不依赖于被控制的线程的协作,所以它们是高度侵入性的并且可能导致严重的应用程序问题,例如死锁(例如,如果你挂起一个拥有另一个线程的资源的线程需要)。

因此,您可以使用该机制并等待并查看可能的死锁或其他同步错误或:

  1. 您可以将该同步逻辑移动到要挂起的线程中,在您的情况下使用processRunning()方法并执行以下操作:

    while(i <execTime.Sum()&& flag == 0)//尚未停止

如果你想要更精细的粒度,甚至在循环的中间停止而不是在每次迭代时,你可以随意添加更多的检查。

您还需要将标志声明为volatile,因为该字段由两个线程使用,需要标记为:

public static volatile int flag = 0 ;

这是一个简化的例子:

    private static AutoResetEvent signal = new AutoResetEvent(false);
    private volatile static bool interruptFlag;
    private volatile static bool abortFlag;

    private static void Process()
    {
        //replace true with your condition
        while (true)
        {
            if (interruptFlag)
            {
                if (signal.WaitOne())
                {
                    if (abortFlag)
                    {
                        Console.WriteLine("exiting");
                        return;
                    }
                }
            }

            Console.WriteLine("doing work");

            //.. important work here
        } 
    }

    private static void Interrupt() {
        ConsoleKeyInfo c = Console.ReadKey();
        if (c.Key == ConsoleKey.Enter) {
            interruptFlag = true;
            // Should Pause The First Thread Here And Ask To Continue/Resume Or Kill

            c = Console.ReadKey();

            if (c.Key == ConsoleKey.Escape)
            {
                Console.WriteLine("Interrupting");
                abortFlag = true;
                signal.Set();
            }
            else
            {
                Console.WriteLine("Continuing");
                interruptFlag = false;
                abortFlag = false;
                signal.Set();
            }
        }
    }

    static void Main()
    {
        Thread process = new Thread(Process);
        process.Start();

        Thread interrupt = new Thread(Interrupt);
        interrupt.Start();

    }

There are a couple of things that you should have in mind here:

  1. It exists a Thread.Suspend mechanism described here but it is obsolete and for a good reason, since:

Because Thread.Suspend and Thread.Resume do not rely on the cooperation of the thread being controlled, they are highly intrusive and can result in serious application problems like deadlocks (for example, if you suspend a thread that holds a resource that another thread will need).

So you could use that mechanism and wait and see possible deadlocks or other synchronization bugs or:

  1. You could move that synchronization logic into the thread that you want to suspend, the processRunning() method in your case and do something like this:

    while (i < execTime.Sum() && flag==0) //not stopped yet

If you want finer granularity, that is even stopping in the middle of the loop not at each iteration, it is your job to add more checks as you please.

You also need to declare your flag as volatile since that field is used by two threads and needs to be marked as such:

public static volatile int flag = 0;

This is a simplified example:

    private static AutoResetEvent signal = new AutoResetEvent(false);
    private volatile static bool interruptFlag;
    private volatile static bool abortFlag;

    private static void Process()
    {
        //replace true with your condition
        while (true)
        {
            if (interruptFlag)
            {
                if (signal.WaitOne())
                {
                    if (abortFlag)
                    {
                        Console.WriteLine("exiting");
                        return;
                    }
                }
            }

            Console.WriteLine("doing work");

            //.. important work here
        } 
    }

    private static void Interrupt() {
        ConsoleKeyInfo c = Console.ReadKey();
        if (c.Key == ConsoleKey.Enter) {
            interruptFlag = true;
            // Should Pause The First Thread Here And Ask To Continue/Resume Or Kill

            c = Console.ReadKey();

            if (c.Key == ConsoleKey.Escape)
            {
                Console.WriteLine("Interrupting");
                abortFlag = true;
                signal.Set();
            }
            else
            {
                Console.WriteLine("Continuing");
                interruptFlag = false;
                abortFlag = false;
                signal.Set();
            }
        }
    }

    static void Main()
    {
        Thread process = new Thread(Process);
        process.Start();

        Thread interrupt = new Thread(Interrupt);
        interrupt.Start();

    }

相关问答

更多
  • Condition s可以用于此 。 以下是填充骨架的示例: class Me(threading.Thread): def __init__(self): threading.Thread.__init__(self) #flag to pause thread self.paused = False # Explicitly using Lock over RLock since the use of self.paused ...
  • bool pause=false; std::condition_variable cv; std::mutex m; void C::t2_func(){ for(int i=0;i lk(m); cv.wait(lk); lk.unlock(); } process_data(data[i]); } } vo ...
  • 请记住,除了IO阻塞操作的情况之外,在Pythin中使用线程不会授予您并行处理。 有关这方面的更多信息,请查看此内容 你不能在Python中任意暂停一个线程 (在进一步阅读之前请记住这一点)。 我不确定你是否有办法在操作系统级别(例如使用pure-C)。 您可以做的是允许线程在您事先考虑的特定点暂停。 我会举个例子: class MyThread(threading.Thread): def __init__(self, *args, **kwargs): super(MyThre ...
  • 并CyclicBarrier有两个非常有用的类 - CountDownLatch和CyclicBarrier 。 如果您只需要此行为一次,则可能需要第一个(因为它无法重置)。 线程1将等待直到线程2通知。一旦它被倒计数到0,线程1将永远不会再次阻塞在await() : CountDownLatch cdl = new CountDownLatch(1); // thread 1: cdl.await(); // thread 2: cdl.countDown(); 线程将在await()处阻塞,直到有 ...
  • 在这里你应该记住几件事: 它存在一个Thread.Suspend机制, 这里描述但它已经过时,并且有充分的理由,因为: 因为Thread.Suspend和Thread.Resume不依赖于被控制的线程的协作,所以它们是高度侵入性的并且可能导致严重的应用程序问题,例如死锁(例如,如果你挂起一个拥有另一个线程的资源的线程需要)。 因此,您可以使用该机制并等待并查看可能的死锁或其他同步错误或: 您可以将该同步逻辑移动到要挂起的线程中,在您的情况下使用processRunning()方法并执行以下操作: while ...
  • 我刚刚开始玩游戏,我在早期使用LunarLander作为灵感......但事实证明LunarLander示例代码实际上有这个错误(尝试启动已经运行的线程)! 下面是在扩展SurfaceView的类中解决问题的方法。 我在某个地方找到了解决方案,但是我记不起来了,因为很久以前。 public void surfaceCreated(SurfaceHolder holder) { if (mMyThreadName.getState() == Thread.State.TERMINATED) { ...
  • 您可以使用Form.ShowDialog方法在应用程序中显示模式对话框。 调用此方法时,直到关闭对话框后才会执行其后面的代码。 通过将对话框分配给窗体上Button的DialogResult属性或通过在代码中设置窗体的DialogResult属性,可以为对话框分配DialogResult枚举的值之一。 然后,此方法返回此值。 您可以使用此返回值来确定如何处理对话框中发生的操作。 例如,如果关闭对话框并通过此方法返回DialogResult.Cancel值,则可能会阻止执行ShowDialog调用后的代码。 ...
  • 首先,全局声明myThread对象(只有一个!)。 myThread heartGraph = new myThread() 然后你想在一个新线程中启动你的Worker-Method。 Thread worker = new Thread(heartGraph.threadHeartrateGraph); worker.Start(); 现在,您可以使用ManualResetEvent暂停/恢复工作。 if (checkBoxPause.Checked == true) { //HRDataSu ...

相关文章

更多

最新问答

更多
  • 您如何使用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)