/
カテゴリー
問題名
Ansibleが動かない
概要
ICTSCエンジニアリング株式会社のサーバー構築チームにあなたは所属しています。
チームの先輩がAnsibleなるものを知り、スクリプトでの構築から移行したいらしい。
先輩がサンプルを見よう見まねで書いたところ動かないため、君に正常にように動くPlayBookの修正と実行するためのコマンドを調べてほしい。
先輩が/home/user/ansible/
に書きかけのAnsible PlayBook setup.yaml
を用意してくれた。
PlayBookで実行されてほしい手順としては
- サーバーのUpdate
- Nginxのインストール
nginx-default.template
に変数を埋め込んでNginxのConfigを置き換える- Nginxを再起動する
inventory.yaml
をインベントリとしてsetup.yaml
をAnsibleで実行できるようにsetup.yaml
を書き換えて動くAnsibleを用意してくれ。
前提条件
inventory.yaml
,nginx-default.template
の変更禁止
初期状態
Ansibleの実行の仕方がわからない
終了状態
inventory.yaml
をインベントリとしてsetup.yaml
をAnsibleで実行できるようになっている- 実行時に、Nginxのインストール、変数展開したConfigファイルに置き換えられて、Nginxが正常に起動していること
問題環境
Ansibleを実行できるサーバー一台のみである。
- ansibleをインストールしたマシンを用意する
- 以下のファイル
- inventory.yaml
all: hosts: local: ansible_host: localhost
- nginx-default.template
server { listen {{ port_num }} default_server; listen [::]:{{ port_num }} default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
- setup.yaml
- hosts: all tasks: - name: apt update apt: update_cache: "yes" - name: nginx install apt: name: nginx state: present - name: update config copy: src: ./nginx-default.template dest: /etc/nginx/sites-available/default owner: root group: root - name: nginx.service restart service: name: nginx.service daemon_reload: yes state: restarted enabled: yes vars: port_num: 8080
解説
問題としては
- 権限昇格ができないためansible-playbookの実行に失敗する
- Nginxの設定ファイルであるnginx-default.templateは変数展開する箇所があるが、copyでは展開されない
become: yes
を追加して権限昇格- copyからtemplateへ変更
setup.yamlの書き換え
- hosts: all become: yes connection: local tasks: - name: apt update apt: update_cache: "yes" - name: nginx install apt: name: nginx state: present - name: update config template: src: ./nginx-default.template dest: /etc/nginx/sites-available/default owner: root group: root - name: nginx.service restart service: name: nginx.service daemon_reload: yes state: restarted enabled: yes vars: port_num: 8080
実行の仕方としては
ansible-playbook -i inventory.yaml -u user -k setup.yaml
今回はlocalhostに対して実行するのでconnection: local
を追加している。
採点基準
- NginxのインストールがAnsibleの実行によってできる。:50%
nginx-default.template
をもとにNginxが正常に稼働するようにAnsibleの実行によってできる:50%