大网数据广告活动
服务器运维问答

What is the equivalent of Puppet's `unless` in Ansible?

· 52CE 我爱测

Background: in Puppet it is possible to execute a command unless it has already been executed: exec { '/bin/echo root >> /usr/lib/cron/cron.allow': path => '/usr/bin:/usr/sbin:/bin', unless => 'grep root /usr/lib/cron/cron.allow …

问题原文

原帖链接 · 030 · CC BY-SA 3.0

Background: in Puppet it is possible to execute a command unless it has already been executed:

exec { '/bin/echo root >> /usr/lib/cron/cron.allow':
  path   => '/usr/bin:/usr/sbin:/bin',
  unless => 'grep root /usr/lib/cron/cron.allow 2>/dev/null',
}

Aim: to execute a command unless it has already been run in Ansible

Methods

tasks/main.yml

- name: add vhost sensu
  command: rabbitmqctl add_vhost /sensu

Results

TASK [ansible-rabbitmq : add vhost sensu] **************************************
fatal: [111.222.333.444]: FAILED! => {"changed": true, "cmd": ["rabbitmqctl",
"add_vhost", "/sensu"], "delta": "0:00:00.210140", "end": 
"2016-07-29 12:55:19.384455", "failed": true, "rc": 2, "start":
"2016-07-29 12:55:19.174315", "stderr": "Error: vhost_already_exists: /sensu", 
"stdout": "Creating vhost \"/sensu\" ...", "stdout_lines": 
["Creating vhost \"/sensu\" ..."], "warnings": []}

Discussion

Googling unless ansible showed this document about when. Based on that documentation, a when statement was added:

- name: add vhost sensu
  command: rabbitmqctl add_vhost /sensu
  when: rabbitmqctl list_vhosts | grep sensu

running the code resulted in:

fatal: [192.168.0.9]: FAILED! => {"failed": true, "msg": "The conditional
 check 'rabbitmqctl list_vhosts | grep sensu' failed. The error was: template
 error while templating string: expected token 'end of statement block', got
 'list_vhosts'. String: {% if rabbitmqctl list_vhosts | grep sensu %} True {%
 else %} False {% endif %}\n\nThe error appears to have been in '/etc/ansible
/roles/ansible-rabbitmq/tasks/main.yml': line 10, column 3, but may\nbe
 elsewhere in the file depending on the exact syntax problem.\n\nThe
 offending line appears to be:\n\n\n- name: add vhost sensu\n  ^ here\n"}
  1. First of all, imagine that when was successful then the command will not run and then it looks more like onlyif in Puppet.
  2. Secondly, if the when would be successful should an escalation mark be used in order to simulate an unless?
  3. Use of register. What if that file is lost or the vhost has been removed by for example a human? Puppet's unless always executes the commands so that it is clear whether the command needs to be executed.

已采纳解答

原帖链接 · jscott · CC BY-SA 3.0

I think what you're looking do is this:

- name: get vhosts
  command: rabbitmqctl list_vhosts
  register: vhosts
  changed_when: false

- name: add vhost sensu
  command: rabbitmqctl add_vhost /sensu
  when: "'/sensu' not in vhosts.stdout"

Re: #3 register does not create a file. If you're capturing the output of rabbitmqctl list_vhosts via register, the contents will be as valid as the system's current state.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。