小编Ken*_*ney的帖子

对 Terraform 状态保密

我试图避免在 Terraform 状态下拥有秘密。

是否有更好的方法从 Secrets Manager 中的密钥设置 RDS 密码来执行此操作?

resource "null_resource" "master_password" {
  triggers = {
    db_host = module.myrdsdatabase.cluster_id
  }

  provisioner "local-exec" {
    command = <<TOF
    password=$(aws secretsmanager get-secret-value --secret-id myrdscreds | jq '.SecretString | fromjson | .password' | tr -d '"')
    aws rds modify-db-cluster --db-cluster-identifier ${module.myrdsdatabase.cluster_id} --master-user-password $password --apply-immediately
    TOF

    interpreter = ["bash", "-c"]
  }
}
Run Code Online (Sandbox Code Playgroud)

terraform

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

Python Boto3 分页错误:“PageIterator”对象不可下标

我正在尝试获取某个区域中可用实例的完整列表。该代码将迭代多个页面,但会因错误而停止:

Traceback (most recent call last):
  File "list_available_instance_offerings.py", line 29, in <module>
    marker = page_iterator['Marker']
TypeError: 'PageIterator' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

如何迭代所有页面而不会过早出错?

这是我的脚本:

import sys
import boto3

ec2 = boto3.client("ec2")
marker = None
while True:
    paginator = ec2.get_paginator('describe_instance_type_offerings')
    page_iterator = paginator.paginate(
        LocationType='availability-zone',Filters=[{'Name': 'location', 'Values':['us-east-1a']}],
        PaginationConfig={
            'PageSize': 50,
            'StartingToken': marker})
    for page in page_iterator:
        offerings = page['InstanceTypeOfferings']
        for offer in offerings:
            print(offer['InstanceType'])
    try:
        marker = page_iterator['Marker']
    except KeyError:
        sys.exit()
Run Code Online (Sandbox Code Playgroud)

python boto3

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

Ansible 测试变量以什么开头

我需要能够安装一个 MySQL 库。Python 有 1 个用于 v2 的包和另一个用于 v3 的包。我需要能够告诉 Ansible 安装哪个包。

- name: Ensure MySQL-python is installed
  pip:
    name: MySQL-python
    state: present
  become: true
  when: python_version is regex("^2.*")

- name: Ensure mysqlclient is installed
  pip:
    name: mysqlclient
    state: present
  become: true
  when: python_version is regex("^3.*")
Run Code Online (Sandbox Code Playgroud)

正则表达式是有效的,但 Ansible 正在跳过它们,即使这样:

- debug:
    var: python_version
Run Code Online (Sandbox Code Playgroud)

返回这个:

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "python_version": "2.7.10"
}
Run Code Online (Sandbox Code Playgroud)

ansible

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

Python正则表达式匹配特殊字符

我需要一个可以测试字符串中是否有特殊字符的函数。我目前正在使用以下功能,但没有运气:

import re

def no_special_characters(s, pat=re.compile('[@_!#$%^&*()<>?/\|}{~:]')):
  if pat.match(s):
    print(s + " has special characters")
  else:
    print(s + " has NO special characters")
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

no_special_characters('$@')  # $@ has special characters
no_special_characters('a$@') # a$@ has NO special characters
no_special_characters('$@a') # $@a has special characters
Run Code Online (Sandbox Code Playgroud)

这对我来说没有任何意义。如何测试字符串中的任何特殊字符?

python regex

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

标签 统计

python ×2

ansible ×1

boto3 ×1

regex ×1

terraform ×1