gpt4 book ai didi

chef-infra - 如何在 Chef Recipe 中自动执行用户交互命令

转载 作者:行者123 更新时间:2023-12-01 05:18:29 25 4
gpt4 key购买 nike

我正在尝试编写用于自动化/etc/init.d/oracleasm 配置命令的代码,因为它需要 4 个输入才能继续。

Default user to own the driver interface [grid]: grid
Default group to own the driver interface [y]: oinstall
Start Oracle ASM library driver on boot (y/n) [y]: y
Scan for Oracle ASM disks on boot (y/n) [y]: y

一般而言,编写实现此目标的代码的最佳方式是什么。请给我建议。

最佳答案

如果您有一个可以非交互运行的命令会更好,但是您可以使用 expect 运行一个交互命令。根据您的示例,它将是:

    bash 'OracleASM' do
user 'root'
code <<-EOF
/usr/bin/expect -c 'spawn /etc/init.d/oracleasm configure
expect "Default user to own the driver interface [grid]: "
send "grid\r"
expect "Default group to own the driver interface [y]: "
send "oinstall\r"
expect "Start Oracle ASM library driver on boot (y/n) [y]: "
send "y\r"
expect "Scan for Oracle ASM disks on boot (y/n) [y]: "
send "y\r"
expect eof'
EOF
end

重要的是 /etc/init.d/oracleasm configure 是一个幂等命令,这意味着您可以运行它一次、两次或一百次,它的行为和结果系统将是相同的。如果不是这种情况,您需要对命令进行一些保护(only_ifnot_if)以仅在需要时运行命令:

bash 'OracleASM' do
user 'root'
code <<-EOF
....
EOF
not_if { ::File.exists?('/...') }
end

关于chef-infra - 如何在 Chef Recipe 中自动执行用户交互命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31203989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com