Ansible自動化運維工具,是用來實現一臺主機對多臺主機進行操做的開源軟件。
主要功能:php
Ansible的安裝須要使用epel源進行安裝,因此在安裝以前須要配置epel源
主要配置文件
/etc/ansible/hosts :主機清單配置文件
/etc/ansible/ansible.cnf:主配置文件
/etc/ansible/roles:角色定義目錄html
相關命令介紹
1.幫助類相關命令node
2.一次性執行命令相關python
3.ansible-playbook相關命令
測試相關ios
1.ping:測試與指定主機是否通信正常 [root@ansible ~]# ansible ansible.test.com -m ping ansible.test.com | SUCCESS => { "changed": false, "ping": "pong" } 2.command:遠程在指定主機執行命令,該模塊是直接在內核級執行,因此沒有bash的一些符號 相關參數: chdir:指定運行命令目錄 creates:建立文件或目錄,若文件或目錄存在,則不執行 removes:刪除文件或目錄,若文件或目錄不存在時,則不執行 實例: [root@ansible ~]# ansible ansible.test.com -m command -a "date" ansible.test.com | SUCCESS | rc=0 >> Tue Jun 12 03:23:01 EDT 2018 3.shell:遠程在指定主機執行命令,該模塊時基於bash運行,因此bash相關符號都支持 相關參數: chdir:指定運行命令目錄 creates:建立文件或目錄,若文件或目錄存在,則不執行 removes:刪除文件或目錄,若文件或目錄不存在時,則不執行 實例: [root@ansible ~]# ansible ansible.test.com -m shell -a "echo 123456 | passwd --stdin test1" ansible.test.com | SUCCESS | rc=0 >> Changing password for user test1. passwd: all authentication tokens updated successfully. 4.group:遠程在指定主機建立或刪除組 相關參數: gid:指定組的GID name:指定組名 state:指定刪除仍是建立,present | absent system:指定對應組是否爲系統組 實例: [root@ansible ~]# ansible ansible.test.com -m group -a "name=ansible state=present gid=2000" ansible.test.com | SUCCESS => { "changed": true, "gid": 2000, "name": "ansible", "state": "present", "system": false } [root@ansible ~]# ansible ansible.test.com -m group -a "name=ansible state=absent gid=2000" ansible.test.com | SUCCESS => { "changed": true, "name": "ansible", "state": "absent" } 5.user:在指定主機建立或刪除用戶 相關參數: name:指定用戶名 group:指定主組 uid:指定用戶的uid goups:指定附加組 home:指定家目錄 password:指定用戶密碼 state:指定狀態,若和狀態不想同則做出操做 present | absent system:設定用戶爲系統用戶 shell:設定登入shell 實例: [root@ansible ~]# ansible ansible.test.com -m user -a "name=ansible uid=2000 password=123456 shell=/bin/sh" ansible.test.com | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 2000, "home": "/home/ansible", "name": "ansible", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/sh", "state": "present", "system": false, "uid": 2000 } [root@ansible ~]# ansible ansible.test.com -m user -a "name=ansible state=absent" ansible.test.com | SUCCESS => { "changed": true, "force": false, "name": "ansible", "remove": false, "state": "absent" } 6.copy:從本地複製文件至指定服務器 相關配置: src:本地文件路徑 dest:遠程服務器目錄 owner:指定文件全部者 group:指定文件所屬組 mode:指定文件權限 content:將指定內容複製至指定服務器 實例: [root@ansible ~]# ansible ansible.test.com -m copy -a "src=issue dest=/tmp owner=memcached group=root mode=0444" ansible.test.com | SUCCESS => { "changed": true, "checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540", "dest": "/tmp/issue", "gid": 0, "group": "root", "md5sum": "f078fe086dfc22f64b5dca2e1b95de2c", "mode": "0444", "owner": "memcached", "size": 23, "src": "/root/.ansible/tmp/ansible-tmp-1528789534.05-118540157876873/source", "state": "file", "uid": 995 } 7.fetch:從遠程服務器複製文件至本地,爲了能夠存多個服務器相同文件名,是建立一個以主機名爲目錄將複製文件放入目錄中實現 相關配置: src:遠程地址 dest:本地地址 實例: [root@ansible ~]# ansible ansible.test.com -m fetch -a "src=/etc/passwd dest=/tmp" ansible.test.com | SUCCESS => { "changed": true, "checksum": "49ed3c6ed3359b339b94c0b4b69d596974de5ff2", "dest": "/tmp/ansible.test.com/etc/passwd", "md5sum": "256651c73ef0e89bc7cd5cffe786f27e", "remote_checksum": "49ed3c6ed3359b339b94c0b4b69d596974de5ff2", "remote_md5sum": null } 8.file:對遠程文件或目錄進行操做 相關配置: path:指定遠程操做文件 state:指定path文件的類型,能夠是dreectory、link、hard、file、touch,也能夠是present或absent src:本地路徑,用於在指定鏈接文件時使用 owner:指定文件屬主 group:指定文件屬組 mode:指定文件權限 實例: [root@ansible ~]# ansible ansible.test.com -m file -a "path=/tmp/file state=directory" ansible.test.com | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/file", "size": 6, "state": "directory", "uid": 0 } 9.get_url:批量下載 相關配置: url:指定url路徑 owner:指定文件屬主 group:指定文件屬組 mode:指定文件權限 dest:指定目標路徑,必須是一個目錄 實例: [root@ansible ~]# ansible ansible.test.com -m get_url -a "url=https://mirrors.aliyun.com/gentoo-portage/app-accessibility/SphinxTrain/SphinxTrain-1.0.8.ebuild dest=/tmp" ansible.test.com | SUCCESS => { "changed": true, "checksum_dest": null, "checksum_src": "b1a87e9c2e46841866387b11805d2cdf3d240577", "dest": "/tmp/SphinxTrain-1.0.8.ebuild", "gid": 0, "group": "root", "md5sum": "a52bed11534400b1901760bc1bcc9dd9", "mode": "0644", "msg": "OK (912 bytes)", "owner": "root", "size": 912, "src": "/tmp/tmpNHTpAE", "state": "file", "uid": 0, "url": "https://mirrors.aliyun.com/gentoo-portage/app-accessibility/SphinxTrain/SphinxTrain-1.0.8.ebuild" } 10.git:用於下載git代碼倉庫命令,該命令須要安裝git軟件包才能夠下載 相關配置: repo:指定倉庫地址 dest:指定目標路徑 version:指定版本號,默認最新版本號 11.cron:爲指定主機定義計劃任務 相關配置: minute:定義分鐘 day:定義天 month:定義月 weekday:定義周 hour:定義時 job:定義命令 name:計劃任務名 state:present:建立 | absent:刪除 實例: [root@ansible ~]# ansible ansible.test.com -m cron -a "name=mycron minute=*/5 job=date" ansible.test.com | SUCCESS => { "changed": true, "envs": [], "jobs": [ "mycron" ] } 12.yum:軟件包的安裝,默認輸出格式不對,這裏是整理過的格式 相關配置: name:程序包名,能夠帶版本號,若不指定,默認安裝最新版 state:設置安裝,卸載 installed | removed enablerepo:啓用某倉庫 diablerepo:禁用某倉庫 實例: [root@ansible ~]# ansible ansible.test.com -m yum -a "name=tree state=installed" ansible.test.com | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ ] } 13.service:服務相關 相關配置: name:指定服務名稱 state:指定服務狀態,能夠是started、restarted、reloaded、stoped enabled:是否開機自啓 true runlevel:定義級別CentOS7版本不須要指定 實例: [root@ansible ~]# ansible ansible.test.com -m service -a "name=memcached state=started" ansible.lin.com | SUCCESS => { ·······這裏顯示的都是一些該服務的環境參數 } "warnings": [] } 14.hostname:設置遠程主機的主機名,通常不適用 15.pip:用於安裝python模塊 16.npm:用於安裝JS模塊 17.setup:顯示當前系統中全部的變量,能夠在配置文件中直接調用 顯示: ansible.test.com | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.1.161", "192.168.1.111", "192.168.1.112" ], "ansible_all_ipv6_addresses": [ "fe80::880b:c17f:5022:67c3", "fe80::acd6:5a07:96b5:c5d4", "fe80::805f:d6bd:3a96:c3dc" ], "ansible_architecture": "x86_64", "ansible_bios_date": "07/02/2015", "ansible_bios_version": "6.00", "ansible_cmdline": { "BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64", "LANG": "en_US.UTF-8", "crashkernel": "auto", "quiet": true, "rd.lvm.lv": "centos/swap", "rhgb": true, "ro": true, "root": "/dev/mapper/centos-root" }, "ansible_date_time": { "date": "2018-06-12", "day": "12", "epoch": "1528792866", "hour": "04", "iso8601": "2018-06-12T08:41:06Z", "iso8601_basic": "20180612T044106138105", "iso8601_basic_short": "20180612T044106", "iso8601_micro": "2018-06-12T08:41:06.138178Z", 18.template:用於複製模板配置文件,模板配置文件必須爲 .j2格式 相關配置: src:源文件 dest:目標路徑 owner:屬主 group:數組 mode:權限 Jinja2: 字面量: 字符串:使用單引號或雙引號; 數字:整數,浮點數; 列表:[item1, item2, ...] 組:(item1, item2, ...) 字典:{key1:value1, key2:value2, ...} 布爾型:true/false 算術運算: +, -, *, /, //, %, ** 比較操做: ==, !=, >, >=, <, <= 邏輯運算: and, or, not
YAMLnginx
YAML:YAML(/ˈjæməl/,尾音相似camel駱駝)是一個可讀性高,用來表達數據序列的格式。YAML參考了其餘多種語言,包括:C語言、Python、Perl,並從XML、電子郵件的數據格式(RFC 2822)中得到靈感。Clark Evans在2001年首次發表了這種語言,另外Ingy döt Net與Oren Ben-Kiki也是這語言的共同設計者。目前已經有數種編程語言或腳本語言支持(或者說解析)這種語言。 YAML是"YAML Ain't a Markup Language"(YAML不是一種標記語言)的遞歸縮寫。在開發的這種語言時,YAML 的意思實際上是:"Yet Another Markup Language"(還是一種標記語言),但爲了強調這種語言以數據作爲中心,而不是以標記語言爲重點,而用反向縮略語重命名。 YAML的語法和其餘高級語言相似,而且能夠簡單表達清單、散列表,標量等數據形態。它使用空白符號縮進和大量依賴外觀的特點,特別適合用來表達或編輯數據結構、各類配置文件、傾印除錯內容、文件大綱(例如:許多電子郵件標題格式和YAML很是接近)。
Playbook的核心元素git
* Hosts:主機 * Tasks:任務列表 * Varoables:變量 * Templates:包含了模板語法文本文件 * Handlers:特定條件觸發的任務 * Roles:角色,將重複須要執行的命令寫成一個單獨的功能模塊,當須要使用時,直接使用便可,相似於函數
Playbook的基本組件web
- hosts: 運行指定主機 remote_user: 遠程主機執行任務所用的身份,通常爲root tasks: 任務列表 選項: 模塊 參數 模塊: 參數 handlers:特定模式下執行,須要在上述指定notify,在notify後面指定字符串,在下面至指定觸發便可 -name:
實例:shell
[root@ansible ansible]# vim httpd.yml - hosts: ansible.test.com remote_user: root tasks: - name: install httpd package yum: name=httpd state=installed - name: start httpd servier service: name=httpd state=started [root@ansible ansible]# ansible-playbook httpd.yml PLAY [ansible.test.com] ********************************************************* TASK [setup] ******************************************************************* ok: [ansible.test.com] TASK [install httpd package] *************************************************** changed: [ansible.test.com] TASK [start httpd servier] ***************************************************** changed: [ansible.test.com] PLAY RECAP ********************************************************************* ansible.test.com : ok=3 changed=2 unreachable=0 failed=0
variables:傳遞參數npm
1.facts:能夠直接調用,使用setup查看 2.用戶自定義變量 * ansible-playbook -e 變量 * -var1: value1 3.經過reles傳遞變量 4.在/etc/ansible/hosts文件中定義主機或是能夠直接定義 ip var=value1 [groupname::vars] varname=valus
條件測試:
when語句,在task中使用,當知足條件式纔會執行
tasks: - name: install conf file to centos7 template: src=files/nginx.conf.c7.j2 when: ansible_distribution_major_version == "7" - name: install conf file to centos6 template: src=files/nginx.conf.c6.j2 when: ansible_distribution_major_version == "6"
循環,迭代,可以將重複任務循環完成
對迭代項引用,固定變量名爲"item",然後給定列表
- name: install some packages yum: name={{ item }} state=present with_items: - nginx - memcached - php-fpm - name: install some packages yum: name={{ item }} state=present with_items: - { name: 'nginx', group: 'nginx' } - { name: 'nginx', group: 'nginx' } - { name: 'nginx', group: 'nginx' }
roles角色的定義:
角色目錄結構
nginx/:指定角色名稱,當須要調用時,也是根據該名稱調用,在一下目錄下的每個目錄,若須要使用,至少應該擁有一個main.yml文件,若須要擴展,則可使用include進行擴展
利用roles實現模塊化nginx
1.建立目錄結構 [root@ansible ansible]# tree /etc/ansible/roles/nginx/ /etc/ansible/roles/nginx/ ├── default ├── files │ ├── proxy.html │ └── web.html ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ ├── nginx.conf.j2 │ └── proxy.conf.j2 └── vars └── main.yml 2.建立 tasks目錄下main.yml [root@ansible nginx]# cat tasks/main.yml - name: install nginx package yum: name=nginx state=installed - name: create conf.d directory file: path=/etc/nginx/conf.d state=directory - name: install web conf file template: src=templates/nginx.conf.j2 dest={{ nginx }}/nginx.conf when: servertype == 'web' notify: 'restart nginx server' - name: install proxy conf file template: src=templates/proxy.conf.j2 dest={{ nginx }}/nginx.conf notify: 'restart nginx server' when: servertype == 'proxy' - name: create web index file: path={{ webpath }} state=directory when: servertype == 'web' - name: create proxy index file: path={{ proxypath }} state=directory when: servertype == 'proxy' - name: copy web index file copy: src=files/web.html dest={{ webpath }}/index.html when: servertype == 'web' - name: copy proxy index file copy: src=files/proxy.html dest={{ proxypath }}/index.html when: servertype == 'proxy' - name: start nginx service service: name=nginx state=started enabled=true 3.建立對應的變量vars/main.yml [root@ansible nginx]# cat vars/main.yml nginx: /etc/nginx/conf.d/ servertype: web webpath: /app/web/ proxypath: /app/proxy/ 4.建立handles目錄下main.yml [root@ansible nginx]# cat handlers/main.yml - name: restart nginx server service: name=nginx state=restarted 5.配置模板文件templates [root@ansible nginx]# cat templates/proxy.conf.j2 unstream tomser { server tomcat1.lin.com:8080; server tomcat2.lin.com:8080; } server { listen 80; server_name {{ ansible_fqdn }}; index index.html; root {{ proxypath }}; location / { proxy_pass http://tomser/; } } [root@ansible nginx]# cat templates/nginx.conf.j2 server { listen 80; server_name {{ ansible_nodename }}; root {{ webpath }}; index index.jsp index.html; location / { proxy_pass http://{{ ansible_fqdn }}:8080; } } 6.建立對應的測試主頁 [root@ansible nginx]# cat files/ proxy.html web.html [root@ansible nginx]# cat files/* <h1>proxy</h2> <h1>test</h1> 7.根據參數來選擇安裝的類型 [root@ansible ansible]# ansible-playbook nginx.yml 或 [root@ansible ansible]# ansible-playbook -e server=proxy nginx.yml