ansible安裝配置及最佳實踐roles


ansible是什么?

ansible是一款輕量級配置管理工具,用於遠程批量部署、安裝、配置。類似的還有puppet、saltstack,各有所長,任君自選。

官方文檔:http://docs.ansible.com/ansible/latest/index.html

中文文檔:http://www.ansible.com.cn/index.html

安裝ansible

Linux系統上最簡單的可以使用yum安裝,但由於ansible故不需要后台進程,不需要root權限,不依賴其他軟件,只要有ssh和python環境即可運行,這里采用run from source安裝方式。

#python版本2.5以上,低於2.5需要額外安裝模塊python-simplejson
#安裝pip yum install epel-release -y ; yum install python-pip -y
#開始安裝
python --version
    Python 2.7.5
mkdir /app && cd /app
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible/
source ./hacking/env-setup   #通常每次登錄都要執行一次來配置環境,我們可以將其添加到~/.bash_profile文件中,保證每次登錄都會自動執行
echo "source /app/ansible/hacking/env-setup" >> ~/.bash_profile
pip install paramiko PyYAML Jinja2 httplib2 six

//此外還可以通過pip install ansible的方式來安裝,更加方便

 配置ssh密鑰認證

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.10

 配置inventory文件並測試

inventory文件是ansible的客戶機清單,默認的位置是/etc/ansible/hosts,當然我們可以使用 -i 參數另行指定。

cd /app
mkdir ansible-playbook
cd ansible-playbook/
echo "172.16.1.10" > hosts
ansible all -i /app/ansible-playbook/hosts -m ping

 最佳實踐:playbook+roles方式

"ansible all -i /app/ansible-playbook/hosts -m ping” 這種執行方式被稱為ad-hoc模式,即命令行或交互模式,但任何配置管理工具的官方文檔都會告訴你要用編排的方式進行復雜的部署,例如saltstack里的.sls文件,ansible里的playbook。除此之外,ansible提供了一種目錄樹結構的編排方式,頂層目錄對應roles,里面包含子目錄,比如defaults、files、tasks、templates等,不同的子目錄對應不同的功能,這種方式使得管理和重復調用變得極為方便。

用ansible編譯安裝nginx

注意:

1.roles下子目錄里必須要有main.yml文件,ansible會自動查詢並執行。

2.roles目錄和nginx.yml放在同一級目錄中,或者在ansible.cfg中配置roles的查詢路徑。

3.如果yml中冒號后引用jinja模板以{{開頭,則整行語句需要加上" ",防止yml認為這是個字典。

[root@localhost app]# tree ansible-playbook
ansible-playbook
├── nginx.yml
└── roles
    └── nginx                   #這就是在nginx.yml主文件中指定的role
        ├── defaults
        │   └── main.yml
        ├── files
        │   ├── compile.sh.j2
        │   └── nginx-1.6.3.tar.gz
        ├── handlers
        │   └── main.yml
        ├── tasks
        │   ├── install.yml
        │   └── main.yml
        └── templates
            └── nginx.conf.j2

1.defaults中存放默認的變量,可以通過jinja模板調用
2.files中存放文件、軟件包、腳本等內容,可以被copy、unarchive、script等模塊調用
3.handlers中存放依賴任務,可以被notify關鍵字調用
4.tasks中存放主任務,ansible會首先進行調用
5.templates中存放模板文件,模板中可以使用jinja模板調用defaults中定義的變量,被templates模塊調用

tasks 

nginx的安裝過程包括創建用戶、創建目錄、下載安裝包、下載依賴包、編譯安裝、創建軟鏈接、修改配置文件、測試、啟動這些環節。

[root@localhost nginx]# tree tasks/
tasks/
├── install.yml
└── main.yml

[root@localhost nginx]# less tasks/main.yml 

---
- import_tasks: install.yml

#ansible的playbook以---開頭,然后使用yml語法編寫
#tasks/main.yml中加載install.yml,include方式不久會被淘汰,這里采用import_tasks關鍵字

[root@localhost nginx]# less tasks/install.yml 

---
- name: groupadd nginx   #創建組,存在則忽略,group模塊   - name:說明
  group:                          
    name: "{{ group }}"
    gid: 888

- name: useradd nginx   #創建用戶,存在則忽略,user模塊
  user:
    name: "{{ user }}"
    group: "{{ group }}"
    uid: 888
    createhome: no
    shell: /sbin/nologin

- name: install pcre-devel  #安裝依賴,package模塊
  package:
    name: pcre-devel
    state: latest

- name: install openssl-devel  #安裝依賴,package模塊
  package:
    name: openssl-devel
    state: latest

- name: create /tools    #創建目錄,file模塊
  file: 
    path: /tools
    state: directory  

- name: copy and extract nginx tarball  #解壓壓縮包,unarchive模塊
  unarchive: 
    src: "{{ tarball_name }}"
    dest: /tools

- name: ./configure         #檢查環境,command模塊
  command: ./configure --user={{ user }} --group={{ group }} --prefix=/app/{{ nginx_dir }} --with-http_stub_s
tatus_module --with-http_ssl_module
  args:
    chdir: /tools/{{ nginx_dir }}

- name: make    #編譯,command模塊
  command: make
  args:
    chdir: /tools/{{ nginx_dir }}

- name: make install    #安裝,command模塊
  command: make install
  args:
    chdir: /tools/{{ nginx_dir }}

- name: modify nginx configuration   #修改配置文件,template模塊
  template:
    src: "{{ nginx_configuration }}"
    dest: /app/{{ nginx_dir }}/conf/nginx.conf

- name: make link     #創建軟連接,file模塊
  file:
    src: /app/{{ nginx_dir }}
    dest: /app/nginx
    state: link

- name: test nginx   #測試nginx配置,command模塊
  command: /app/nginx/sbin/nginx -t
  notify:              #調用handlers目錄下的main.yml
    - start nginx

 handlers

[root@localhost nginx]# tree handlers/
handlers/
└── main.yml

[root@localhost nginx]# less handlers/main.yml 

---
- name: start nginx     #notify下面指定的內容在name這里定義
  command: /app/nginx/sbin/nginx

#這里只是演示,實際批量部署不需要nginx -t 這一步

 files

[root@localhost nginx]# tree files/
files/
└── nginx-1.6.3.tar.gz

#ansible中unarchive、copy等模塊會自動來這里找文件,從而我們不必寫絕對路徑,只需寫文件名

 templates

[root@localhost nginx]# tree templates/
templates/
└── nginx.conf.j2

#一般來說,模板文件中都會使用jinja2模板,所以通常我們在模板文件后加上.j2后綴,但不是必須的

[root@localhost nginx]# less templates/nginx.conf.j2 

    server {
        listen       {{ nginx_port }};    #這里使用jinja模板引用變量
        server_name  localhost;


#template模塊會將模板文件中的變量替換為實際值,然后覆蓋到客戶機指定路徑上

 defaults

[root@localhost nginx]# tree defaults
defaults
└── main.yml

[root@localhost nginx]# less defaults/main.yml 

user: nginx
group: nginx
tarball_name: nginx-1.6.3.tar.gz
nginx_configuration: nginx.conf.j2
nginx_dir: nginx-1.6.3
nginx_port: 2223             #這是我們剛才在模板文件中使用的變量

#defaults中的變量優先級最低,通常我們可以臨時指定變量來進行覆蓋

 執行playbook

到了激動人心的時刻,ansible的好處在於什么都不用配,直接就能用,所以這里我們將inventory、nginx.yml、roles目錄放在同一級目錄ansible-playbook下,便於管理

#首先看看nginx.yml主文件

[root@localhost ansible-playbook]# less nginx.yml 

---
- name: deploy nginx
  hosts: all
  remote_user: root
  roles:
    - nginx


#hosts表示選擇哪些主機進行部署
#remote_user表示選擇哪個用戶進行部署
#roles表示選擇部署什么內容

#當然,這里還可以通過字典的方式指定不同的變量
---
- name: deploy nginx
  hosts: all
  remote_user: root
  roles:
    - { role: nginx, nginx_port: 8080 }

我們進入ansible-playbook目錄下,執行 ansible-playbook -i hosts nginx.yml 即可開始部署

[root@localhost ansible-playbook]# ansible-playbook -i hosts nginx.yml 

TASK [Gathering Facts]   **************************************************************************************
ok: [172.16.1.10]

TASK [nginx : groupadd nginx]  *******************************************************************************
ok: [172.16.1.10]

TASK [nginx : useradd nginx] ********************************************************************************
ok: [172.16.1.10]

。。。。。

# TASK[]里的內容就是定義在首行name中的提示內容
# -i 表示自行指定inventory文件

 總結

到這里,ansible的基本用法就展示完畢了,可以看出ansible本身很簡單,重點在於對模塊的掌握情況,建議要經常練習,經常去官方文檔的Module Index部分查看各模塊的用法。

 

 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM