説明
演習問題
Webmin をインストールするプレイブックです。プレイブック内で判断はしていませんが RedHat 系ディストリビューションの Ver.8 用です。
---
all:
hosts:
oike:
---
ansible_user: vagrant
ansible_password: vagrant
---
ansible_host: 192.168.111.106
ansible_python_interpreter: /usr/libexec/platform-python
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
tasks:
- name: Register a repository for Webmin.
ansible.builtin.blockinfile:
path: "{{ webmin_repo_path }}"
create: yes
block: |
[webmin]
name=Webmin Repository
baseurl=http://download.webmin.com/download/yum/
gpgcheck=1
enabled=1
gpgkey=http://www.webmin.com/jcameron-key.asc
state: present
- name: Install the perl-Net-SSLeay module.
ansible.builtin.dnf:
name: perl-Net-SSLeay
state: present
- name: Install the Webmin module.
ansible.builtin.dnf:
name: webmin
state: present
プレイブックの実行後、ブラウザーで次の URL を指定すると Webmin のログイン画面が表示され、管理対象ノードの root アカウントでログインできます。
https://192.168.111.106:10000
Q1. タスク Install the perl-Net-SSLeay module. と Install the Webmin module. を次の条件を満たすよう変更してください。
- プレイブックファイル名は「 install_webmin1.yml 」にする。
- 2 つのタスクを 1 つにまとめ、
loop
ディレクティブにインストールするパッケージを指定する。
解答
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
tasks:
- name: Register a repository for Webmin.
ansible.builtin.blockinfile:
path: "{{ webmin_repo_path }}"
create: yes
block: |
[webmin]
name=Webmin Repository
baseurl=http://download.webmin.com/download/yum/
gpgcheck=1
enabled=1
gpgkey=http://www.webmin.com/jcameron-key.asc
state: present
- name: Install the package.
ansible.builtin.dnf:
name: "{{ item }}"
state: present
loop:
- perl-Net-SSLeay
- webmin
y_mrok@ctrl:~/code/chap13$ ansible-playbook -i hosts.yml install_webmin1.yml
PLAY [Install Webmin.] *******************************************************************************************************************************
TASK [Register a repository for Webmin.] *************************************************************************************************************
changed: [oike]
TASK [Install the package.] **************************************************************************************************************************
ok: [oike] => (item=perl-Net-SSLeay)
changed: [oike] => (item=webmin)
PLAY RECAP *******************************************************************************************************************************************
oike : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap13$
Q2. 「 Q1. 」のプレイブックの loop
ディレクトリに指定したパッケージ名をタスク変数で指定するよう変更してください。
- プレイブックファイル名は「 install_webmin2.yml 」にする。
- タスク変数名は packages にする。
解答
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
tasks:
- name: Register a repository for Webmin.
ansible.builtin.blockinfile:
path: "{{ webmin_repo_path }}"
create: yes
block: |
[webmin]
name=Webmin Repository
baseurl=http://download.webmin.com/download/yum/
gpgcheck=1
enabled=1
gpgkey=http://www.webmin.com/jcameron-key.asc
state: present
- name: Install the package.
ansible.builtin.dnf:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
vars:
packages:
- perl-Net-SSLeay
- webmin
y_mrok@ctrl:~/code/chap13$ ansible-playbook -i hosts.yml install_webmin2.yml
PLAY [Install Webmin.] *******************************************************************************************************************************
TASK [Register a repository for Webmin.] *************************************************************************************************************
changed: [oike]
TASK [Install the package.] **************************************************************************************************************************
ok: [oike] => (item=perl-Net-SSLeay)
changed: [oike] => (item=webmin)
PLAY RECAP *******************************************************************************************************************************************
oike : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap13$
Q3. 「 Q2. 」のプレイブックのタスク変数に指定したパッケージ名を vars_failes
セクションで取り込むよう変更してください。
- プレイブックファイル名は「 install_webmin3.yml 」にする。
-
vars_files
セクションで取り込むファイルの名前は「 parameter.yml 」にする。
解答
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
vars_files:
- parameter.yml
tasks:
- name: Register a repository for Webmin.
ansible.builtin.blockinfile:
path: "{{ webmin_repo_path }}"
create: yes
block: |
[webmin]
name=Webmin Repository
baseurl=http://download.webmin.com/download/yum/
gpgcheck=1
enabled=1
gpgkey=http://www.webmin.com/jcameron-key.asc
state: present
- name: Install the package.
ansible.builtin.dnf:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
---
packages:
- perl-Net-SSLeay
- webmin
y_mrok@ctrl:~/code/chap13$ ansible-playbook -i hosts.yml install_webmin3.yml
PLAY [Install Webmin.] *******************************************************************************************************************************
TASK [Register a repository for Webmin.] *************************************************************************************************************
changed: [oike]
TASK [Install the package.] **************************************************************************************************************************
ok: [oike] => (item=perl-Net-SSLeay)
changed: [oike] => (item=webmin)
PLAY RECAP *******************************************************************************************************************************************
oike : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap13$
Q4. 「 Q3. 」のプレイブックのタスク Register a repository for Webmin. を次の条件を満たすよう変更してください。
- プレイブックファイル名は「 install_webmin4.yml 」にする。
-
ansible.builtin.lineinfile
モジュールを使用する。 -
loop
ディレクティブに 「 /etc/yum.repos.d/webmin.repo 」ファイルの内容を指定する。 -
loop
ディレクティブに [webmin] を指定するときは "[webmin]" のようにダブルクォート"
でくくること。
ansible.builtin.lineinfile モジュール
ansible.builtin.lineinfile
モジュールはの主な働きです。
- ファイル内の特定の行の内容を別の内容に置換する
- ファイルの末尾に行を追加する
- 対象のファイルが存在しない場合、ファイルを作成することができる
ansible.builtin.lineinfile
モジュールは path
パラメーターに指定したファイルに line
パラメーターに指定した内容を書き込みます。管理対象ノードに「 /tmp/sample.txt 」ファイルを作成し、 3 行書き込む ( 追加する ) プレイです。
---
- name: Add a line to the end of the file.
hosts: all
gather_facts: no
tasks:
- name: 1 line item
ansible.builtin.lineinfile:
path: /tmp/sample.txt
create: yes
line: "abcdefg"
state: present
- name: 2 line items
ansible.builtin.lineinfile:
path: /tmp/sample.txt
create: yes
line: "123456"
state: present
- name: 3 line items
ansible.builtin.lineinfile:
path: /tmp/sample.txt
create: yes
line: "alpha-beta"
state: present
実行前の管理対象ノードの「 /tmp 」ディレクトリーの状態です。「 /tmp/sample.txt 」ファイルは存在しません。
y_mrok@ctrl:~/code/exam6$ ansible oike -i hosts.yml -a 'ls -l /tmp'
oike | CHANGED | rc=0 >>
total 4
drwx------. 2 vagrant vagrant 56 Oct 9 10:40 ansible_ansible.legacy.command_payload_9k95iyqr
drwx------. 3 root root 17 Oct 9 01:40 systemd-private-b502fad713114cf0a97851d9e875a5b1-chronyd.service-HiURai
drwx------. 3 root root 17 Oct 9 01:40 systemd-private-b502fad713114cf0a97851d9e875a5b1-systemd-hostnamed.service-w3WQdj
-rwxrwxr-x. 1 vagrant vagrant 240 Oct 3 08:46 vagrant-shell
y_mrok@ctrl:~/code/exam6$
プレイブックを実行します。
y_mrok@ctrl:~/code/exam6$ ansible-playbook -i hosts.yml sample.yml
PLAY [Add a line to the end of the file.] ************************************************************************************************************
TASK [1 line item] ***********************************************************************************************************************************
changed: [oike]
TASK [2 line items] **********************************************************************************************************************************
changed: [oike]
TASK [3 line items] **********************************************************************************************************************************
changed: [oike]
PLAY RECAP *******************************************************************************************************************************************
oike : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/exam6$
実行後の管理対象ノードの「 /tmp 」ディレクトリーの状態です。「 /tmp/sample.txt 」ファイルが作成されました。
y_mrok@ctrl:~/code/exam6$ ansible oike -i hosts.yml -a 'ls -l /tmp'
oike | CHANGED | rc=0 >>
total 8
drwx------. 2 vagrant vagrant 56 Oct 9 10:41 ansible_ansible.legacy.command_payload_pzlw2rcs
-rw-rw-r--. 1 vagrant vagrant 26 Oct 9 10:41 sample.txt
drwx------. 3 root root 17 Oct 9 01:40 systemd-private-b502fad713114cf0a97851d9e875a5b1-chronyd.service-HiURai
-rwxrwxr-x. 1 vagrant vagrant 240 Oct 3 08:46 vagrant-shell
y_mrok@ctrl:~/code/exam6$
「 /tmp/sample.txt 」ファイルの内容です。プレイブックで指定した 3 行が書き込まれています。
y_mrok@ctrl:~/code/exam6$ ansible oike -i hosts.yml -a 'cat /tmp/sample.txt'
oike | CHANGED | rc=0 >>
abcdefg
123456
alpha-beta
y_mrok@ctrl:~/code/exam6$
解答
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
vars_files:
- parameter.yml
tasks:
- name: Register a repository for Webmin.
ansible.builtin.lineinfile:
path: "{{ webmin_repo_path }}"
create: yes
line: "{{ item }}"
state: present
loop:
- "[webmin]"
- name=Webmin Repository
- baseurl=http://download.webmin.com/download/yum/
- gpgcheck=1
- enabled=1
- gpgkey=http://www.webmin.com/jcameron-key.asc
- name: Install the package.
ansible.builtin.dnf:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
y_mrok@ctrl:~/code/chap13$ ansible-playbook -i hosts.yml install_webmin4.yml
PLAY [Install Webmin.] *******************************************************************************************************************************
TASK [Register a repository for Webmin.] *************************************************************************************************************
changed: [oike] => (item=[webmin])
changed: [oike] => (item=name=Webmin Repository)
changed: [oike] => (item=baseurl=http://download.webmin.com/download/yum/)
changed: [oike] => (item=gpgcheck=1)
changed: [oike] => (item=enabled=1)
changed: [oike] => (item=gpgkey=http://www.webmin.com/jcameron-key.asc)
TASK [Install the package.] **************************************************************************************************************************
ok: [oike] => (item=perl-Net-SSLeay)
changed: [oike] => (item=webmin)
PLAY RECAP *******************************************************************************************************************************************
oike : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap13$
Q5. 「 Q4. 」のプレイブックの loop
ディレクトリに指定した内容をタスク変数で指定するよう変更してください。
- プレイブックファイル名は「 install_webmin5.yml 」にする。
- タスク変数名は webmin_repo にする。
解答
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
vars_files:
- parameter.yml
tasks:
- name: Register a repository for Webmin.
ansible.builtin.lineinfile:
path: "{{ webmin_repo_path }}"
create: yes
line: "{{ item }}"
state: present
loop: "{{ webmin_repo }}"
vars:
webmin_repo:
- "[webmin]"
- name=Webmin Repository
- baseurl=http://download.webmin.com/download/yum/
- gpgcheck=1
- enabled=1
- gpgkey=http://www.webmin.com/jcameron-key.asc
- name: Install the package.
ansible.builtin.dnf:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
y_mrok@ctrl:~/code/chap13$ ansible-playbook -i hosts.yml install_webmin5.yml
PLAY [Install Webmin.] *******************************************************************************************************************************
TASK [Register a repository for Webmin.] *************************************************************************************************************
changed: [oike] => (item=[webmin])
changed: [oike] => (item=name=Webmin Repository)
changed: [oike] => (item=baseurl=http://download.webmin.com/download/yum/)
changed: [oike] => (item=gpgcheck=1)
changed: [oike] => (item=enabled=1)
changed: [oike] => (item=gpgkey=http://www.webmin.com/jcameron-key.asc)
TASK [Install the package.] **************************************************************************************************************************
ok: [oike] => (item=perl-Net-SSLeay)
changed: [oike] => (item=webmin)
PLAY RECAP *******************************************************************************************************************************************
oike : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap13$
Q6. 「 Q5. 」のプレイブックのタスク変数に指定した値を vars_failes
セクションで取り込むよう変更してください。
- プレイブックファイル名は「 install_webmin6.yml 」にする。
-
vars_files
セクションで取り込むファイルの名前は「 parameter.yml 」にする。
解答
---
- name: Install Webmin.
hosts: all
gather_facts: no
become: yes
vars:
webmin_repo_path: "/etc/yum.repos.d/webmin.repo"
vars_files:
- parameter.yml
tasks:
- name: Register a repository for Webmin.
ansible.builtin.lineinfile:
path: "{{ webmin_repo_path }}"
create: yes
line: "{{ item }}"
state: present
loop: "{{ webmin_repo }}"
- name: Install the package.
ansible.builtin.dnf:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
---
packages:
- perl-Net-SSLeay
- webmin
webmin_repo:
- "[webmin]"
- name=Webmin Repository
- baseurl=http://download.webmin.com/download/yum/
- gpgcheck=1
- enabled=1
- gpgkey=http://www.webmin.com/jcameron-key.asc
y_mrok@ctrl:~/code/chap13$ ansible-playbook -i hosts.yml install_webmin6.yml
PLAY [Install Webmin.] *******************************************************************************************************************************
TASK [Register a repository for Webmin.] *************************************************************************************************************
changed: [oike] => (item=[webmin])
changed: [oike] => (item=name=Webmin Repository)
changed: [oike] => (item=baseurl=http://download.webmin.com/download/yum/)
changed: [oike] => (item=gpgcheck=1)
changed: [oike] => (item=enabled=1)
changed: [oike] => (item=gpgkey=http://www.webmin.com/jcameron-key.asc)
TASK [Install the package.] **************************************************************************************************************************
ok: [oike] => (item=perl-Net-SSLeay)
changed: [oike] => (item=webmin)
PLAY RECAP *******************************************************************************************************************************************
oike : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap13$