2016-05-23 5 views
2

私はいくつかのローカルブックで意味をなさないプレイブックを持っています。そうでなければ、リモートで実行されます。私はdelegate_toディレクティブを使用しますが、それはまた、例えば、私はすべての私の仕事を倍増しなければならないことを意味:条件付きでdelegate_toローカルまたはリモートですか?

--- 
- hosts: all 
    gather_facts: no 

    tasks: 

    - name: Local command 
    command: hostname 
    register: target_host 
    when: vhost is undefined 
    delegate_to: 127.0.0.1 

# ---  

    - name: Remote command 
    command: hostname 
    register: target_host 
    when: vhost is defined 

Execのローカルにあることを行うために:

$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml 

PLAY [all] ******************************************************************** 

TASK: [Local command] ********************************************************* 
changed: [new-server -> 127.0.0.1] 

TASK: [Remote command] ******************************************************** 
skipping: [new-server] 

PLAY RECAP ******************************************************************** 
new-server     : ok=1 changed=1 unreachable=0 failed=0 

Execのリモートで

$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml -e vhost=y 

PLAY [all] ******************************************************************** 

TASK: [Local command] ********************************************************* 
skipping: [new-server] 

TASK: [Remote command] ******************************************************** 
changed: [new-server] 

PLAY RECAP ******************************************************************** 
new-server     : ok=1 changed=1 unreachable=0 failed=0 

ansibleローカル環境にフォールバックするときにはよりスマートな方法がありますか?現在、私はansible==1.9.2を使用しています。

答えて

6

タスクを実行する場所は、タスクで定義しないでください。プレイリストそのものと、それゆえ、ほとんどのタスクがプレイブックレベルで定義されたホストを実行している間に、タスクは常にローカルで、または関連するマシン(例えば、データベースホストまたはルータ)上で実行されなければならない場合、

しかし、プレイブック全体をローカルまたはリモートホストのセットで実行することを目標とする場合は、異なるインベントリファイルまたはグループを使用する必要があります。

インベントリファイルが2つある場合は、localhostを定義し、他のすべてのリモートホストで、-i inv.d/localまたは-i inv.d/remoteを呼び出すときに必要なインベントリを適用します。

または、すべてを1つのインベントリにまとめ、グループに動的に渡します。 ansibleする

[local] 
127.0.0.1 

[remote] 
host-1 
host-2 
host-N 

そして余分-VARとしてグループを渡します:-e "run=local"またはあなたの脚本で

-e "run=remote"あなたはhosts動的に設定:インベントリでは、2つのグループを定義

--- 
- hosts: "{{ run | mandatory }}" 
    gather_facts: no 
    tasks: 
    ... 

あなたの例では、vhost extra-varごとに定義された単一のリモートホストでのみ動作するようです。その場合、最適なオプションは、hostsセクションでこの変数を再利用し、デフォルトでlocalhostになるように見えます。

--- 
- hosts: "{{ vhost | default('127.0.0.1') }}" 
    gather_facts: no 
    tasks: 
    ... 

vhostが定義されているのであれば全体の脚本は、そのホスト上で実行されます。定義されていない場合、プレイブックはローカルで実行されます。それが定義されていないかのように、Ansibleはオプションを無視するために

- name: Local AND remote command 
    command: hostname 
    delegate_to: "{{ '127.0.0.1' if vhost is undefined else omit }}" 

omit is a special variable

最後に、あなたはまだそのようなシングルタスクのdelegate_toオプションを使用することができます。

+0

'connection:" {{'ansible_host' | default( 'local')}} ''? [Docs](http://docs.ansible.com/ansible/intro_inventory.html#non-ssh-connection-types)はあいまいであるように見えますが、 'ansible_host'は'接続するDockerコンテナの名前 'として定義されています。私は喜んでそれはドキュメントの単なるエラーです。 –

+0

このセクションの下部に記載されているように、インベントリファイルに接続を設定します:http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups – udondan

+0

'localhost ansible_connection = local' – udondan

関連する問題