小编Mos*_*she的帖子

Jinja2模板中的子串

使用ansible进行部署时,我需要删除一个尾随-p子字符串的特定情况.

该字符串somemachine-prod-p应该成为somemachine-prod只有在-p很底.

我看到我可以使用Jinja的子串函数不能满足我的需要,因为我需要去除字符串的结尾,而不是开头.

想法?

substring jinja2 ansible

18
推荐指数
3
解决办法
3万
查看次数

Active NavLink到父元素

我正在使用React Router v4,我有一个案例,在我的导航链接上,我想将activeclassName 启用到NavLink父元素,而不是NavLink自身.

有没有办法访问路径(match)即使我不在Switch元素内?

或者我必须保持状态?因为我觉得它有点缺少路由器的想法.

这是我的例子,我想将activeclassName应用于li元素而不是NavLink:

const {
  HashRouter,
  Switch,
  Route,
  Link,
  NavLink,
} = ReactRouterDOM

const About = () => (
    <article>
        My name is Moshe and I'm learning React and React Router v4.
    </article>
);

const Page = () => (
    <Switch>
      <Route exact path='/'  render={() => <h1>Welcome!</h1>} />
      <Route path='/about' component={About}/>
    </Switch>
);

const Nav = () => (
    <nav> …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router

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

Jquery - 将Ajax jSON响应存储为变量

我正在尝试获取ajax请求的结果以设置在我可以在该请求之外访问的变量中.我已经尝试过这个JQuery - 将ajax响应存储到全局变量中但我的变量beer在函数$.getJSON$.ajax函数之外仍未定义(我尝试了两者).

这是我的代码,我可以从中看到结果console.log(beer).

var beer;
$.getJSON(jsonUrl, function (json) {
    beer = json;
    console.log(beer); // returns beer
});
console.log(beer); // returns undefined

var beer = (function () {
    var result;

    $.ajax({
        url: jsonUrl,
        success: function (data) {
            result = data;
            console.log(beer); // returns beer

        }
    });
    console.log(result); // returns undefined
    if (result) return result;
})();
console.log(beer); // returns undefined
Run Code Online (Sandbox Code Playgroud)

variables ajax jquery json

14
推荐指数
2
解决办法
4万
查看次数

在ansible playbook中Concat多个变量和字符串

with_items为目标部分执行预处理时,我正在尝试多个连接.

现在它看起来像这样:

- name: create app except+lookup
  copy: content="" dest="{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}" force=no group=devops owner=devops mode: 0755
  with_items:
...
Run Code Online (Sandbox Code Playgroud)

我明白了:

We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they 
start a value. For instance:            

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"
Run Code Online (Sandbox Code Playgroud)

尝试了几种方法,但没有一种方法可以实现.

是否可以用字符串连接变量?

yaml concatenation ansible

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

Terraform 在 for_each 上获取列表索引

Terraform新手在这里。我想使用 迭代列表for_each,但键和值似乎相同:

provider "aws" {
  profile = "default"
  region  = "us-east-1"
}

variable "vpc_cidrs" {
  default = ["10.0.0.0/16", "10.1.0.0/16"]
}

resource "aws_vpc" "vpc" {
  for_each             = toset(var.vpc_cidrs)
  cidr_block           = each.value
  enable_dns_hostnames = true
  tags                 = { Name = "Company0${each.key}" }
}
Run Code Online (Sandbox Code Playgroud)

我想的标签名称是"Name" = "Company01""Name" = "Company02",但根据terraform apply我得到: "Name" = "Company010.0.0.0/16""Name" = "Company010.1.0.0/16" 我缺少什么?

terraform

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

未处理的承诺拒绝,即使我确实处理了它

我将一个request-promise-native调用包装在一个返回承诺的函数中。

import request from 'request-promise-native';
function requestWrapper(url) {
    return request(url)
        .then(data => {...})
        .catch(err => Promise.reject(err));
}
Run Code Online (Sandbox Code Playgroud)

简单吧?

现在我正在使用这个函数,并且then工作正常,但catch从未捕获Promise.reject

requestWrapper('http://some-url.com')
    .then(data => {...})
    .catch(err => console.log(err));
Run Code Online (Sandbox Code Playgroud)

我从来没有接过电话catchcatch如果我将of中的 return 语句更改为requestWrapper

.catch(err => err)
Run Code Online (Sandbox Code Playgroud)

甚至这个:

.catch(err => Promise.resolve(err))
Run Code Online (Sandbox Code Playgroud)

要返回 a ,我按照预期得到了调用的错误resolve堆栈。thenrequestWrapper

节点喊道:

(node:24260) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ...
Run Code Online (Sandbox Code Playgroud)

javascript request promise

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

祖父母的第一个孩子

我在div中有多个div.我希望父div的第一个h1加下划线.first-child的问题是它查看了父div,在我的例子中是父错误.例如:

        <div class="box">
            <div><h1>first h1 needs to be underlined</h1></div>
            <div>bla bla bla</div>
            <div>bla bla bla2</div>
            <div><h1>big bla bla bla but not underlined</h1></div>
        </div>
Run Code Online (Sandbox Code Playgroud)

我只希望第一个h1加下划线但是使用.box h1:firstchild会导致h1加下划线.救命?

html css

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

将命令的结果作为参数

我想得到结果,rpm -qa | grep something然后运行我得到的结果rpm -ql result-i-got,并在一行中.

我发现这个工作:

rpm -ql $(rpm -qa | grep something)
Run Code Online (Sandbox Code Playgroud)

我想知道,有没有更好的甚至不同的方法来管理这个结果,以获得与我上面写的相同的结果?谢谢.

linux io bash centos pipe

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

Jenkins Pipeline waitUntil bash 命令返回特定字符串

我有一个管道阶段,我等待从sh脚本中获取某个字符串,并且只有当字符串匹配时,才继续到下一阶段,但是,它没有按预期工作:

node('master') {
    stage("wait for bash completion") {
        waitUntil {
            def output = sh returnStdout: true, script: 'cat /tmp/test.txt'
            output == "hello"
        }
    }
    stage("execute after bash completed") {
        echo "the file says hello!!!"
    }
}
Run Code Online (Sandbox Code Playgroud)

执行是这样的:

+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.25 sec
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ cat /tmp/test.txt
[Pipeline] }
Will try again after 0.3 sec
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ cat /tmp/test.txt …
Run Code Online (Sandbox Code Playgroud)

jenkins jenkins-groovy jenkins-pipeline

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

Define a variable in Helm template

I need to define a variable based on an if statement and use that variable multiple times. In order not to repeat the if I tried something like this:

{{ if condition}}
    {{ $my_val = "http" }}
{{ else }}
    {{ $my_val = "https" }}
{{ end }}
{{ $my_val }}://google.com
Run Code Online (Sandbox Code Playgroud)

However this returns an error:

Error: render error in "templates/deployment.yaml":
template: templates/deployment.yaml:30:28:
executing "templates/deployment.yaml" at
<include (print $.Template.BasePath "/config.yaml") .>: error calling
include: template: templates/config.yaml:175:59:
executing "templates/config.yaml" at <"https">: …
Run Code Online (Sandbox Code Playgroud)

go-templates kubernetes-helm

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

Gitlab-CI 多行脚本和多脚本的区别

GitLab-CI 的新手,我正在尝试通过 Helm 在 Kubernetes 集群上进行部署。我有这个:

image: docker:stable

deploy:
  stage: deploy
  image:
    name: alpine/helm:3.4.1
  script:
    - echo "Deploying to production"
    - helm list --all-namespaces
Run Code Online (Sandbox Code Playgroud)

它失败了:

Error: unknown command "sh" for "helm"
Run Code Online (Sandbox Code Playgroud)

并且echo没有回显,但是,如果我要删除该echo行,则helmcmd 将成功执行。

我想我在问如何使多行脚本运行?gitlab-runner 如何执行script:数组中提供的一系列命令?

continuous-deployment gitlab gitlab-ci kubernetes kubernetes-helm

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

在循环中注册多个变量

我有一个可变的 yaml 文件:

---
apps:
  - client
  - node
  - gateway
  - ic
  - consul
Run Code Online (Sandbox Code Playgroud)

和这个任务:

- name: register if apps exists
  stat: path="/etc/init.d/{{ item }}"
  with_items: apps
  register: "{{ item.stat.exists }}exists"
Run Code Online (Sandbox Code Playgroud)

无论文件是否存在,我都需要为每个应用程序创建一个值为 true 或 false 的变量:

clientexists = true
nodeexists = true
gatewayexists = false
icexists = true
consulexists = false
Run Code Online (Sandbox Code Playgroud)

出于某种原因,itemand existsconcat 不起作用。

我怎样才能做到这一点??

ansible

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

重新提出异常并不像预期的那样有效

我有这个相当复杂的try except块:

try:
  self.sorting = sys.argv[1]
  try:
    test_sorting_var = int(self.sorting)
    if test_sorting_var < 1:
      print "Sorting column number not valid."
      raise ValueError
    else:
      self.sorting = test_sorting_var
  except ValueError:
    print "There's a problem with the sorting value provided"
    print "Either the column doesn't exists or the column number is invalid"
    print "Please try a different sorting value or omit it."
    sys.exit(1)
  except:
    if self.sorting not in self.output_table.column_headers:
      print "Sorting column name not valid."
      raise ValueError
except:
  pass
Run Code Online (Sandbox Code Playgroud)

基本上我正在检查:

  1. 如果有的话 …

python

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