前提・実現したいこと
ansible-playbookを実行する際、入力の手間を省き、パスワードを自動入力するプログラムを作成したいと考えています。
いくつかのオプションをgetoptを使用して、独自オプションで指定しています。
発生している問題・エラーメッセージ
"--extra-vars"オプションが認識されず、変数がdebugモジュールで表示されません。
./ansible.sh test.yml -l -e test=ts,test2=tts spawn env LANG=C /usr/local/bin/ansible-playbook test.yml -K --connection=local --extra-vars '{"test":"ts","test2":"tts"}' Using /etc/ansible/ansible.cfg as config file BECOME password: PLAY [local] **************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************** ok: [localhost] TASK [debug] **************************************************************************************** fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'test' is undefined\n\nThe error appears to be in '/home/yukki/ansible/playbook/role_make.yml': line 5, column 13, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: make project path\n ^ here\n"} PLAY RECAP ****************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
該当のソースコード
bash
1!/bin/bash 2 3PASS="pass" 4 5#オプション処理 6#vオプションはカンマで区切ることで複数指定が可能 7OPT=`getopt -o e: -- "$@"` 8eval set -- "$OPT" 9while [ $# -gt 0 ] 10do 11 case $1 in 12 -e) e_list=(${2//,/ });; 13 --) shift; break;; 14 esac 15 shift 16done 17 18#-eオプションがあるときに、e_optionの作成 19if [ -n "${e_list[0]}" ]; then 20 e_option="'{" 21 for h in "${e_list[@]}" ; do 22 e_option+="\"${h%=*}\":\"${h#*=}\"," 23 done 24 e_option="${e_option%,*}}'" 25fi 26 27if [ -n "${e_list[0]}" ]; then 28 cmd="/usr/local/bin/ansible-playbook $1 -K -v --extra-vars $e_option" 29else 30 cmd="/usr/local/bin/ansible-playbook $1 -K -v" 31fi 32 33#password自動入力 34expect -c " 35set timeout 10 36spawn env LANG=C $cmd 37expect \"password:\" 38send \"${PASS}\n\" 39expect \"$\" 40exit 0 41" 42 43exit 0
test.yml
- hosts: local become: yes tasks: - debug: msg: "{{ test }}"
試したこと
とにかくいろいろ試しましたが、"--extra-vars"オプションが認識されなくて困っています。
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2020/09/02 13:57
2020/09/02 15:44