第三章 3-4~3-5 Ansible playbooks常用模塊介紹
一、Ansible playbooks常用模塊介紹
1.1、File模塊
name:表示本次任務的名稱
file:表示任務的類型
path:表示生成到目標主機中的文件的路徑
state=touch:touch表示需要去創建一個文件
mode:表示文件的權限
owner:表示文件所屬用戶名
group:表示文件所屬用戶組名稱
1.2、Copy模塊
name:表示本次任務的名稱
copy:表示本次是文件的複製
remote_src=no:表示我們需要將本機中的文件複製到目標主機當中
src:表示源文件的路徑
dest:表示目標的文件路徑
mode:表示文件的權限
force=yes:表示強制執行
1.3、Stat模塊
校驗文件是否存在
name:表示本次任務的名稱
stat:表示調用的是stat模塊
register:表示把前面校驗文件是否存在的狀態傳遞給這裏的變量
1.4、Debug模塊
當script_stat.stat.exists爲true時打印“foo.sh exists”語句。
debug:msg:定義條件語句
when:表示條件語句
1.5、Command/Shell模塊
第一句的意思是:執行foo.sh文件
第二句的意思是:打印test到test.txt文件中(“>”爲重定向符號)
下面兩個語句的區別在於shell可以使用重定向符;
1.6、Template模塊
這裏主要注意的是在下面的傳送過程中,文件中的變量可以調用到模板中定義的參數。
src:表示源文件的地址
dest:表示傳送的目標主機中的地址
1.7、Packaging模塊
下面的語句表示安裝一個nginx最新版
pkg:表示安裝包的名稱
state:表示版本號
1.8、Service模塊
下面的語句表示開啓Nginx服務
name:表示服務的名稱
state:表示需要達到的服務的狀態
1.9、綜合上面的模塊的案例
二、Ansible playbooks案例演示
2.1、ansible主機的準備
首先切換到ansible主機的deploy用戶,然後使用下面的兩個語句,在Python3.6的虛擬環境下啓動ansible
下圖中,我已經啓動了。
2.2、testbox上準備
# 進入到testbox主機界面
# 創建兩個系統用戶
useradd foo
useradd deploy
# 創建一個nginx的文件目錄
mkdir /etc/nginx
# 安裝一個Nginx的yum源,防止playbook運行安裝的時候出錯
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安裝完後退出testbox
exit
2.3、文件創建案例-File模塊
2.3.1、編寫main.yml文件
# 進入到test_playbooks文件夾
cd test_playbooks/
# 編輯main.yml文件
vim roles/testbox/tasks/main.yml
添加一個任務
2.3.2、測試
# 測試上面編寫的代碼
ansible-playbook -i inventory/testenv ./deploy.yml
- name: create a file
file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
小竅門:
# 可以使用下面的代碼去查看是否創建了該文件,如果創建會返回該文件的相關信息
ssh [email protected] ls -al /root/foo.txt
2.4、文件複製案例-Copy模塊
2.4.1、環境準備
首先在/roles/testbox文件夾下創建一個files文件夾
# 創建files文件夾
mkdir roles/testbox/files
# 創建並編寫foo.sh文件
vim roles/testbox/files/foo.sh
2.4.2、編寫main.yml文件
# 編寫main.yml文件
vim roles/testbox/tasks/main.yml
- name: copy a file
copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'
2.4.3、測試腳本
2.5、案例-Stat,Debug模塊
2.5.1、編寫main.yml文件
- name: check if foo.sh is exists
stat: 'path=/root/foo.sh'
register: script_stat
- debug: msg="foo.sh exists"
when: script_stat.stat.exists
2.5.2、測試
2.6、案例-Command模塊
2.6.1、編寫main.yml文件
- name: run the script
command: 'sh /root/foo.sh'
2.6.2、測試
2.7、案例-Template模塊
2.7.1、編寫testenv文件
首先我們需要準備環境,在testenv清單文件中添加一些配置項
在roles/testbox/目錄下創建templates目錄
# 創建文件夾
mkdir roles/testbox/templates
# 創建Nginx的模板文件
vim roles/testbox/templates/nginx.conf.j2
2.7.2、編寫nginx.conf.j2文件
# For more information on configuration, see:
user {{ user }};
worker_processes {{ worker_processes }};
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections {{ max_open_file }};
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
#include /etc/nginx/conf.d/*.conf;
server {
listen {{ port }} default_server;
server_name {{ server_name }};
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root {{ root }};
index index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
2.7.3、編寫main.yml文件
下面的文件中添加了三個任務,分別是:
- 1.將Nginx的配置文件通過template模板方式傳送到目標服務器上
- 2.保證Nginx服務是最新的版本
- 3.啓動Nginx服務
- name: write the nginx config file
template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: ensure nginx is at the latest version
yum: pkg=nginx state=latest
- name: start nginx service
service: name=nginx state=started
2.7.4、測試
運行成功後可以使用下面的語句查看生成通過Template模塊,從下面的截圖中可以看到之前nginx.conf.j2上的變量已經被testenv上配置的變量所替換。
ssh [email protected] cat /etc/nginx/nginx.conf
查看testbox主機的Nginx服務是否已開啓
ssh [email protected] ps -ef | grep nginx
自此可以看到把上面常用模塊的連貫案例已實現成功!!!