Ansible-playbook

yum
state:狀態(present,absent,latest),表示是安裝還卸載
   present:默認的,表示爲安裝
   latest: 安裝爲最新的版本
   absent:表示刪除

 確認一個軟件包已經安裝,但不去升級它:
            $ ansible webservers -m yum -a "name=lftp state=present"
            確認一個軟件包的安裝版本:
            $ ansible webservers -m yum -a "name=lftp  state=present"

            確認一個軟件包還沒有安裝:
            $ ansible webservers -m yum -a "name=lftp  state=absent"

service 
            確認某個服務在所有的webservers上都已經啓動:
            $ ansible webservers -m service -a "name=httpd state=started"
            在所有的webservers上重啓某個服務
            $ ansible webservers -m service -a "name=httpd state=restarted"
            確認某個服務已經停止:
            $ ansible webservers -m service -a "name=httpd state=stopped"
            使服務開機自啓:
            $ ansible webservers -m service -a "name=httpd enabled=yes"


user  group:  創建或刪除用戶及組
archive    : 壓縮文件或目錄    path   dest    format
unarchive  : 解壓  

synchronize
    需要系統rsync軟件支持,如果沒有,則yum安裝    
    因爲rsync傳文件時也需要用戶密碼,所以最好配置免密登錄。
    目的:將主控方/root/a目錄推送到指定節點的/tmp目錄下
        命令:ansible webservers  -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
        執行效果:
    delete=yes   使兩邊的內容一樣(即以推送方爲主)
    compress=yes  開啓壓縮,默認爲開啓
    --exclude=.git  忽略同步.git結尾的文件
    由於模塊,默認都是推送push。因此,如果你在使用拉取pull功能的時候,可以參考如下來實現
    mode=pull   更改推送模式爲拉取模式。將webservers 節點的/tmp/a目錄拉取到主控節點的/root目錄下命令:
    ansible webservers  -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
    ansible minis -m synchronize -a 'src=/root/tmp1/ dest=/root/tmp1/ compress=yes recursive=yes delete=yes'

cron   計劃任務模塊 (name對ansible生效,如果名字一樣,則會修改)
    # ansible nginx -m cron -a 'name="ansible test1" job="/usr/bin/date >> /tmp/a.txt" minute=* hour=* day=22 month=4'
debug  調試使用
    # ansible nginx -m debug -a 'msg="haha  666"'
template:  可以渲染模板
    它和copy模塊操作相同,只是在遠程複製文件時會先填坑

YAML

YAML 是一門標記性語言,專門用來寫配置文件的語言,非常簡潔和強大,遠比 JSON 格式方便。
YAML 語言(發音 /ˈjæməl/ )的設計目標,就是方便人類讀寫。它實質上是一種通用的數據串行化格式。

基本語法規則:
  1.大小寫敏感
  2.使用縮進表示層級關係
  3.不允許使用TAB鍵來縮進,只允許使用空格鍵來縮進
  4.縮進的空格數量不重要,空格數量要一致,建議使用兩個空格
  5.使用"#"來表示註釋

YAML 支持的數據結構有三種。
對象(字典):鍵值對的集合,又稱爲映射(mapping)/ 哈希(hashes) / 字典(dictionary)
數組(列表):一組按次序排列的值,又稱爲序列(sequence) / 列表(list)
純量(scalars):單個的、不可再分的值

YAML 還有一個小的怪癖. 所有的 YAML 文件(無論和 Ansible 有沒有關係)開始行都應該是 ---. 這是 YAML 格式的一部分, 表明一個文件的開始.
後綴名最好爲: yaml    yml

例1:第一個playbook腳本

cat first.yml

---
- hosts: nginx
  gather_facts: no
  tasks:
    - name: install ntpdate software
      yum: name=ntpdate state=present
    - name: sync time to ntp1.aliyun.com
      command: ntpdate -u ntp1.aliyun.com
    - name: use crontab sync time
      cron: name="sync time" job="/sbin/ntpdate -u ntp1.aliyun.com" minute=5
例2:假如有一臺新服務器,需要對其初始化
    新服務器 -》 安裝系統--> 配置防火牆 selinux --> 配置時區和時間同步  ---> 配置常用軟件或公司內部的監控  --> 需要配置內部yum源
    安裝系統,配置ip 一般可以使用cobbler或手工完成,接下來使用ansible
一個完整的系統初始化腳本
---
- hosts: nginx
  vars:
    ntp_server: ntp2.aliyun.com
  gather_facts: no
  tasks:
    - name: mkdir for backup old yum repo
      file: path=/etc/yum.repos.d/bak  state=directory
    - name: backup old yum repo
      raw: mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
    - name: config 163 yum repo
      copy: src=/etc/ansible/CentOS7-Base-163.repo dest=/etc/yum.repos.d/
    - name: make cache for 163 repo
      raw: yum clean all && yum makecache
    - name: disable selinux  (reboot manuel)
      copy: src=/etc/ansible/config dest=/etc/selinux/config
    - name: start firewalld
      service: name=firewalld state=restarted
    - name: enable firewalld when reboot
      service: name=firewalld enabled=yes
    - name: open 80 port
      command: firewall-cmd --add-service=http
    - name: set timezone
      command: timedatectl set-timezone  Asia/Shanghai
    - name: install ntpdate software
      yum: name=ntpdate state=present
    - name: sync time to {{ ntp_server }}
      command: ntpdate -u {{ ntp_server }}
    - name: use crontab sync time
      cron: name="sync time" job="/sbin/ntpdate -u {{ ntp_server }}" minute=5
使用變量,實例使用facts變量
---
- hosts: nginx
  tasks:
    - name: test variable facts
      debug: msg={{ ansible_default_ipv4.address }}
    - name: test variable facts
      debug: msg={{ ansible_default_ipv4["address"] }}

安裝並配置nginx

---
- hosts: nginx
  gather_facts: no
  tasks:
    - name: install epel-release
      yum: name=epel-release state=present
    - name: install nginx
      yum: name=nginx state=present
      notify:
        - restart nginx
    - name: config nginx
      template: src=/etc/ansible/nginx.j2 dest=/etc/nginx/nginx.conf
      notify:
        - restart nginx
    - name: open port
      command: firewall-cmd --add-port {{ http_port }}/tcp
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章