【问题标题】:How to automate user interactive command in chef recipe如何在厨师食谱中自动化用户交互命令
【发布时间】:2015-09-21 03:05:52
【问题描述】:

我正在尝试编写代码来自动化 /etc/init.d/oracleasm configure 命令,因为它需要 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

一般来说,为实现这一目标编写代码的最佳方式是什么。请给我建议。

【问题讨论】:

    标签: chef-infra recipe


    【解决方案1】:

    如果您有一个可以非交互式运行的命令会更好,但您可以使用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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 2015-05-01
      相关资源
      最近更新 更多