質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

1回答

16580閲覧

Ansibleでwith_itemsを使ってファイル存在チェックし、存在するファイルにのみ処理を行う方法

yu_1985

総合スコア7427

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

0クリップ

投稿2017/04/26 11:02

先日の質問の続きです。

複数ファイルについてログのS3へのコピーと削除(/dev/nullで上書き)を行う部分で、
その前にstatによる存在チェックを行い、
exists:trueであるもののみに処理を行うように改良したいと考えています。

- hosts: hoge vars: logs: - { path: '/path/to/log', logname: 'foo.log', rotation: '3', size: '10M' } - { path: '/path/to/log', logname: 'bar.log', rotation: '3', size: '10M' } - { path: '/path/to/log', logname: 'baz.log', rotation: '5', size: '100M' } tasks: - name: Check if log exists stat: path: "{{ item.path }}/{{ item.logname }}" with_items: "{{ logs }}" register: existence tags: checkfiles - debug: var=existence tags: checkfiles - name: Backup existing log to S3 with compression shell: gzip -c {{ item.path }}/{{ item.logname }} | aws s3 - s3://bucketname/{{ ansible_hostname }}/{{ item.logname }}.gz when: existence.stat.exists with_items: "{{ logs }}" ignore_errors: yes tags: backup_logs - name: Delete logs shell: cat /dev/null > {{ item.path }}/{{ item.logname }} when: existence.stat.exists with_items: "{{ logs }}" tags: delete_logs

というように、statsの結果をregisterで変数に入れてその後の処理で使おうとしたのですが、上記だとエラーになります。

TASK [Backup existing log to S3 with compression] ****************************** fatal: [xxx.xxx.xxx.xxx]: FAILED! => {"failed": true, "msg": "The conditional check 'existence.stat.exists == True' failed. The error was: error while evaluating conditional (existence.stat.exists == True): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/home/foo/logrotate.yml': line 16, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tags: checkfiles\n - name: Backup existing log to S3 with compression\n ^ here\n"} ...ignoring TASK [Delete logs] ************************************************************* fatal: [xxx.xxx.xxx.xxx]: FAILED! => {"failed": true, "msg": "The conditional check 'existence.stat.exists == True' failed. The error was: error while evaluating conditional (existence.stat.exists == True): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/home/foo/logrotate.yml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tags: backuplogs\n - name: Delete logs\n ^ here\n"}

※一部内容を伏せるなどしています。

実現したいことはタイトルの通りです。
whenとwith_itemの記述順を入れ替えたりもしましたが、変わらずでした。

whenの指定が誤っているのか、そもそもこういった指定の仕方は不可能なのか、自分にはちょっとわかりませんでした。
どなたかご教示いただけますと幸いです。

参考
statモジュール

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

3,4番目のタスクに、statの取得結果を使用したいのであれば、with_itemsには、statの取得結果を指定するのが簡単です。
3,4番目のタスクの
with_itemsは「existence.results」に変更
shellないのでpath、logname参照は、「item.item.path」「item.item.logname」に変更
こんなコーディングになると思います。

- hosts: hoge vars: logs: - { path: '/path/to/log', logname: 'foo.log', rotation: '3', size: '10M' } - { path: '/path/to/log', logname: 'bar.log', rotation: '3', size: '10M' } - { path: '/path/to/log', logname: 'baz.log', rotation: '5', size: '100M' } tasks: - name: Check if log exists stat: path: "{{ item.path }}/{{ item.logname }}" with_items: "{{ logs }}" register: existence tags: checkfiles - debug: var=existence tags: checkfiles - name: Backup existing log to S3 with compression shell: gzip -c {{ item.item.path }}/{{ item.item.logname }} | aws s3 - s3://bucketname/{{ ansible_hostname }}/{{ item.item.logname }}.gz when: item.stat.exists with_items: "{{ existence.results }}" ignore_errors: yes tags: backup_logs - name: Delete logs shell: cat /dev/null > {{ item.item.path }}/{{ item.item.logname }} when: item.stat.exists with_items: "{{ existence.results }}" tags: delete_logs

投稿2017/04/27 00:18

編集2017/04/27 00:20
yasu-tamura

総合スコア64

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yu_1985

2017/04/27 10:35

まさしく! ご教示いただいた内容でうまくいきました! なるほど、with_itemsにregisterの結果の変数を指定して、そこから値を持ってくることができるんですね。 今後同様のPlaybookを書きたい際も参考にさせていただきます! 本当に助かりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問