星期二, 11月 24, 2015

Ansible 小記 - shell module 與 apt module

Ansible 小記 - shell module 與 apt module

Ansible 的 Module 可以在官網找到所有的 Module, 這邊是按照分類的 Index http://docs.ansible.com/ansible/modules_by_category.html

今天先來介紹兩個 module - shell 與 apt

首先是 shell module

shell

shell module 跟 command module 的差別就是可以處理 pipe 之類的做法
可以把在系統習慣的執行方式透過 shell module 來執行

指令的做法
$ ansible  cenic_master -m shell   -a   'dpkg -l  |  grep ftp'
cenic_master | success | rc=0 >>
ii  ftp                                 0.17-28                             amd64        classical file transfer client

範例
ansible-playbook 方式

建立一個 yml 檔案
$ vi  test_shell_module.yml

- name: Test shell module
 hosts: cenic_master
 tasks:
   - name: Test shell module
     shell: dpkg -l | grep ftp
     register: dpkg
   - debug: var=dpkg.stdout_lines
以 ansible-playbook 指令執行
$ ansible-playbook test_shell_module.yml

PLAY [Test shell module] ******************************************************

GATHERING FACTS ***************************************************************
ok: [cenic_master]

TASK: [Test shell module] *****************************************************
changed: [cenic_master]

TASK: [debug var=dpkg.stdout_lines] *******************************************
ok: [cenic_master] => {
   "var": {
       "dpkg.stdout_lines": [
           "ii  ftp                                 0.17-28                             amd64        classical file transfer client",
           "ii  lftp                                4.4.13-1                            amd64        Sophisticated command-line FTP/HTTP client programs",
           "ii  openssh-sftp-server                 1:6.6p1-2ubuntu2                    amd64        secure shell (SSH) sftp server module, for SFTP access from remote machines"
       ]
   }
}

PLAY RECAP ********************************************************************
cenic_master               : ok=3    changed=1    unreachable=0    failed=0   

另外一個 module 就是 apt

apt

指令的方式
例如 安裝 telnetd 套件
$ ansible cenic_master  -s   -m  apt  -a ' name=telnetd update_cache=yes '

這邊要注意兩件事情
  • -s 代表 sudo , 因為要更新套件庫還有安裝, 所以需要 sudo
  • apt 通常要搭配 update_cache=yes, 來更新, 以免套件安裝有問題


範例
ansible-playbook 方式

建立一個 yml 檔案, 這邊我們透過 with_item 的方式一次安裝多個套件
$ vi  test_apt_module.yml

- name: Using Iteration (with_item) to install multiple packages
 hosts: cenic_master
#  sudo: True 這邊學到 sudo 不一定要放在一般選項內, 也可以放在 task 內
 tasks:
   - name: install apt packages
# 於 apt moudule 使用 sudo
     sudo: True
# 使用 pkg 加上 {{ item }} 配合後面的 with_item 來安裝多個套件
     apt: pkg={{ item }} update_cache=yes cache_valid_time=3600
# 配合前面的 {{ item }} 來安裝多套件
     with_items:
       - openjdk-7-jdk
       - wget
       - scala

以 ansible-playbook 指令執行

$ ansible-playbook  test_apt_module.yml

PLAY [Using Iteration (with_item) to install multiple packages] ***************

GATHERING FACTS ***************************************************************
ok: [cenic_master]

TASK: [install apt packages] **************************************************
changed: [cenic_master] => (item=openjdk-7-jdk,wget,scala)

PLAY RECAP ********************************************************************
cenic_master               : ok=2    changed=1    unreachable=0    failed=0   



okay, 今天先到這邊
~ enjoy it

星期一, 11月 16, 2015

Ansible 小記 - ping module 與 command module

Ansible 小記 - ping module 與 command module

接下來就開始介紹 Ansible Module

Ansible 的 Module 可以在官網找到所有的 Module, 這邊是按照分類的 Index http://docs.ansible.com/ansible/modules_by_category.html

今天先來介紹兩個 module

首先是 ping module
ping

這大概是我在使用 Ansible 第一個會執行的 module, 測試與遠端主機是否有連線成功

使用 ansible 的指令語法就是
ansible  對象  -m  module名稱
  • -m 是 moudle 的意思

範例為對所有的主機執行 ping module, 成功就會回覆 pong
$ ansible  all   -m   ping
cenic_slave | success >> {
   "changed": false,
   "ping": "pong"
}


接下來介紹  command module
command module 適合執行單一指令, 沒有做其他處理的狀況

command
  • 執行 command, 但是如果是會用到 pipe | 這樣的方式就要透過 shell module 來執行
    • 例如
      • ansible  testserver  -m command  -a  'uptime'
    • 但是如果要執行指令進行二次處理
      • ansible  testserver  -m  command  -a  'dpkg -l | grep nginx'   這樣是不行的, 要使用 shell module

範例
ansible 指令方式

ansible  對象  -m  module名稱  -a  “參數
  • -m 是 module 的意思
  • -a  是 module arguments

所以這邊對機器執行 command module 去執行 uptime 指令
$ ansible  cenic_master   -m   command   -a   'uptime'
cenic_master | success | rc=0 >>
06:38:13 up 20 days,  2:08,  2 users,  load average: 0.00, 0.01, 0.05

範例
ansible-playbook 方式

建立一個 yml 檔案, debug 的部分日後再講
$ vi  4_output_of_command.yml
- name: show return value of command module
 hosts: testserver
 tasks:
   - name: capture output of id command
     command: id -un
     register: login
# 使用 變數 {{ xxx.stdout }} 將輸入導到螢幕前
# - debug 後面的 msg 也是有規定名稱, 不能用自定命名
   - debug: msg="Logged in as user {{ login.stdout }}"

使用 ansible-playbook 指令執行

使用 ansible 的指令語法就是

ansible-playbook  指定的yml檔案

$ ansible-playbook  4_output_of_command.yml

PLAY [show return value of command module] ************************************

GATHERING FACTS ***************************************************************
ok: [cenic_master]

TASK: [capture output of id command] ******************************************
changed: [cenic_master]

TASK: [debug msg="Logged in as user {{ login.stdout }}"] **********************
ok: [cenic_master] => {
   "msg": "Logged in as user maxhuang"
}

PLAY RECAP ********************************************************************
cenic_master               : ok=3    changed=1    unreachable=0    failed=0   

okay, 今天先到這邊

~ enjoy it

星期日, 11月 15, 2015

Ansible 小記 - 用 playbook 安裝 nginx

上次介紹 playbook
接下來要在沒有很了解 playbook 以及 module 的情況下, 來實驗書上的第一個 playbook - nginx 安裝.

接下來的筆記就會規劃 不同 module 的指令還有 playbook 的實作

Lab: 安裝 nginx without TLS

在 playbook 目錄下
$ vi   web-notls.yml
- name: Configure webserver with nginx
 hosts: webservers
 sudo: True
 tasks:
   - name: install nginx
     apt: name=nginx update_cache=yes

   - name: copy nginx config file
     copy: src=files/nginx.conf  dest=/etc/nginx/sites-available/default

   - name: enable configuration
     file: >
       dest=/etc/nginx/sites-enabled/default
       src=/etc/nginx/sites-available/default
       state=link

   - name: copy index.html
     template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html
       mode=0644

   - name: restart nginx
     service: name=nginx state=restarted


建立 預設的 conf 檔案( 對應上方的設定 )
在 playbook 目錄下
$ mkdir  files

建立設定檔
$ vi  files/nginx.conf
server {
       listen 80 default_server;
       listen [::]:80 default_server ipv6only=on;

       root /usr/share/nginx/html;
       index index.html index.htm;

       server_name localhost;

       location / {
                try_files $uri $uri/ =404;
       }
}

建立 templates 目錄
在 playbook 目錄下
$ mkdir  templates

建立首頁的範本
$ vi   templates/index.html.j2
<html>
<head>
   <title>Welcome to ansible</title>
</head>
<body>
<h1>nginx, configured by Ansible</h1>
<p>If you can see this, Ansible successfully installed nginx.</p>

<p>{{ ansible_managed }}</p>
</body>
</html>

修改 hosts 檔案( 因為上面的 web-notls.yml 對象是 webservers 群組  )
在 playbook 目錄下
新增 webservers 群組
$ vi  hosts
ubuntu_utah ansible_ssh_host=pcvm2-13.utah.geniracks.net

ubuntu_cenic ansible_ssh_host=pcvm2-28.instageni.cenic.net

[geni]
ubuntu_utah
ubuntu_cenic

[webservers]
ubuntu_utah

測試群組
$ ansible   webservers   -m ping
ubuntu_utah | success >> {
   "changed": false,
   "ping": "pong"
}



觀察目前目錄下物件
$ ls -R
ansible.cfg   files         hosts         templates     web-notls.yml

./files:
nginx.conf

./templates:
index.html.j2

執行  playbook

$ ansible-playbook   web-notls.yml

驗證 webservers 主機的 port 80

成功之後來進行另外一個 Lab

Lab: 使用 TLS support 的 nginx

在 playbooks 目錄下
$ vi   web-tls.yml

- name: Configure webserver with nginx and tls
 hosts: webservers
 sudo: True
 vars:
   key_file: /etc/nginx/ssl/nginx.key
   cert_file: /etc/nginx/ssl/nginx.crt
   conf_file: /etc/nginx/sites-available/default
   server_name: localhost
 tasks:
   - name: Install nginx
     apt: name=nginx update_cache=yes cache_valid_time=3600

   - name: create directories for ssl certificates
     file: path=/etc/nginx/ssl state=directory

   - name: copy TLS key
     copy: src=files/nginx.key dest={{ key_file }} owner=root mode=0600
     notify: restart nginx

   - name: copy TLS certificate
     copy: src=files/nginx.crt dest={{ cert_file }}
     notify: restart nginx

   - name: copy nginx config file
     template: src=templates/nginx.conf.j2 dest={{ conf_file }}
     notify: restart nginx

   - name: enable configuration
     file: dest=/etc/nginx/sites-enabled/default src={{ conf_file }} state=link
     notify: restart nginx

   - name: copy index.html
     template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644

 handlers:
   - name: restart nginx
     service: name=nginx state=restarted

手動建立憑證
在 playbooks 目錄下
使用 openssl 指令建立憑證
$ openssl  req  -x509  -nodes  -days 3650 -newkey rsa:2048 -subj /CN=localhost -keyout files/nginx.key -out files/nginx.crt
Generating a 2048 bit RSA private key
........................................+++
...............................................+++
writing new private key to 'files/nginx.key'
-----

驗證輸出
$ ls   files/
nginx.conf nginx.crt  nginx.key

建立 nginx.conf.j2  給支援 tls 設定檔使用 ( 跟沒有 TLS 的差異為紅色部分 )

在 playbooks 目錄下

$ vi  templatess/nginx.conf.j2

server {
       listen 80 default_server;
       listen [::]:80 default_server ipv6only=on;

       listen 443 ssl;

       root /usr/share/nginx/html;
       index index.html index.htm;

       server_name {{ server_name }};
       ssl_certificate {{ cert_file }};
       ssl_certificate_key {{ key_file }};

       location / {
                try_files $uri $uri/ =404;
       }
}

使用之前已經安裝過的主機測試  playbook
$ ansible-playbook   web-tls.yml
PLAY [Configure webserver with nginx and tls] *********************************

GATHERING FACTS ***************************************************************
ok: [ubuntu_utah]

TASK: [Install nginx] *********************************************************
ok: [ubuntu_utah]

TASK: [create directories for ssl certificates] *******************************
ok: [ubuntu_utah]

TASK: [copy TLS key] **********************************************************
ok: [ubuntu_utah]

TASK: [copy TLS certificate] **************************************************
ok: [ubuntu_utah]

TASK: [copy nginx config file] ************************************************
changed: [ubuntu_utah]

TASK: [enable configuration] **************************************************
ok: [ubuntu_utah]

TASK: [copy index.html] *******************************************************
changed: [ubuntu_utah]

NOTIFIED: [restart nginx] *****************************************************
changed: [ubuntu_utah]

PLAY RECAP ********************************************************************
ubuntu_utah                : ok=9    changed=3    unreachable=0    failed=0

測試完全新的 ubuntu_cenic 也okay

今天先到這邊

~ enjoy it