Ansible ansible-playbook コマンドでプレイブックを実行する

環境
RHEL8.6

概要
playbook 実行するプレイブックファイルを指定する
-i インベントリーファイルを指定する
–list-tasks プレイブックを実行せず、実行対象のタスクの一覧 ( タスク名、設定したタグ 等 ) を表示する
-t –tags
指定したタグを設定したタスクを実行する
–skip-tags
指定したタグを設定したタスクを除いて実行する

操作例
1.apache_setup.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- name: Install Apache.
hosts: testserver
become: yes
gather_facts: no
vars:
apache_package_name: httpd
apache_service_name: httpd.service
tasks:
- name: httpdパッケージのインストール
ansible.builtin.yum:
name: "{{ apache_package_name }}"
state: present
notify:
- start apache service
- name: Install the firewalld package.
ansible.builtin.yum:
name: firewalld
state: present
- name: Start "firewalld.service".
ansible.builtin.systemd:
name: firewalld.service
enabled: yes
state: started
handlers:
- name: ポート番号を変更
ansible.builtin.lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen "
line: "Listen 8080"
validate: httpd -t -f %s
listen:
- start apache service
- name: Start "httpd.service".
ansible.builtin.systemd:
name: "{{ apache_service_name }}"
enabled: yes
state: started
listen:
- start apache service
- name: Install Apache. hosts: testserver become: yes gather_facts: no vars: apache_package_name: httpd apache_service_name: httpd.service tasks: - name: httpdパッケージのインストール ansible.builtin.yum: name: "{{ apache_package_name }}" state: present notify: - start apache service - name: Install the firewalld package. ansible.builtin.yum: name: firewalld state: present - name: Start "firewalld.service". ansible.builtin.systemd: name: firewalld.service enabled: yes state: started handlers: - name: ポート番号を変更 ansible.builtin.lineinfile: path: /etc/httpd/conf/httpd.conf regexp: "^Listen " line: "Listen 8080" validate: httpd -t -f %s listen: - start apache service - name: Start "httpd.service". ansible.builtin.systemd: name: "{{ apache_service_name }}" enabled: yes state: started listen: - start apache service
- name: Install Apache.
  hosts: testserver
  become: yes
  gather_facts: no

  vars:
    apache_package_name: httpd
    apache_service_name: httpd.service

  tasks:
    - name:  httpdパッケージのインストール
      ansible.builtin.yum:
        name: "{{ apache_package_name }}"
        state: present
      notify:
        - start apache service

    - name: Install the firewalld package.
      ansible.builtin.yum:
        name: firewalld
        state: present

    - name: Start "firewalld.service".
      ansible.builtin.systemd:
        name: firewalld.service
        enabled: yes
        state: started

  handlers:
    - name: ポート番号を変更
      ansible.builtin.lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen "
        line: "Listen 8080"
        validate: httpd -t -f %s 
      listen:
        - start apache service

    - name: Start "httpd.service".
      ansible.builtin.systemd:
        name: "{{ apache_service_name }}"
        enabled: yes
        state: started
      listen:
        - start apache service

2.hosts.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
---
all:
hosts:
kkk:
children:
web:
hosts:
testserver:
cft:
--- all: hosts: kkk: children: web: hosts: testserver: cft:
---
all:
  hosts:
    kkk:
  children:
    web:
      hosts:
        testserver:
        cft:

実行結果
$ ansible-playbook -i hosts.yml apache_setup.yml

Ansible

Posted by arkgame