Ansible delegate_toディレクティブでIPアドレスを指定するサンプル

環境
Ansible
RHEL8

概要
他のホストを参照して 1 つのホストでタスクを実行する場合は、
delegate_toタスクでキーワードを使用します。これは、負荷分散されたプール内のノードを管理したり、停止期間を制御したりするのに最適です。

インベントリ

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[group1]
172.17.101.1
172.17.101.2
[group2]
172.17.102.1
172.17.102.2
[group3]
172.17.103.1
[group1] 172.17.101.1 172.17.101.2 [group2] 172.17.102.1 172.17.102.2 [group3] 172.17.103.1
[group1]
172.17.101.1
172.17.101.2

[group2]
172.17.102.1
172.17.102.2

[group3]
172.17.103.1

Playbook
インベントリで定義されたターゲットノードの中から、『172.17.101.1』を指定します。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
---
- hosts: all
become: yes
gather_facts: False
tasks:
- name: ディレクトリの作成
file:
path: /tmp/dir
state: directory
- name: ファイルコピー
copy:
src: /etc/ansible/arkgame.txt
dest: /tmp/dir
delegate_to: 172.17.101.1
- name: ディレクトリを圧縮
archive:
path: /tmp
dest: /tmp/archive.tgz
...
--- - hosts: all become: yes gather_facts: False tasks: - name: ディレクトリの作成 file: path: /tmp/dir state: directory - name: ファイルコピー copy: src: /etc/ansible/arkgame.txt dest: /tmp/dir delegate_to: 172.17.101.1 - name: ディレクトリを圧縮 archive: path: /tmp dest: /tmp/archive.tgz ...
---
- hosts: all
  become: yes
  gather_facts: False

  tasks:
    - name: ディレクトリの作成
      file:
        path: /tmp/dir
        state: directory

    - name: ファイルコピー
      copy:
        src: /etc/ansible/arkgame.txt
        dest: /tmp/dir
      delegate_to: 172.17.101.1

    - name: ディレクトリを圧縮
      archive:
        path: /tmp
        dest: /tmp/archive.tgz
...

 

Ansible

Posted by arkgame