AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-455822

Lethargos's questions

Martin Hope
Lethargos
Asked: 2021-04-06 23:48:22 +0800 CST

ansible - docker 容器中环境变量的模板 json

  • 0

我正在尝试将单行 json 字符串分配给 docker 容器中的环境变量。这是json的样子:

{"ip_access": {"IP_whitelist": {"192.168.99.19/32": "grafana/status (Provider)"}}, "vhosts": {"prometheus1": {"dns_names": ["prometheus1.company.internal"], "add_lines_443": ["include IP_whitelist;", "set $prometheus http://prometheus:9090;", "location / { proxy_pass $prometheus; }"], "options": {"cert_path": "/etc/ssl/certs/prometheus1.crt", "key_path": "/etc/ssl/private/prometheus1.key"}}}}

因此,["prometheus1.company.internal"]我不想拥有["{{ inventory_hostname }} .company.internal"](以及其他prometheus1.

我正在使用 docker_container 如下:

- name: create nginx reverse proxy container
  docker_container:
    image: registry.company.com/devops/nginx-reverseproxy:{{ nginx_version }}
    name: nginx-reverseproxy
    labels:
      role=metrics
    volumes:
      - /etc/ssl/certs/{{ inventory_hostname }}.crt:/etc/ssl/certs/{{ inventory_hostname }}.crt
      - /etc/ssl/private/{{ inventory_hostname }}.key:/etc/ssl/private/{{ inventory_hostname }}.key
    container_default_behavior: compatibility
    networks_cli_compatible: yes
    network_mode: default
    purge_networks: yes
    networks:
      - name: metrics-net
      - name: proxy-net
    env:
      STAGING_ENVIRONMENT: 'production'
      NGINX_VHOSTS_JSON: '{{ lookup("template", "rproxy/nginx_vhosts_prometheus_develop.j2") }}'

不幸的是,我不断得到:

TASK [prometheus : create nginx reverse proxy container] **********************************************
fatal: [prometheus_vag]: FAILED! => {"changed": false, "msg": "Non-string value found for env option. Ambiguous env options must be wrapped in quotes to avoid them being interpreted. Key: NGINX_VHOSTS_JSON"}

奇怪的是,如果我只是使用模板模块,它会按预期工作:

  template:
    src: rproxy/nginx_vhosts_prometheus_develop.j2
    dest: /tmp/tempo.json
  when: "prometheus_host in inventory_hostname"
  tags:
    - copytmp

inventory_hostname将被库存中的实际价值取代,我得到了正确的东西。此外,如果我将这个确切的结果作为 yaml playbook 中 NGINX_VHOSTS_JSON 的值粘贴到 playbook 中,它也可以正常工作。

但是查找模板似乎没有提供预期的字符串。

有什么想法可以解决这个问题吗?

json ansible jinja2
  • 2 个回答
  • 811 Views
Martin Hope
Lethargos
Asked: 2021-03-28 10:38:51 +0800 CST

使用 ansible 模板文件以获取不同主机的不同变量部分(通过其索引)

  • 0

我正在尝试将证书分发到其相应的主机(我将仅给出私钥任务的示例):

- name: create certificate private key
  community.crypto.openssl_privatekey:
    path: "/root/client/{{ item }}.key"
    type: Ed25519
    backup: yes
    return_content: yes
  register: privatekey
  loop: "{{ ansible_play_hosts_all }}"
  when: "'prometheus1' in inventory_hostname"

我可以像这样为其他主机调用变量:

{{ hostvars['prometheus1']['privatekey']['results'][0]['privatekey'] }}

索引指向某个键,因此 0 将是第一个主机 ( prometheus1),1 将是第二个主机,依此类推。

我想模板化是要走的路,但我根本不知道如何编写模板。我认为ansible_play_hosts_all是解决方案的关键,因为它的索引对应于私钥的索引,例如: ansible_play_hosts_all[2]-->hostvars['prometheus1']['privatekey']['results'][2]['privatekey']

但逻辑是:

for i in index of ansible_play_hosts_all
add the  hostvars['prometheus1']['privatekey']['results'][i]['privatekey']
if ansible_play_hosts_all[i] in inventory_hostname

我想有这样的效果:) 非常感谢任何帮助。


更新

也许更准确一些:

{% for i in ansible_play_hosts_all|length) %}
{{ hostvars['prometheus1']['privatekey']['results'][i]['privatekey'] }}
{% endfor %}

并在其中添加条件:

{% if ansible_play_hosts_all[i] in inventory_hostname %}
ansible template jinja2
  • 3 个回答
  • 401 Views
Martin Hope
Lethargos
Asked: 2021-03-27 06:04:28 +0800 CST

将注册变量的内容复制到其他主机

  • 1

我正在运行以下任务(我将只在整个角色中粘贴相关的问题):

- name: create certificate private key
  community.crypto.openssl_privatekey:
    path: "/root/client/{{ item }}.key"
    type: Ed25519
    backup: yes
    return_content: yes
  register: privatekey
  loop: "{{ ansible_play_hosts_all }}"
  when: "'prometheus1' in inventory_hostname"

我正在尝试将变量的内容复制到另一台主机。

- name: copy private key to host
  copy:
    content: "{{ privatekey }}"
    dest: /root/private_key_from_var.key

(此时我并不担心过滤或将密钥分发给相应的主机 - 只是想了解为什么内容本身没有被复制。

奇怪的是,它运行的唯一主机是“prometheus1”,我实际上正在运行生成密钥的任务。private_key_from_var.key在那里,我得到了一个包含我期望的所有内容的生成文件。

其他两台主机上完全相同的文件只是来自 ansible 本身的错误报告:

{"results": [{"changed": false, "skipped": true, "skip_reason": "Conditional result was False", "item": "prometheus1", "ansible_loop_var": "item"}, {"changed": false, "skipped": true, "skip_reason": "Conditional result was False", "item": "breitband", "ansible_loop_var": "item"}, {"changed": false, "skipped": true, "skip_reason": "Conditional result was False", "item": "status1", "ansible_loop_var": "item"}], "msg": "All items completed", "changed": false}

我不确定为什么我无法在任何我希望的主机上复制此内容,但这很奇怪。我也尝试使用模板并直接在 jinja 文件中调用变量,但无济于事。

有任何想法吗?:)

ansible
  • 1 个回答
  • 56 Views
Martin Hope
Lethargos
Asked: 2018-10-03 09:39:35 +0800 CST

wordpress ERR_TOO_MANY_REDIRECTS 通过清漆,但不通过 apache

  • 1

我在使用 varnish-wordpress 设置时遇到了这个奇怪的问题。当我尝试通过清漆转移流量时,我得到了 TOO_MANY_REDIRECTS,但是如果我直接配置 apache 并使用一个在 443 上监听的虚拟主机,它就可以正常工作。清漆中 default.vcl 的相关部分:

    if (std.port(local.ip) == 80 && req.http.host ~ "example.com(:[0-9]+)?$") {
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return(synth(850, "Moved permanently"));

    [...]


   else if (req.http.host ~ "(www\.)?example.com(:[0-9]+)?$") {
    set req.backend_hint = web227;
        return (pipe);
        }
sub vcl_synth {
    if (resp.status == 850) {
        set resp.http.Location = req.http.x-redir;
        set resp.status = 301;
        return (deliver);
    }

我暂时使用管道,以确保在我仍在测试的这个阶段不会遇到更多缓存。

在这个清漆上,有几个 wordpress 后端可以正常工作,相同的 .htaccess 驻留在其上,“经典”wordpress .htaccess:

    <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

这是我的 apache 站点配置:

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName  digital.ringier.ro
        ErrorLog "/var/log/apache2/example.com.error.log"
        CustomLog "/var/log/apache2/example.com.access.log" common
        #Redirect / https://example.com/
        DocumentRoot "/var/www/example.com"
        <Directory "/var/www/example.com">
        AllowOverride all
        Options -Indexes +FollowSymLinks +MultiViews
        </Directory>
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin [email protected]
        ServerName  example.com
        DocumentRoot "/var/www/example.com"
        <Directory "/var/www/example.com">
        AllowOverride all
        Options -Indexes +FollowSymLinks +MultiViews
        </Directory>
        ErrorLog "/var/log/apache2/example.com.error.log"
        CustomLog "/var/log/apache2/example.com.access.log" common
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>

对于我的生活,我无法理解为什么当我直接访问 apache 时它会起作用(我正在更改主机中的 ip 以做到这一点),但不是通过清漆。这是我在浏览器中得到的响应(重复了几十次):

HTTP/1.1 301 Moved Permanently
Date: Tue, 02 Oct 2018 17:36:28 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://example.com/
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

据我所知,这显然是来自 apache 的响应,而不是来自 varnish 的响应(因为“服务器”标头和“通过”标头丢失)有什么想法可以解决这个问题吗?

apache-2.4
  • 3 个回答
  • 1462 Views
Martin Hope
Lethargos
Asked: 2018-08-03 03:03:17 +0800 CST

在 varnishlog 中记录 5xx 错误,但不高于“599”

  • 1

在互联网上搜索后,我找到了识别 5xx 状态码的解决方案:

varnishlog -q "RespStatus >=500"

问题在于它还包括内部代码,例如 750 或 850,我不需要这些,因为它们没有问题。我也尝试了以下方法,但无济于事:

varnishlog -q "RespStatus >=500 和 RespStatus <599"

varnish
  • 1 个回答
  • 362 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve