标签: conditional

有没有办法检查 ansible 任务中未定义字典键?

所以在我的代码中我有一个任务

- name: cool task
  shell: 'touch iamnotcool.txt'
  when: me.cool is not defined
Run Code Online (Sandbox Code Playgroud)

我的 vars 看起来像

---
me:
  stumped: yes
Run Code Online (Sandbox Code Playgroud)

因此,当我运行任务时,它会返回以下错误

{"failed": true, "msg": "The conditional check 'me.cool' failed. The error was: error while evaluating conditional (me.cool): 'dict object' has no attribute 'cool'.
Run Code Online (Sandbox Code Playgroud)

conditional ansible ansible-playbook

31
推荐指数
2
解决办法
6万
查看次数

如何在 Nginx 位置块中正确编写“或”语句?

我在 CentOS 7 上使用 Nginx。我想为以特定扩展名结尾或在 URL 中包含“/image/”字符串的文件添加缓存控制标头。我试过这个

  location / {
    proxy_pass http://scale; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    if ($request_uri ~* ".(ico|gif|jpe?g|png)$") | ($request_uri ~* "/image/") {
      expires 60d;
      access_log off;
      add_header Pragma public;
      add_header Cache-Control "public";
      break;
    }
  }
Run Code Online (Sandbox Code Playgroud)

但重新启动服务器后,我得到了

invalid condition "$request_uri" in /etc/nginx/conf.d/scale.conf:16
Run Code Online (Sandbox Code Playgroud)

错误。如果我从有问题的行中删除“ | ($request_uri ~* “/image/”)”,一切都会重新启动,但随后我无法匹配我想要的东西。如何在配置文件中写入正确的 or 语句?

configuration nginx cache conditional

8
推荐指数
1
解决办法
2万
查看次数

有人可以解释nginx条件标志吗?(-f、-d、-e 等)

我正在从 Apache 迁移到 nginx 并使用http://winginx.com/en/htaccess迁移重写规则。我试图理解翻译的意思,但我找不到任何关于它的文档。下面是一个例子:

if (-e $request_filename){
    rewrite ^(.*)$ /index.php break;
}
Run Code Online (Sandbox Code Playgroud)

相信 -f检查文件是否存在,并-d检查目录是否存在,但是呢-e?我在哪里可以在文档中找到这些?谢谢!

configuration nginx flags conditional

7
推荐指数
1
解决办法
6953
查看次数

基于其他角色的 Ansible 条件语句?

我在 Ansible 部署中有以下场景:

  • hostX 具有角色common-mta和其他一些角色
  • hostY 有角色common-mta也有角色package-postfix

common-mta角色拷贝一个文件(如果你能猜到,它的/etc/postfix/main.cf),但我想基于主机是否有限制这个角色package-postfix,而不是仅仅通过主机限制像我现在有,例如:

when: not group_names == "mail"
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的:

when: not role_names == "package-postfix"
Run Code Online (Sandbox Code Playgroud)

我认为这会更清晰,并且允许我随意将package-postfix角色添加到其他主机,而无需修改common-mta角色。

这可能吗?

编辑:Navern 的建议不适用于此特定用例;角色列表独立于组列表,虽然我可以通过组执行此操作,但它更脏。考虑一下,这是我的实际任务线:

- name: install postfix generic config if we aren't an mx, mail, or luna host
  template: src=main.cf.j2 dest=/etc/postfix/main.cf
  when: "'luna' not in group_names and 'mx' not in group_names and 'mail' not in group_names"
Run Code Online (Sandbox Code Playgroud)

我想将其替换为:

- name: install postfix generic config …
Run Code Online (Sandbox Code Playgroud)

linux conditional ansible

6
推荐指数
1
解决办法
6091
查看次数

Nginx 中不可能有条件标头吗?

我目前正在尝试使用 Nginx 有条件地返回一组 CORS 标头。起初,这似乎是一个简单的任务,因为我已经有了这个工作配置:

upstream api-app {
  server unix:/tmp/api-app.sock fail_timeout=0;
}

server {
  listen 80;
  # [other server stuff...]

  # CORS headers added to all ALL responses of this server (needed for all requests)
  more_set_headers 'Access-Control-Allow-Methods: GET,POST,PATCH,PUT,DELETE,OPTIONS';
  more_set_headers 'Access-Control-Allow-Origin: *';
  more_set_headers 'Access-Control-Allow-Credentials: true';
  more_set_headers 'Access-Control-Request-Method: GET,POST,PATCH,PUT,DELETE,OPTIONS';
  more_set_headers 'Access-Control-Request-Headers: Content-Type';
  more_set_headers 'Access-Control-Allow-Headers: Origin,X-Requested-With,Content-Type,Accept,Session-Id,Role-Id,Visitor-Id,X-Window-Location';
  more_set_headers 'Access-Control-Expose-Headers: X-Total-Entries,X-Total-Pages,X-Page,X-Per-Page,X-Folder-Hierarchy';

  location / {
    # For OPTIONS request only return above headers and no content (also allow caching them)
    if ($request_method = 'OPTIONS') {
      # …
Run Code Online (Sandbox Code Playgroud)

nginx conditional cors

5
推荐指数
1
解决办法
2136
查看次数

linux bash中的条件代码块

几乎每个人都知道非常有用的 && 和 || 运算符,例如:

rm myf && echo "File is removed successfully" || echo "File is not removed"
Run Code Online (Sandbox Code Playgroud)

我有一个问题:如何在 && 或 || 之后放置一个命令块 不使用函数的运算符?

例如我想做:

rm myf && \
  echo "File is removed successfully" \
  echo "another command executed when rm was successful" || \
  echo "File is not removed" \
  echo "another command executed when rm was NOT successful"
Run Code Online (Sandbox Code Playgroud)

该脚本的正确语法是什么?

bash conditional

4
推荐指数
1
解决办法
4739
查看次数

基于端口的Apache2条件段

我不太了解 apache 配置中的 if 语句,我想知道是否可以仅在某个端口收到请求时才应用一部分配置。

简而言之,这是关于 SSL。我有基于名称的虚拟主机,我可以为端口 80 进行配置,然后为端口 443 复制所有内容,并添加相关的 SSL 配置。

但这似乎是多余的。我想知道我是否可以拥有类似的东西:

<VirtualHost *:80 *:443>
Run Code Online (Sandbox Code Playgroud)

然后我可以放:

<IfModule mod_ssl.c>
    SSLEngine on
SSLCertificateFile ...
SSLCertificateKeyFile ...
SSLCACertificateFile ...
</IfModule>
Run Code Online (Sandbox Code Playgroud)

在检查连接是否在端口 443 上的 if 语句中......或者这样的事情是不可能的?服务器支持 SNI,我对非 SNI 兼容的浏览器没有任何担心。

ssl virtualhost sni conditional apache-2.2

4
推荐指数
1
解决办法
2397
查看次数

需要检查是否需要重新启动以进行内核更新?

我正在使用 Ansible 检查主机,如果/vmlinuz内核没有解析为uname -r.

if条件是,虽然总是identifing即使试验机已经重新启动,重新启动和内核解析为相同的内核:

if [ $(readlink -f /vmlinuz) != /boot/vmlinuz-$(uname -r) ]; 然后回声'重启'; 否则回声'不'; 菲

- name: Check for reboot hint.
  shell: if [ $(readlink -f /vmlinuz) != /boot/vmlinuz-$(uname -r) ]; then echo 'reboot'; else echo 'no'; fi
  ignore_errors: true
  register: reboot_hint

- name: Rebooting ...
  command: shutdown -r now "Ansible kernel update applied"
  async: 0
  poll: 0
  ignore_errors: true
  when: kernelup|changed or reboot_hint.stdout.find("reboot") != -1
  register: rebooting

- name: Wait for …
Run Code Online (Sandbox Code Playgroud)

centos conditional ansible

4
推荐指数
1
解决办法
3717
查看次数

bash if 语句在 crontab 作业中出现意外行为

这是脚本

bash --version | head -n1
if [ "$1" == "now"  ]
then
    echo if now
    execut job
else
    echo else "_"$1"_"  # make sure we are not picking any spaces
    if [ condition  ]
    then
    execut something else
    fi
fi

./script now
if now
Run Code Online (Sandbox Code Playgroud)

如果从交互式 shell 运行,则按预期工作。但是,如果从 cron as 调用,则 if 会转到 else 块

* * * * *   root    /home/user/./script now >> /tmp/log
cat /tmp/log
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
else _now_
Run Code Online (Sandbox Code Playgroud)

也与“-eq”相同。

我是否因为没有吃早餐而错过了一些非常简单的事情?

运行 Ubuntu 14.04LTS。

linux shell bash cron conditional

4
推荐指数
1
解决办法
1215
查看次数

Apache:仅在尚未设置时才设置全局标头

我在这里遇到了一些烦人的情况。

我有一台由 plesk 管理的服务器。在 /etc/apache2/apache2.conf 中,会发生基于 mod_headers 和 mod_expires 的配置。例如:

<filesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
</filesMatch>
Run Code Online (Sandbox Code Playgroud)

这工作得很好。

我的某些应用程序有问题。如果需要,我希望应用程序保持对缓存的控制。目前这是不可能的,因为标头设置在将响应返回给客户端之前立即发生,这意味着在应用程序完成所有操作之后。应用程序设置的标头将被覆盖。

Header merge Cache-Controll...
Run Code Online (Sandbox Code Playgroud)

不工作,因为它只是附加给定的标题字符串。而且我怀疑浏览器是否会像我希望的那样处理这样的标题:

Cache-Control max-age=0, no-cache, no-store, must-revalidate, max-age=604800, public
Run Code Online (Sandbox Code Playgroud)

我现在的问题是:是否有可能仅在特定标题“字段”中绝对没有内容时才设置标题?我不希望我的应用程序设置任何环境变量,因为不应更改应用程序以满足一台服务器的需求。

我还尝试过 - 仅用于测试目的 - 通过 vhost.conf 为特定域取消设置标头。但是它会在 apache2.conf 中的头设置之前执行,这意味着它们也会被覆盖。

conditional apache-2.2

3
推荐指数
1
解决办法
6063
查看次数

写一个apache配置,条件基于apache版本

我想为 apache 服务器编写一个配置,它应该适用于 apache-2.2 和 -2.4 而无需更改。由于 v2.4 中有很多必须加载的新模块,以及一些更改的指令,我需要一些条件语句。我想要的是类似的东西

<If ApacheVersion >= 2.4>
  LoadModule mod_only_in_2.4.so
</If>
Run Code Online (Sandbox Code Playgroud)

但我还没有找到正确的表达方式。有人可以指出我正确的方向吗?

澄清我的动机:我正在开发一个服务器,我希望能够将其复制到服务器上并在没有进一步依赖主机的配置的情况下运行。而且由于机器可以有任一版本的 apache,我想要一个在这两种情况下都可以使用的配置。

configuration web-server conditional apache-2.2 apache-2.4

2
推荐指数
2
解决办法
1740
查看次数

Ansible:尝试将条件放在 vars 文件上

我正在开发一个项目,该项目将使用许多设备,这些设备需要配置相同的播放,但不同的变量。我有一个 vars 文件,其中包含许多库,每个设备一个。库名称与设备名称匹配,例如:

(设备 1)

- {xxx: "AAA", yyy: "BBB", zzz: "CCC"}
- {xxx: "AAA", yyy: "BBB", zzz: "CCC"}
Run Code Online (Sandbox Code Playgroud)

(设备 2)

- {xxx: "AAA", yyy: "BBB", zzz: "CCC"}
- {xxx: "AAA", yyy: "BBB", zzz: "CCC"}
Run Code Online (Sandbox Code Playgroud)

该剧本身应该找出哪个库与设备匹配,使用该库的变量而不使用其他变量。我目前的尝试是这样完成的:

- name: do this
  command: "do some of item.xxx with some item.zzz
- include_vars: device-library.yaml
  when: "{{item}} == {{ansible_hostname}}"
Run Code Online (Sandbox Code Playgroud)

但我总是得到:

致命:[xxx.xxx.xxx.xxx]:失败!=> {"failed": true, "msg": "ERROR!'item' 未定义"}

当这个项目只是一台设备时,我们with_items: stuff.yaml一直在使用,而且效果很好。

我的问题是:我必须用什么来代替{{item}},还是有更好的方法?

library conditional ansible

1
推荐指数
1
解决办法
1365
查看次数

SaltStack:如果 /var/log/backup 存在,则执行 x

我想执行这个状态:

backup_log_readable:
  acl.present:
    - name: /var/log/backup
    - acl_type: user
    - acl_name: monitor
    - perms: rx
    - recurse: True
Run Code Online (Sandbox Code Playgroud)

但这只有在/var/log/backup存在时才会发生。

如何在盐中做到这样的条件?

conditional saltstack

0
推荐指数
1
解决办法
8538
查看次数