Ansible delegate_toディレクティブでhostnameを指定するサンプル

環境
Ansible
RHEL8

概要
delegate_to: ホスト名
他のホストを参照して 1 つのホストでタスクを実行する場合は、delegate_toタスクでキーワードを使用します。
値にhostnameを指定した場合(グループ内の一ホスト)、「delegate_to」ディレクティブを利用します。

使用例
「delegate_to」ディレクティブの値に、hostname『ec2-user@ip-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 file
copy:
src: /etc/ansible/res.txt
dest: /tmp/dir
delegate_to: ec2-user@ip-172-17-101-1
- name: ディレクトリの圧縮
archive:
path: /tmp
dest: /tmp/data.tgz
- hosts: all become: yes gather_facts: False tasks: - name: ディレクトリ作成 file: path: /tmp/dir state: directory - name: Copy file copy: src: /etc/ansible/res.txt dest: /tmp/dir delegate_to: ec2-user@ip-172-17-101-1 - name: ディレクトリの圧縮 archive: path: /tmp dest: /tmp/data.tgz
- hosts: all
  become: yes
  gather_facts: False

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

    - name: Copy file
      copy:
        src: /etc/ansible/res.txt
        dest: /tmp/dir
      delegate_to: ec2-user@ip-172-17-101-1

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

 

Ansible

Posted by arkgame