ansible提供的腳本,遵循規范yaml(一般用於寫配置文件)
可用於配制文件的語言:yaml、xml、json - 冒號后面必須有空格 - 橫線后面必須要空格 - 嚴格保持對齊 - 等號前面不能有空格? yaml數據結構 - 字典 key:value - 列表 (兩種表示方式[], -)
*** 基本命令**
ansible-playbook -h ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check # 白跑,執行但是不會有結果 --list-hosts # 列出符合的主機 -f FORKS, --forks=FORKS # 做並發 --syntax-check # 檢查語法 -k, --ask-pass # 輸入密碼 操作示例: - - hosts: web remote_user: root tasks: - name: createuser user: name=jason666 home=/opt/jason666 uid=4000 ansible-playbook --syntax-check p1.yml # 檢測語法 ansible-playbook -C p1.yml # 干跑 ansible-palybook p1.yml # 真正執行文件
*** 傳參**
- hosts: web tasks: - name: createuser user: name=jason33 # 創建用戶jason33 過段時間又需要創建其他用戶tank33 頻繁修改文件yml明顯不合理 # 解決方案 傳參! # 方式1:命令中傳值 - hosts: web tasks: - name: create{{user}} user: name={{user}} ansible-playbook -e user=jason20 p3.yml # 方式2:hosts文件中主機后面直接添加 [web] 192.168.226.[101:102] user=jason30 # 方式3:hosts文件中新增 [web:vars] user=jason31 # 方式4:yml文件中配置 - hosts: web vars: - user: jason32 tasks: - name: create{{user}} user: name={{user}} # 方式5:了解 - hosts: web tasks: - name: yum yum: name=bc - name: sum shell: echo 11+22|bc register: user - name: echo shell: echo {{user.stdout}} > /tmp/echo.txt - name: create{{user.stdout}} user: name=alex{{user.stdout}} 傳參優先級:-e > playbook > hosts
**tags可以單獨調用任務**
- hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copy - name: start service: name=redis state=started yum install -y redis # 安裝redis ansible-playbook --syntax-check p7.yml # 檢測語法 ansible-playbook -t copy p7.yml # 執行
**handlers**
- hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copy notify: restart # 觸發handlers里面的任務 - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted
- **template**
絕對路徑
- hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=/etc/redis.conf tags: copy notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted
相對路徑
- hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf.j2 tags: copy notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted # 在當前目錄下創建一個templates的目錄,就可以使用相對路徑
**when 類似於后端if判斷**
- hosts: web tasks: - name: copyfile copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt when: ansible_distribution_major_version=="7" - name: copyfile copy: content="小弦切切如私語" dest=/tmp/a.txt when: ansible_distribution_major_version=="6" - hosts: web tasks: - name: copyfile copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt when: user=="4" - name: copyfile copy: content="小弦切切如私語" dest=/tmp/a.txt when: user=="3"
- hosts: web tasks: - name: createuser user: name={{item}} with_items: - jason50 - tank50 - oscar50 - hosts: web tasks: - name: createuser user: name={{item}} with_items: - jason51 - tank51 - oscar51 - name: creategroup group: name={{item}} with_items: - jason60 - tank60 - oscar60
**循環嵌套**
- hosts: web tasks: - name: crateuser user: name={{item.name}} group={{item.group}} with_items: - {"name":jason52,"group":jason60} - {"name":tank52,"group":tank60} - {"name":oscar52,"group":oscar60} demo:安裝nginx並啟動,設置開機自啟動,指定監聽地址為ip地址 - hosts: web tasks: - name: install yum: name=nginx - name: copyfile template: dest=/etc/nginx/nginx.conf src=/etc/nginx/nginx.conf - name: start service: name=nginx state=started enabled=yes
roles
-
-
可以相互調用 - import_tasks: roles/nginx/tasks/install.yml
-
備份方便
# 在任意位置新建roles文件夾 文件夾內建不同的功能 mkdir /data cd /data/ mkdir roles cd roles/ mkdir {nginx,uwsgi,redis,mysql} cd nginx # 之后不需要在文件內按照hosts、tasks、handlers等順序依次書寫 而是以文件夾的形式創建 data/roles/nginx/ ├── files # -- 靜態文件 │ └── c.txt ├── handlers # -- 觸發的任務 │ └── main.yml # - name: restart # service: name=nginx state=restarted ├── tasks # -- 任務(必須的) │ ├── copyfile.yml # 只需要書寫對應的yml格式任務即可 # - name: copyfile # template: dest=/etc/nginx/nginx.conf src=/etc/nginx/nginx.conf │ ├── install.yml # - name: install # yum: name=nginx │ ├── main.yml # (必須有main.yml文件 將其他yml文件導入即可也可以這里面直接寫) # - import_tasks: install.yml # - import_tasks: copyfile.yml # - import_tasks: start.yml # - name: file # copy: dest=/tmp/aaa.txt src=c.txt\ # - name: createuser # user: name={{ user }} # notify: restart │ └── start.yml # - name: start # service: name=nginx state=started enabled=yes ├── templates # -- 動態文件,需要傳遞參數 拷貝nginx配置文件cp /etc/nginx/nginx.conf . │ └── nginx.conf # yum install -y nginx下載並將配置文件弄一弄 └── vars # -- 變量 └── main.yml # {"user":jason70} data/nginx.ym # - hosts: web # roles: # - nginx
# nginx中worker_connections默認1024,理論最大在100萬左右,再多nginx就無法正常啟動了
# nginx中listen監聽ipv4和ipv6,default_server含義:nginx默認多個server,訪問使誰寫了default_server就返回誰
# 上面的代碼如果演示不成功 將nginx配置文件中的default_server刪除即可**
查找順序
- 主文件看到roles,就會去roles目錄下面找對應的目錄 - 先去tasks目錄里面找main.yml入口文件,如果遇到import_task則加載任務 - 如果遇到了template,則去templates目錄里面找文件 - 如果遇到了copy,則去files目錄里面找文件 - 如果遇到了變量,則去vars目錄里面找main.yml文件 - 如果遇到了notify,則去handlers目錄里面找main.yml文件
# roles文件參考網站:https://galaxy.ansible.com/ # 下載nginx相關roles文件 ansible-galaxy install geerlingguy.nginx