Ansible の shell モジュールを試してみた。
オプション |パラメータ|デフォルト値|選択肢|説明| |:-|:-|:-|:-|:-| |chdir|-|-|コマンドを実行する前にcdコマンドでディレクトリを移動する| |creates|-|-|指定したファイル名が既に存在する場合、コマンドを実行しない。| |executable|-|-|実行するシェルを絶対パスで指定する| |removes|-|-|指定したファイル名が存在しない場合、コマンドを実行しない| |warn|true|-|『no』または『false』を指定した場合、コマンド警告を出さない|
コマンドを実行する 設定 playbook.yml 1 2 3 4 5 6 7 8 - hosts: test-hosts become: true tasks: - shell: pwd registier: result - debug: msg: "{{ result }} "
実行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ ansible-playbook -i hosts playbook.yml -K SUDO password: PLAY [test-hosts] ************************************************************** TASK [setup] ******************************************************************* ok: [192.168.1.201] TASK [command] ***************************************************************** changed: [192.168.1.201] TASK [debug] ******************************************************************* ok: [192.168.1.201] => { "msg": { "changed": true, "cmd": "pwd", "delta": "0:00:00.002570", "end": "2020-06-11 18:26:15.729557", "rc": 0, "start": "2020-06-11 18:26:15.726987", "stderr": "", "stdout": "/home/foo", "stdout_lines": [ "/home/foo" ], "warnings": [] } } PLAY RECAP ********************************************************************* 192.168.1.201 : ok=3 changed=1 unreachable=0 failed=0
スクリプトの実行前にディレクトリーを移動する (chdir) 設定 playbook.yml 1 2 3 4 5 6 7 8 9 10 - hosts: test-hosts become: true tasks: - shell: pwd args: chdir: "/tmp/" register: result - debug: msg: "{{ result }} "
実行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ ansible-playbook -i hosts playbook.yml -K SUDO password: PLAY [test-hosts] ************************************************************** TASK [setup] ******************************************************************* ok: [192.168.1.201] TASK [command] ***************************************************************** changed: [192.168.1.201] TASK [debug] ******************************************************************* ok: [192.168.1.201] => { "msg": { "changed": true, "cmd": "pwd", "delta": "0:00:00.002509", "end": "2020-06-11 18:32:28.174569", "rc": 0, "start": "2020-06-11 18:32:28.172060", "stderr": "", "stdout": "/tmp", "stdout_lines": [ "/tmp" ], "warnings": [] } } PLAY RECAP ********************************************************************* 192.168.1.201 : ok=3 changed=1 unreachable=0 failed=0
ファイルが存在したら実行しない(creates) 設定 playbook.yml 1 2 3 4 5 6 7 8 9 10 - hosts: test-hosts become: true tasks: - shell: touch /tmp/hogehoge args: creates: "/tmp/hogehoge" register: result - debug: msg: "{{ result }} "
実行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 $ ansible-playbook -i hosts playbook.yml -K SUDO password: PLAY [test-hosts] ************************************************************** TASK [setup] ******************************************************************* ok: [192.168.1.201] TASK [command] ***************************************************************** changed: [192.168.1.201] [WARNING]: Consider using file module with state=touch rather than running touch TASK [debug] ******************************************************************* ok: [192.168.1.201] => { "msg": { "changed": true, "cmd": "touch /tmp/hogehoge", "delta": "0:00:00.003274", "end": "2020-06-11 18:35:25.642839", "rc": 0, "start": "2020-06-11 18:35:25.639565", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": [ "Consider using file module with state=touch rather than running touch" ] } } PLAY RECAP ********************************************************************* 192.168.1.201 : ok=3 changed=1 unreachable=0 failed=0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 $ ansible-playbook -i hosts playbook.yml -K SUDO password: PLAY [test-hosts] ************************************************************** TASK [setup] ******************************************************************* ok: [192.168.1.201] TASK [command] ***************************************************************** ok: [192.168.1.201] TASK [debug] ******************************************************************* ok: [192.168.1.201] => { "msg": { "changed": false, "cmd": "echo \"ihelloworld\" > /tmp/hogehoge", "rc": 0, "stdout": "skipped, since /tmp/hogehoge exists", "stdout_lines": [ "skipped, since /tmp/hogehoge exists" ] } } PLAY RECAP ********************************************************************* 192.168.1.201 : ok=3 changed=0 unreachable=0 failed=0