文章目錄
- ansible
- 配置文件的優先級
- 嘗試開始進行操作
- ansible常用模塊
- ansible 的playbook
- 示例
- 安裝php
- playbook中變量的引用
ansible
yum install -y ansible
測試是否可用 ansible localhost -m ping
/etc/ansible/ansible.cfg :主配置文件,配置 ansible 工作特性
/etc/ansible/hosts :配置主機清單文件
/etc/ansible/roles/ :存放 ansible 角色的目錄主配置文件存在 /etc/anible/ansible.cfg
指定特權用戶
[privilege_escalation]
become=True
become_method=sudo
become_user=user
become_ask_pass=Falseansible;adhoc;playbook;tasks;Roles;Roles方式編排web集群架構;
配置文件的優先級
1) 最先查找 $ANSIBLE_CONFIG 變量
2) 其次查找當前項目目錄下 ansible.cfg
3) 然后查找用戶家目錄下的 .ansible.cfg
4) 最后查找 /etc/ansible/ansible.cfgInventory 文件主要用來填寫被管理主機以及主機組信息;(邏輯上定義);
默認 Inventory 文件為 /etc/ansible/hosts ;
當然也可以自定義一個文件,當執行 ansible 命令時使用 -i 選項指定 Inventory
文件位置;
配置主機清單
[webservers]
172.16.1.7
172.16.1.8 ansible_become=yesansible_become=yes 這個的意思是加上sudo
嘗試開始進行操作
ad-hoc執行步驟
1.加載自己的配置文件,默認 /etc/ansible/ansible.cfg ;
2.查找對應的主機配置文件,找到要執行的主機或者組;
3.加載自己對應的模塊文件,如 command ;
4.通過 ansible 將模塊或命令生成對應的臨時 py 文件,并將該文件傳輸至遠
程服務器對應執行用戶 $HOME/.ansible/tmp/ansible-tmp-number/XXX.PY ;
5.執行用戶家目錄的 `` 文件;
6.給文件 +x 執行;
7.執行并返回結果;
8.刪除臨時 py 文件, sleep 0 退出;使用 ad-hoc 執行一次遠程命令,注意觀察返回結果的顏色;
綠色: 代表被管理端主機沒有被修改
黃色: 代表被管理端主機發現變更
紅色: 代表出現了故障,注意查看提示

ansible常用模塊
command模塊
[root@manger ~]# ansible localhost -m command -a 'chdir=/root
echo $PWD'ansible localhost -m command -a
'creates=/data/file ifconfig eth0'shell 模塊
參數 選項 含義
chdir chdir /opt 執行ansible時,切換到指定的目錄
creates creates /data/file 如果文件存在,則跳過執行
removes removes /data/file 如果文件存在,則執行
ansible localhost -m shell -a "ifconfig eth0|awk
'NR==2' "scripts 模塊ansible webservers -m script -a "/data/yum.sh"copy模塊
ansible webservers -m copy -a "src=./httpd.conf
dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644"file模塊ansible webservers -m file -a
"path=/tmp/foo.conf state=touch mode=666"ansible webservers -m file -a "path=/tmp/foo
state=directory mode=777"
ansible webservers -m file -a "path=/tmp/foo
state=directory owner=root group=root mode=777 recurse=yes"等模塊,有很多平時用不到,這里做一個大概記錄
ansible 的playbook
playbook 是一個 由 yaml 語法編寫的文本文件,它由 play 和 task 兩部分組
成。
play : 主要定義要操作主機或者主機組
task :主要定義對主機或主機組具體執行的任務,可以是一個任務,也可以是多個
任務(模塊)
總結: playbook 是由一個或多個 play 組成,一個 play 可以包含多個 task 任
務。
可以理解為: 使用多個不同的模塊來共同完成一件事情。1) playbook 是對 AD-Hoc 的一種編排方式。
2) playbook 可以持久運行,而 Ad-Hoc 只能臨時運行。
3) playbook 適合復雜的任務,而 Ad-Hoc 適合做快速簡單的任務。
4) playbook 能控制任務執行的先后順序。語法 描述
縮進 YAML使用固定的縮進風格表示層級結構,每個縮進由兩個空格組成, 不能
使用tabs
冒號 以冒號結尾的除外,其他所有冒號后面所有必須有空格。
短橫線
表示列表項,使用一個短橫杠加一個空格。多個項使用同樣的縮進級別
作為同一列表。
示例
$cat installed_httpd.yml
#1.定義play
#2.定義task、(Installed、Configure、Init、Systemd)- hosts: webserverstasks:- name: Installed Httpd Serveryum:name: httpdstate: present- name: Configure Httpd Servercopy:src: ./httpd.conf.j2dest: /etc/httpd/conf/httpd.confowner: "root"group: "root"mode: '0644'backup: yesnotify: Restart Httpd Server- name: Init Httpd Servercopy:src: ./index.html.j2dest: /var/www/html/test.html- name: Systemd Httpd Serversystemd:name: httpdstate: startedenabled: yeshandlers:- name: Restart Httpd Serversystemd:name: httpdstate: restarted上面是用root 用戶執行的
下面是用普通用戶執行
- hosts: webserversbecome: truebecome_user: roottasks:- name: Installed Httpd Serveryum:name: httpdstate: present- name: Configure Httpd Servercopy:src: ./httpd.conf.j2dest: /etc/httpd/conf/httpd.confowner: "nouser"group: "nouser"mode: '0644'backup: yesnotify: Restart Httpd Server- name: Init Httpd Servercopy:src: ./index.html.j2dest: /var/www/html/test.htmlowner: "nouser"group: "nouser"mode: '0644'- name: Systemd Httpd Serversystemd:name: httpdstate: startedenabled: yeshandlers:- name: Restart Httpd Serversystemd:name: httpdstate: restarted檢查語法 :ansible-playbook installed_httpd.yml --
syntax-check執行命令: ansible-playbook installed_httpd.yml
安裝php
cat install_nginx_php.yml
#1.安裝nginx
#2.安裝php
#3.添加nginx虛擬主機,觸發重啟
#4.配置php,連接redis;觸發重啟
#5.部署phpadmin;、- hosts: webserversvars:web_site_directory: /ansible/admin2tasks:- name: Installed Nginx PHP Serveryum:name: "{{ item }}"state: presentloop:- nginx- php71w- php71w-cli- php71w-common- php71w-devel- php71w-embedded- php71w-gd- php71w-mcrypt- php71w-mbstring- php71w-pdo- php71w-xml- php71w-fpm- php71w-mysqlnd- php71w-opcache- php71w-pecl-memcached- php71w-pecl-redis- php71w-pecl-mongodbtags: Install- name: Create Nginx Process Runtime Groupgroup:name: wwwgid: 666tags: Install- name: Create Nginx Process Runtime Useruser:name: wwwuid: 666create_home: notags:- Install- Configure- name: Configure Nginx Nginx.confcopy:src: ./conf/nginx.conf.j2dest: /etc/nginx/nginx.confowner: 'root'group: 'root'mode: '0644'notify: Restart Nginx Servertags: Configure- name: Configure Nginx VHosts ansible.oldxu.com;template:src: ./conf/ansible.oldxu.com.conf.j2dest: /etc/nginx/conf.d/ansible.oldxu.com.confnotify: Restart Nginx Server- name: Check Web Configureshell:cmd: /usr/sbin/nginx -tregister: Check_Nginxchanged_when:- Check_Nginx.stdout.find('successful')- false- name: Configure php php.inicopy:src: "{{ item.src }}"dest: "{{ item.dest }}"mode: "{{ item.mode }}"loop:- { src: "./conf/php.ini.j2", dest: "/etc/php.ini" , mode: "0644" }- { src: "./conf/php-fpm.d.www.conf.j2", dest: "/etc/php-fpm.d/www.conf" , mode: "0644" }notify: Restart PHP Server- name: Systemd Nginx And PHP Serversystemd:name: "{{ item }}"state: startedenabled: yesloop:- nginx- php-fpm# download code- name: Create Web Site Directoryfile:path: "{{ web_site_directory }}"state: directoryowner: 'www'group: 'www'mode: '0755'- name: Unarchive Myadmin Codeunarchive:src: file/phpmyadmin.zipdest: "{{ web_site_directory }}"owner: 'www'group: 'www'handlers:- name: Restart Nginx Serversystemd:name: nginxstate: restarted- name: Restart PHP Serversystemd:name: php-fpmstate: restarted
playbook中變量的引用
變量提供了便捷的方式來管理 ansible 項目中的動態值。 比如 nginx-1.12 ,可能
后期會反復的使用到這個版本的值,那么如果將此值設置為變量,后續使用和修改都
將變得非常方便。這樣可以簡化項目的創建和維護;在 Ansible 中定義變量分為如下三種方式:
1) 通過命令行傳遞變量參數定義
2) 在play文件中進行定義變量
2.1) 通過vars定義變量
2.2) 通過vars_files定義變量
3) 通過inventory在主機組或單個主機中設置變量
3.1) 通過host_vars對主機進行定義
3.2) 通過group_vars對主機組進行定義vars 形式的變量
- hosts: webserversvars:web_packages: httpdftp_packages: vsftpdtasks:- name: Output Variablesdebug:msg:- "{{ web_packages }}"- "{{ ftp_packages }}"輸出結果為"msg": ["httpd","vsftpd"]在 playbook 中使用 vars_files 指定文件作為變量文件,好處就是其他的
playbook 也可以調用;
[root@ansible project1]# cat vars.yml
web_packages: httpd
ftp_packages: vsftpd- hosts: webserversvars_files:- ./vars.ymltasks:- name: Output Variablesdebug:msg:- "{{ web_packages }}"- "{{ ftp_packages }}"playbook 傳送多個變量
ansible-playbook f5.yml -i hosts -e
"web_packages=GeoIP" -e "ftp_packages=telnet"