How to automate user interactive command in chef recipe

左心房为你撑大大i 提交于 2019-12-12 13:45:22

问题


I am trying to write code for automating /etc/init.d/oracleasm configure command where as it needs 4 inputs to proceed.

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

Generally what is the best way to write code for achieving this. Please suggest me.


回答1:


It would be better if you had a command you could run non-interactively, but you can run an interactive command using expect. Based in your example it would be:

    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

It is important /etc/init.d/oracleasm configure to be an idempotent command, this means that you can run it once, twice or one hundred times and its behavior and the result system will be the same. If this is not the case, you need some guards to the command (only_if or not_if) to run only the command when needed:

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


来源:https://stackoverflow.com/questions/31203989/how-to-automate-user-interactive-command-in-chef-recipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!