2017-07-28 10 views
0

私は比較的新しい方です。grepは、後でforループを使用してタスクで使用することができます。

私はこのようなものでparameters.ymlを持っている:

parameters: 
    stuff: bullshit 
    otherstuff: otherbullshit 
    domain_locale: domain.locale.test 
    domain_differentlocale1: domain.differentlocale1.test 
    domain_differentlocale2: domain.differentlocale2.test 
    domain_differentlocale3: domain.differentlocale3.test 
    uninteresting_stuff: uninteresting_bullshit 

このファイルは別のディレクトリにあるが、私はまた、デフォルト/ main.ymlにあるように、それをシンボリックリンクができます。関係ない。

私が達成しようとしているのは、どうにかしてgrepです。これらの値はdomain.*.testの値であり、別のタスクではそれを使うことができます。この他のタスクは、/etc/hostsの同じ行に127.0.0.1 localhostの直後に書き込むことです。

私はどのモジュールを使用するべきかについても不安です。 include_varsには追加のgrepはありません。 set_factは別のファイルから読み込みません(シェルコマンドと組み合わせることはできますか?)。 lineinfileには送信元と宛先がないため、1つのファイルにも制限されています。

また、この投稿は私に役立つと思った:Ansible read multiple variables with same name from vars_file しかし、grepの部分は必要ありません。ここで

答えて

1

あなたが行く:

--- 
- hosts: localhost 
    gather_facts: no 
    vars: 
    parameters: 
     stuff: bullshit 
     otherstuff: otherbullshit 
     domain_locale: domain.locale.test 
     domain_differentlocale1: domain.differentlocale1.test 
     domain_differentlocale2: domain.differentlocale2.test 
     domain_differentlocale3: domain.differentlocale3.test 
     uninteresting_stuff: uninteresting_bullshit 
    tasks: 
    - lineinfile: 
     dest: /tmp/test.txt 
     regexp: ^127.0.0.1 
     line: "127.0.0.1 localhost {{ parameters.values() | select('match','domain.*test') | list | join(' ') }}" 
+0

私が手: OK:[ローカルホスト] => { "MSG": "0x7f5e8225b730で<ジェネレータオブジェクトの_select_or_reject>" } – metanerd

+1

あなたは印刷するかは、 'ですデバッグ?この場合は明示的な 'list'変換が必要です。明確にするために私の答えに追加しました。 –

+0

私は1つのキーを持つ別々のファイルでそれを試しました。しかし、私がデバッグすることができるparameters.ymlを試すと、私はまだ{"failed":true、 "msg": "{{parameters.values()| select(\" match "value"はファイルの2行目の単純な値です。 これまでにありがとうございました:) ああ、あなたの投稿の別の部分に表示されています。これは、msgが定義されていないことを意味します。くそー、なぜ? – metanerd

関連する問題