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
    • 最新
    • 标签
主页 / server / 问题

问题[variables](server)

Martin Hope
TSG
Asked: 2021-09-12 10:13:38 +0800 CST

在 Ansible 中使用变量构造字典键

  • 0

我正在尝试更新主机变量中的字典,并且密钥的名称是使用变量(节点)构造的。例如,如果 'node' 为 1,那么我想更新 hostvars['fakehost']['mydict']['localaddress1']。这是我的代码:

- name: Read IPv4 of first interface
    add_host:
      name: "fakehost"
      telium: "{{ hostvars['fakehost']['mydict'] | combine ({ 'localaddress{{ node }}' : ansible_all_ipv4_addresses[0] }) }}"

我不知道如何用 ansible 抱怨语法来构造 localaddress{{node}} 。

variables ansible
  • 1 个回答
  • 1639 Views
Martin Hope
housemd
Asked: 2021-06-17 14:41:59 +0800 CST

Ansible 打印调试消息结果变量

  • 1

我有一个简单的任务,我无法克服。

我有一个返回 AWS EC2 实例配置的剧本。我只需要打印(显示)private_ip_address。

这是我的剧本

---
- hosts: local
  connection: local
  gather_facts: false
  become: yes
  become_method: enable

  tasks:

  - name: gather-info-ec2
    community.aws.ec2_instance_info:
      instance_ids:
        - i-XXXXXAAAAAA

    register: ec2

  - debug: msg="{{ ec2.instances.network_interfaces.private_ip_address }}"

当我像这样运行它时,我收到以下错误。

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'network_interfaces'\n\nThe error appears to be in '/etc/ansible/playbooks/AWSLinuxMigration/gather_ec2_info.yaml': line 16, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - debug: msg=\"{{ ec2.instances.network_interfaces.private_ip_address }}\"\n    ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

当我在没有 DEBUG 部分并使用-vvv的情况下执行它时,它会显示以下结果。如何提取并打印此地址?我把它缩短了一点,但你明白了

ok: [localhost] => {
    "msg": {
        "changed": false,
        "failed": false,
        "instances": [
            {
                "ami_launch_index": 0,
                "architecture": "x86_64",
                "block_device_mappings": [
                    {
                        "device_name": "/dev/xvda",
                        "ebs": {
                            "attach_time": "2020-04-15T16:11:19+00:00",
                            "delete_on_termination": true,
                            "status": "attached",
                            "volume_id": "xxxxxx"
                        }
                    }
                ],
                "capacity_reservation_specification": {
                    "capacity_reservation_preference": "open"
                },
                "client_token": "",
                "cpu_options": {
                    "core_count": 1,
                    "threads_per_core": 2
                },
                "ebs_optimized": true,
                "ena_support": true,
                "enclave_options": {
                    "enabled": false
                },
                "hibernation_options": {
                    "configured": false
                },
                "hypervisor": "xen",
                "iam_instance_profile": {
                    "arn": "xxxxxx",
                    "id": "xxxxxx"
                },
                "image_id": "xxxxx",
                "instance_id": "xxxxx",
                "instance_type": "t3.medium",
                "key_name": "xxxxx",
                "launch_time": "2021-04-21T00:01:25+00:00",
                "metadata_options": {
                    "http_endpoint": "enabled",
                    "http_put_response_hop_limit": 1,
                    "http_tokens": "optional",
                    "state": "applied"
                },
                "monitoring": {
                    "state": "disabled"
                },
                "network_interfaces": [
                    {
                        "association": {
                            "ip_owner_id": "xxxx",
                            "public_dns_name": "xxxxx",
                            "public_ip": "xxxx"
                        },
                        "attachment": {
                            "attach_time": "2020-04-15T16:11:18+00:00",
                            "attachment_id": "xxxxx",
                            "delete_on_termination": true,
                            "device_index": 0,
                            "network_card_index": 0,
                            "status": "attached"
                        },
                        "description": "Primary network interface",
                        "groups": [
                            {
                                "group_id": "xxxxx",
                                "group_name": "xxxxx"
                            }
                        ],
                        "interface_type": "interface",
                        "ipv6_addresses": [],
                        "mac_address": "xxxxx",
                        "network_interface_id": "xxxx",
                        "owner_id": "xxxxx",
                        "private_dns_name": "ip-10-0-1-161.ec2.internal",
                        "private_ip_address": "10.0.1.161",
                        "private_ip_addresses": [
                            {
                                "association": {
                                    "ip_owner_id": "xxxxx",
                                    "public_dns_name": "xxxx.compute-1.amazonaws.com",
                                    "public_ip": "2.2.2.2"
                                },
                                "primary": true,
                                "private_dns_name": "ip-333333.ec2.internal",
                                "private_ip_address": "1.1.1.1."
                            }
                        ],
                        "source_dest_check": true,
                        "status": "in-use"

                    }
                ]
        ]
    }
}
debug variables ansible ansible-playbook
  • 1 个回答
  • 2724 Views
Martin Hope
Maxime
Asked: 2021-02-15 07:14:59 +0800 CST

Bash脚本 - 检查一个变量是否位于其他两个变量之间?

  • 1

在 Bash 脚本中,我想检查 var1 是否位于 var2 和 var3 之间。但我找不到该怎么做。

像这样的东西。

var1=15
var2=10
var3=20

if [ "$var1" is located beetween "$var2" and "$var3" ]
then
    echo "ok"
else
    echo "not ok"
fi

你能帮我吗 ?

谢谢你。

linux bash variables shell-scripting conditional
  • 2 个回答
  • 286 Views
Martin Hope
Kevin Rutan
Asked: 2020-09-22 12:51:03 +0800 CST

Python 3 中 b' ' 中涵盖的变量

  • 0

我确信这是一个相当菜鸟的问题,我用谷歌搜索过,找不到直接的答案,但我可能问错了……我正在尝试制作一个开箱即用的配置脚本,以及所有需要的问题回答存储在一个名为 pass.ini 的文件中。当我在填充我的文件时从getstr(使用curses)获取用户输入时,它们都有b'variable string'作为它们的值。当我尝试执行剥离命令时,我得到 b'riable strin'。当我做一个 str(variable) 时,它会遇到同样的问题。我看到 b'<variable_string>' 可以表示它是字节码而不是解码的标志。所以我尝试了一个解码命令,但失败了,因为'str object has no attribute'decode'我让它通过ConfigParser写出来,并作为file.write写到一个单独的文件中。现在一切都被注释掉了,

这是信息收集模块:

        wrapper(CommitChanges)
            curses.echo()
            stdscr.addstr(  8, 19,  config.CIP , curses.color_pair(3) )
            config.CIP = stdscr.getstr(  8, 19, 15)
            stdscr.addstr(  9, 19,  config.CSM , curses.color_pair(3) )
            config.CSM = stdscr.getstr( 9, 19, 15)
            stdscr.addstr( 10, 19,  config.CGW , curses.color_pair(3) )
            config.CGW = stdscr.getstr(10, 19, 15)
            stdscr.addstr( 11, 19,  config.CD1 , curses.color_pair(3) ) 
            config.CD1 = stdscr.getstr(11, 19, 15)
            stdscr.addstr( 12, 19,  config.CD2 , curses.color_pair(3) )
            config.CD2 = stdscr.getstr(12, 19, 15)
            stdscr.addstr( 13, 19,  config.CNTP, curses.color_pair(3) )
            config.CNTP = stdscr.getstr(13, 19, 15)
            stdscr.addstr( 16, 19,  config.CHN , curses.color_pair(3) )
            config.CHN = stdscr.getstr(16, 19, 15)
            stdscr.addstr( 14, 19,  config.CID , curses.color_pair(3) )
            config.CID = stdscr.getstr(14, 19, 15)
            stdscr.addstr( 15, 19,  config.CS , curses.color_pair(3) )
            config.CS = stdscr.getstr(15, 19, 15)


这是文件输出模块

def CommitChanges():
    MOP = "X"
    Config['Array=all']['PTLIP']        = a
    Config['Array=all']['PTLSM']        = config.CSM.decode('utf-8')
    Config['Array=all']['PTLGW']        = config.CGW.decode('utf-8')  
    Config['Array=all']['PTLD1']        = config.CD1.decode('utf-8')
    Config['Array=all']['PTLD2']        = config.CD2.decode('utf-8') 
    Config['Array=all']['PTLNTP']       = config.CNTP.decode('utf-8') 
    Config['Array=all']['PTLIF']        = config.CIFN.decode('utf-8') 
    Config['Array=all']['PTLHSTNM']     = config.CHN.decode('utf-8') 
    Config['Array=all']['PTLMOB']       = config.CMOB.decode('utf-8')
    Config['Array=all']['customerid']   = config.CID.decode('utf-8')
    Config['Array=all']['site']         = config.CS.decode('utf-8')
    with open('/opt/passp/pass.ini', 'w') as passini:
        Config.write(passini, space_around_delimiters=False)
    tpass= open('./pass.b', 'w')
    tpass.write("[Array=All]"+ "\n")
    tpass.write("ptlip="+ a + "\n")
    tpass.write("ptlsm="+ config.CSM.decode('utf-8') +"\n")
    tpass.write("ptlgw="+ config.CGW.decode('utf-8') + "\n")
    tpass.write("ptld1="+ config.CD1.decode('utf-8') + "\n")
        tpass.write("ptld2="+ config.CD2.decode('utf-8') + "\n")
    tpass.write("ptlntp="+ config.CNTPdecode('utf-8') + "\n")
    tpass.write("ptlif="+ config.CIFNdecode('utf-8') + "\n")
    tpass.write("ptldhstnm="+ config.CHNdecode('utf-8') + "\n")
    tpass.write("ptlmob="+ config.CMOBdecode('utf-8') + "\n")
    tpass.write("customerid="+ config.CIDdecode('utf-8') + "\n")
    tpass.write("site="+ config.CSdecode('utf-8') + "\n")
    #if Backupfiles():
    textchanges()
    return

这是 ConfigParser 创建的文件保存输出

[Array=all]
ptlip=b'123'
ptlsm=b'321'
ptlgw=b'111'
ptld1=b'222'
ptld2=b'333'
ptlntp=b'444'
ptlif=s19
ptlhstnm=b'555'
ptlmob=
customerid=b'666'
site=b'777'

当我进行直接写入时它完全匹配(它们来自两次不同的运行,但即使有空数据它也有包装器。

有趣的是,'ptlif' 是通过查找接口名称收集的,它不是由用户输入处理的,因此它必须是 config.XXXX 变量的存储方式。


[Array=All]
ptlip=b''
ptlsm=b''
ptlgw=b''
ptld1=b''
ptld2=b''
ptlntp=b''
ptlif=s19
ptldhstnm=b''
ptlmob=
customerid=b''
site=b''
linux python variables strings
  • 1 个回答
  • 69 Views
Martin Hope
adrelanos
Asked: 2020-06-03 08:47:24 +0800 CST

nginx 变量的成本是多少?

  • 1

nginx 常见问题解答有没有一种正确的方法来使用 nginx 变量来缩短配置部分,将它们用作宏来使部分配置作为模板工作?)说(粗体是我的):

问:是否有适当的方法使用 nginx 变量来缩短配置部分,将它们用作宏以使部分配置作为模板工作?

A:变量不应该用作模板宏。变量在处理每个请求期间在运行时进行评估,因此与普通静态配置相比,它们的成本相当高。使用变量来存储静态字符串也是一个坏主意。相反,应该使用宏扩展和“包含”指令来更轻松地生成配置,并且可以使用外部工具来完成,例如 sed + make 或任何其他常见的模板机制。

add_header Content-Security-Policy例如,为了更好的可读性,我使用的是一个超长的:

set $CSP "default-src 'none'";

set $CSP "${CSP}; connect-src 'self'";

set $CSP "${CSP}; script-src 'self' https://*.domain.org 'unsafe-inline' 'unsafe-eval'";

set $CSP "${CSP}; style-src 'self' https://*.domain.org 'unsafe-inline'";

set $CSP "${CSP}; img-src 'self' data: https://*.domain.org";

set $CSP "${CSP}; font-src 'self' https://*.domain.org";

## CSP closing colon.
set $CSP "${CSP};";

add_header Content-Security-Policy "$CSP";

适当地使用变量会对 nginx 性能产生多大影响?是否有关于该主题的任何性能测试/研究?

performance configuration nginx variables
  • 2 个回答
  • 529 Views
Martin Hope
Phineas
Asked: 2020-03-17 17:41:55 +0800 CST

ssh : 用户和主机/host_ip 作为变量

  • 0

我想用变量创建一个数组$user并将$host它们传递给ssh命令。

我努力了:

my_array=('something1' 'something2' 'user' 'host_ip')
sudo ssh ${my_array[2]}@${my_array[3]}

my_array=('something1' 'something2' 'user' 'host_ip')
sudo ssh host -l ${my_array[2]}  ## here host is not a variable

my_array=('something1' 'something2' 'username' 'host_ip')
my_var="${my_array[2]}@${my_array[3]}"
sudo ssh $my_var

在所有情况下,我都会被询问本地用户密码(因为sudo)并且我的答案被接受。当我输入远程用户密码时,我得到以下输出,我可以尝试 agian:

Permission denied, please try again.

笔记:

  • 我使用 IP 作为主机
  • 我的密码是正确的,当我不使用变量(sudo ssh user@host)时它正在工作。
  • 我最近安装了 sshpass 包

非常感谢!

编辑

出于某种原因,第二天所有上述和下面的解决方案都奏效了......有人可以解释一下吗?

ssh variables
  • 2 个回答
  • 515 Views
Martin Hope
Brent
Asked: 2009-05-13 09:54:48 +0800 CST

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

  • 920

确定 bash 中的变量是否为空(“”)的最佳方法是什么?

我听说建议我这样做if [ "x$variable" = "x" ]

这是正确的方法吗?(必须有更直接的东西)

scripting bash variables
  • 15 个回答
  • 1279404 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