1、ansible的安裝
html
一、yum源在線安裝linux
在網易鏡像上下載.repo文件,添加到本地的yum源目錄下web
http://mirrors.163.com/.help/centos.htmlshell
二、檢測是否安裝正確:ansible --versionapache
三、配置文件的查看vim
/etc/ansible/ansible.cfg //ansible的主配置文件centos
/etc/ansible/hosts //這個配置文件就是默認主機清單配置文件安全
四、除了以上兩個重要的配置文件還有三個重要的可執行文件分別是:
ansible 主執行程序,通常用於命令行下執行
ansible-playbook 執行playbook中的任務
ansible-doc 獲取各模塊的幫助信息併發
2、基本使用ssh
一、定義主機組
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地址,遠程的用戶,遠程的密碼以及端口
二、基本的命令用法
ansible web -m ping 測試是否通訊
ansible all -m command -a 'hostname' -k
//遠程執行命令,-m調用的模塊,-a調用的命令,-k交互式輸入密碼
三、批量部署密鑰 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文件強制覆蓋掉
3、經常使用的模塊:
一、ping模塊:測試主機是不是通的,用法很簡單,不涉及參數:
ansible web -m ping
二、command 模塊 遠程執行命令 管道的命令不能執行
ansible all -m command -a "ifconfig"
三、shell模塊:因爲commnad只能執行裸命令(即系統環境中有支持的命令),至於管道之類的功能不支持,
shell模塊能夠作到
ansible all -m shell -a 'cat /etc/passwd | wc -l' -v
四、script 模塊:把本地的腳本傳到遠端執行;前提是到遠端能夠執行。
ansible all -m script -a '/root/a.sh'
五、 copy模塊:不適合大的文件
ansible web -m copy -a 'src=/etc/passwd dest=/root'
六、 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"'
七、 yum 模塊:state(present/安裝 absent/卸載 latest/更新)
ansible web -m yum -a 'name=httpd state=present' -v
八、service 模塊:用於管理服務
state( started,stopped,restarted,reloaded),name=服務名稱,enabled=yes|no
ansible web -m service -a 'name="httpd" enabled=yes state=started'
九、file模塊:對遠程文件管理的模塊
state:
touch:建立一個新的空文件
directory:建立一個新的目錄,當目錄存在時不會進行修改
link:建立軟鏈接,結和src一塊兒使用此選項才生效
hard:建立硬鏈接
absent:刪除文件,目錄,軟鏈接
ansible web -m file -a 'path=/tmp/test.txt state=touch'
4、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 執行完成之後須要調用的任務
一、第一個playbook,vim myping.yml
--- # 第一行,表示開始
- hosts: all
remote_user: root
tasks:
- ping:
ansible-playbook myping.yml -f 5
-f 併發進程數量,默認是 5
hosts 行的內容是一個或多個組或主機的 patterns,以逗號爲分隔符
remote_user 就是帳戶名
二、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
三、編寫 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
四、變量
給全部主機添加用戶 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
五、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}}
六、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"
七、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
八、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'}
九、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
十、debug:
ansible-playbook --syntax-check playbook.yaml //語法檢測
ansible-playbook -C playbook.yaml //測試運行