ansibleを使用してファイルを読み取り、各行をコマンドとして実行します。

ansibleを使用してファイルを読み取り、各行をコマンドとして実行します。

次のシナリオのスクリプトを作成したいと思います。 Linuxコマンドが記録されたテキストファイルを読み取り、1つずつ実行し、コマンドの実行に失敗した場合は中止し、コマンドを修正してスクリプトを再実行すると選択されます。中断された場所(最初から実行する代わりに)

サンプルファイル:sample.txt

echo "hello world"  
df -h  
free -m  
mkdir /tmp/`hostname`_bkp  
touch /tmp/`hostname`_bkp/file{1..5}  
mvn -version  
echo "directory and files created"  
echo "Bye.!"  

たとえば、mvn -version実行が失敗した場合は、ansibleを中断する必要があります。

Ansibleでシナリオを実装する方法は?

ベストアンサー1

以下は、いくつかの簡単なタスクを実行するサンプルプレイブックです。

---
 - hosts: localhost
   tasks:
    - name: say hi
      shell: echo "Hello, World!"

    - name: do df -h
      shell: df -h
      register: space

    - name: show the output of df -h
      debug: var=space

    - name: do free -m
      shell: free -m
      register: memory
      ignore_errors: yes

    - name: show memory stats
      debug: var=memory

    - name: create /tmp/"hostname"_bkp
      file: dest=/tmp/{{ ansible_nodename }}_bkp state=directory

    - name: create files
      file: dest=/tmp/{{ ansible_nodename }}_bkp/file{{ item }} state=touch
      with_items:
       - 1
       - 2
       - 3
       - 4
       - 5

必要な場所にディレクトリとファイルを作成します。要件に適した所有権、権限を設定することもできます。

ansible_nodenameゲーム開始時に収集されたAnsible事実(変数)です。

Ansibleファイルモジュールの詳細を見ることができます。ここ。他のAnsibleモジュールを見てください。豊富で学びやすく強力です。

おすすめ記事