一、ansible的安裝
1、yum源在線安裝
在網易鏡像上下載.repo文件,添加到本地的yum源目錄下
http://mirrors.163.com/.help/centos.html
2、檢測是否安裝正確:ansible --version
3、配置文件的查看
/etc/ansible/ansible.cfg //ansible的主配置文件
/etc/ansible/hosts //這個配置文件就是默認主機清單配置文件
4、除了以上兩個重要的配置文件還有三個重要的可執行文件分別是:
ansible 主執行程序,一般用於命令行下執行
ansible-playbook 執行playbook中的任務
ansible-doc 獲取各模塊的幫助信息
二、基本使用
1、定義主機組
host_key_checking = False //修改主配置文件,不需要檢測key文件,直接登陸
ansible web --list-hosts //檢測web包含哪些主機
[web] //定義組名
192.168.4.85 ansible_ssh_user="root"
ansible_ssh_pass="123456" ansible_ssh_port="22"
192.168.4.86 ansible_ssh_user="root"
ansible_ssh_pass="123456" ansible_ssh_port="22"
//上面定義了IP地址,遠程的用戶,遠程的密碼以及端口
2、基本的命令用法
ansible web -m ping 測試是否通信
ansible all -m command -a 'hostname' -k
//遠程執行命令,-m調用的模塊,-a調用的命令,-k交互式輸入密碼
3、批量部署密鑰 ansible-doc -l | grep auth //查看安全模塊
ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k -v
//把生成的公鑰推送到所有的主機, exclusive=true文件強制覆蓋掉
三、常用的模塊:
1、ping模塊:測試主機是否是通的,用法很簡單,不涉及參數:
ansible web -m ping
2、command 模塊 遠程執行命令 管道的命令不能執行
ansible all -m command -a "ifconfig"
3、shell模塊:由於commnad只能執行裸命令(即系統環境中有支持的命令),至於管道之類的功能不支持,
shell模塊可以做到
ansible all -m shell -a 'cat /etc/passwd | wc -l' -v
4、script 模塊:把本地的腳本傳到遠端執行;前提是到遠端可以執行。
ansible all -m script -a '/root/a.sh'
5、 copy模塊:不適合大的文件
ansible web -m copy -a 'src=/etc/passwd dest=/root'
6、 lineinfile | replace 模塊:line配置整行,replace匹配選中的,直接修改文件
ansible s87 -m lineinfile -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^BOOTPROTO=" line="BOOTPROTO=static"'
ansible s87 -m replace -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^(BOOTPROTO=).*" replace="\1none"'
7、 yum 模塊:state(present/安裝 absent/卸載 latest/更新)
ansible web -m yum -a 'name=httpd state=present' -v
8、service 模塊:用於管理服務
state( started,stopped,restarted,reloaded),name=服務名稱,enabled=yes|no
ansible web -m service -a 'name="httpd" enabled=yes state=started'
9、file模塊:對遠程文件管理的模塊
state:
touch:創建一個新的空文件
directory:創建一個新的目錄,當目錄存在時不會進行修改
link:創建軟連接,結和src一起使用此選項才生效
hard:創建硬連接
absent:刪除文件,目錄,軟連接
ansible web -m file -a 'path=/tmp/test.txt state=touch'
四、playbook的基本使用
playbook 是 ansible 用於配置,部署,和管理託管主機劇本。通過 playbook 的詳細描述,執行其中的一系
列 tasks,可以讓進端主機達到預期的狀態。執行一些簡單的任務,使用ad-hoc命令可以方便的解決
問題,但是有時一個設施過於複雜,需要大量的操作時候,執行的 ad-hoc 命令是不適合的,這時最好使用
playbook,就像執行 shell 命令不寫 shell 腳本一樣,也可以理解爲批處理任務
play 中 hosts,variables,roles,tasks 等對象的表示方法都是鍵值中間以 ": " 分隔表示
playbook 構成:
Target: 定義將要執行 playbook 的進程主機組
Variable: 定義 playbook 運行時需要使用的變量
Tasks: 定義將要在進程主機上執行的任務列表
Handler: 定義 task 執行完成以後需要調用的任務
1、第一個playbook,vim myping.yml
--- # 第一行,表示開始
- hosts: all
remote_user: root
tasks:
- ping:
ansible-playbook myping.yml -f 5
-f 併發進程數量,默認是 5
hosts 行的內容是一個或多個組或主機的 patterns,以逗號爲分隔符
remote_user 就是賬戶名
2、playbook 執行命令給所有主機添加用戶 plj,設置默認密碼 123456
– 要求第一次登錄修改密碼
---
- hosts: all
remote_user: root
tasks:
- name: create user plj
user: group=wheel uid=1000 name=plj
- shell: echo 123456 | passwd --stdin plj
- shell: chage -d 0 plj
3、編寫 playbook 實現以下效果
– 安裝 apache – 修改 apache 監聽的端口爲 8080
– 爲 apache 增加 NameServer 配置 – 設置默認主頁 hello world
– 啓動服務 – 設置開機自啓動
- hosts: web
remote_user: root
tasks:
- yum: name=httpd
- name: config
copy:
src: /root/httpd.conf
dest: /etc/httpd/conf/httpd.conf
notify: //notify模塊:觸發器
- restart httpd
handlers: // task 執行完成以後需要調用的任務
- name: restart httpd
service: name=httpd state=restarted
4、變量
給所有主機添加用戶 plj,設置默認密碼 123456
要求第一次登錄修改密碼(使用變量)
---
- hosts: web
remote_user: root
vars:
username: haha
tasks:
- user: name={{username}} group=users password={{'123456' | password_hash('sha512')}}
- shell: chage -d 0 {{username}}
//變量過濾器 password_hash
5、ansible-playbook 對錯誤的處理
默認情況判斷 $?,如果 值 不爲 0 就停止執行
但某些情況我們需要忽略錯誤繼續執行
如:
我們要關閉 selinux,如果 selinux 已經是關閉的,返
回 1 ,但我們的目的就是關閉,已經關閉不算錯誤,
這個情況我們就需要忽略錯誤繼續運行,忽略錯誤有
兩種方法:
shell: /usr/bin/somecommand || /bin/true
- name: run some command
shell: /usr/bin/somecommand
ignore_errors: True
完整的例子如下:
---
- hosts: web
remote_user: root
vars:
username: plj
tasks:
- name: create user "{{username}}"
user: group=wheel uid=1010 name={{username}} password={{'123456'|password_hash('sha512')}}
- shell: setenforce 0
ignore_errors: true
- shell: chage -d 0 {{username}}
6、when:某些時候我們可能需要在滿足特定的條件後在觸發某
一項操作,戒在特定的條件下織止某個行爲,這個時候我們就需要迚行條件判斷,
when 正是解決這個問題的最佳選擇,進程中的系統變量 facts 變量作爲
when 的條件,這些 facts 我們可以通過 setup 模塊查看
---
- name: Install VIM
hosts: all
tasks:
- name: Install VIM via yum
yum: name=vim-enhanced state=installed
when: ansible_os_family == "RedHat"
- name: Install VIM via apt
apt: name=vim state=installed
when: ansible_os_family == "Debian"
7、register:保存shell命令的執行結果
有時候我們可能還需要更復雜的例子,比如判斷前一
個命令的執行結果,根據結果處理後面的操作,這時候我們就需要 register
模塊來保存前一個命令的返回狀態,在後面進行調用
---
- hosts: web
remote_user: root
vars:
username: plj
tasks:
- shell: id {{username}}
register: result
- name: change "{{username}}" password
user: name={{username}} password={{'12345678'|password_hash('sha512')}}
when: result
---
- hosts: db
remote_user: root
tasks:
- shell: uptime | awk '{printf $(9)}' | cut -b '1-4'
register: result
- service: name=httpd state=stopped
when: result.stdout | float > 0.5
8、with_items 是 playbook 標準循環
爲不同用戶定義不同組
---
- hosts: db
remote_user: root
tasks:
- user:
name: "{{item.name}}"
groups: "{{item.group}}"
with_items:
- {name: 'nb', group: 'root'}
- {name: 'dd', group: 'root'}
- {name: 'jj', group: 'usetr'}
- {name: 'hh', group: 'usetr'}
9、tags:給指定的任務定義一個調用標識
---
- hosts: web
remote_user: root
vars:
soft: httpd
tasks:
- name: install {{soft}}
yum: name={{soft}}
- name: config httpd.conf
copy: src=/root/playbook/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: config services
service: enabled=yes state=restarted name={{soft}}
tags: restartweb
10、debug:
ansible-playbook --syntax-check playbook.yaml //語法檢測
ansible-playbook -C playbook.yaml //測試運行