Ansible特點
不需要安裝用戶端,通過sshd去通信
基于子產品工作,子產品可以由任何語言開發
不僅支援指令行使用子產品,也支援編寫yaml格式的playbook
支援sudo
有提供ui(浏覽器圖形化)www.ansible.com/tower 10台主機以内免費
開源ui https://github.com/alaxli/amsible_ui文檔
http://download.csdn.net/detail/liyang23456/7741185
采集資訊
ansible web2.bbs.com -m setup
安裝
兩台機器 192.168.1.122 192.168.1.124
隻需要在122上安裝ansible就好了
yum install -y epel-release
yum install -y ansible
配置秘鑰
106上生成秘鑰隊
ssh-keygen -t rsa 直接回車即可,不用設定秘鑰密碼
把公鑰(id_rsa.pub)内容放到對方機器124的/root/.ssh/authorized_keys裡面
scp .ssh/id_rsa.pub 192.168.1.124:/root/.shh/authorized_keys
本機也要操作
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
關閉selinux
setenforce 0
yum install -y openssh*
ssh web2.bbs.com
遠端執行指令
vim /etc/ansible/host
[testhosts]
127.0.0.1
192.168.1.124
說明testhost為主機組名字,自定義 下面兩個ip為組内的機器ip
ansible testhost -m command -a 'w'
這樣就可以批量執行指令了 這裡的testhost為主機名,-m後面是子產品名字,-a後面就是指令,當然也可以直接些一個ip,針對某一台機器來執行指令
ansible 127.0.0.1 -m command -a 'hostname'
錯誤 如果出現 “msg”Aborting,target........
解決 yum install -y libselinux-python
還有一個子產品就是shell同樣也可以實作
ansible testhost -m shell -a 'w'
不行command
[root@web1 ~]# ansible testhosts -m command -a 'cat /etc/passwd|grep root'
192.168.1.124 | FAILED | rc=1 >>
cat: /etc/passwd|grep: No such file or directory
cat: root: No such file or directory
127.0.0.1 | FAILED | rc=1 >>
可以 shell
[root@web1 ~]# ansible testhosts -m shell -a 'cat /etc/passwd|grep root'
192.168.1.124 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
127.0.0.1 | success | rc=0 >>
拷貝檔案或目錄
ansible testhosts -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
注意:源目錄會放到目标目錄下面去,如果目标指定的目錄不存在它會自動建立,如果拷貝的是檔案,dest指定的名字和源如果不同,并且它不是已經存在的目錄,相當于拷貝過去後又重命名,但相反,如果desc是目标機器上已經存在的目錄,則會直接把檔案拷貝到該目錄下面
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"
這裡的/tmp/123和源機器上的/etc/passwd是一緻的,但是如果目标機器上已經有/tmp/123目錄,則不會再/tmp/123目錄下面建立passwd檔案
遠端執行腳本
首先建立一個shell腳本
vim /tmp/test.sh
#!/bin/bash
echo `date` > /tmp/ansible_lest.txt
然後把腳本分發到各個機器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最後是批量執行該腳本
ansible testhost -m shell -a "/bin/bash /tmp/test.sh"
shell 子產品,還支援遠端執行指令并且帶管道符
ansible testhost -m shell -a "cat /etc/passwd|wc -l"
實作任務計劃
ansible testhost -m cron -a "name=test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
ansible testhost -m cron -a "name='test_cron' job='/bin/bash /usr/local/sbin/1.sh' weekday=6"
若要删除該cron隻需要加一個字段 stat=absent
ansible testhost -m cron -a "name='test_cron' state=absent"
其他時間表示: 分鐘minute 小時hout 日期day 月份month
安裝rpm包/管理服務
ansible testhost -m yum -a "name=httpd"
在name後面還可以加上state-installed
ansible testhost -m service -a "name=httpd state=started enabled=yes"
這裡的name是centos系統裡面的服務名可以通過chkconfig --list查到
Ansible文檔的使用
ansible-doc -l 列出所有的子產品
ansible-doc cron 檢視指定子產品的文檔
playbook的使用 相當于linux裡面的shell
相當于把子產品寫到配置檔案裡面,
cat /etc/ansible/test.yml
---
- hosts: testhost
remote_user:root
tasks:
- name: test_playbook
shell:touch /tmp/bbs.txt
說明 hosts參數指定了對那些主機進行操作
user參數指定了使用什麼使用者登入遠端主機操作
tasks指定了一個任務,其下面的name參數同樣是對任務的描述,在執行過程中會列印出來
執行:ansible-playbook test.yml
vim user.yml
- name: create_user
hosts: web2.bbs.com
user: root
gather_facts: false
vars:
- user: "test"
tasks:
- name: create user
user: name="` user `"
說明: name參數對該playbook實作的功能做一個概述,後面執行過程中,會列印name變量的值,可以省略: gather_facts參數指定了在一下任務部分執行前,是否先執行setup子產品擷取主機相關資訊,這在後面的task會使用到setup擷取的資訊時用到:vars參數,指定了變量,這裡指定一個user變量,其值為test,需要注意的是,變量值要用引号引住:user提定了調用user子產品,name是user子產品裡的一個參數,而增加的使用者名字調用了上面user變量的值
ansible-playbook user.yml
playbook中的循環
兩台機器上都要有1.txt 2.txt檔案才行
hosts: testhost
task:
- name: change mod for file
file: path=/tmp/` item ` mode=600 owner=root group=root
with_items:
- 1.txt
- 2.txt
playbook判斷
- hosts: testhost
gather_facts: True
- name: use when
shell: touch /tmp/when.txt
when: facter_ipaddress == "192.168.1.122"
~
playbook中的handlers
執行task之後,服務其發生變化之後要執行一些操作,比如修改了配置檔案後,需要重新開機一下服務
remote_user: root
- name: test copy
copy: src=/tmp/1.txt dest=/tmp/2.txt 隻有執行成功這一步
notify: test handlers
handlers:
- name: test handlers 才會執行這一步
shell: echo "121212" >> /tmp/2.txt
說明 隻有copy模式真正執行後,才能去調用下面的handlers相關的操作,也就是說如果1.txt和2.txt内容是一樣的,并不會去執行handlers黎明的shell相關指令,這種比較适合配置檔案發生更改後,重新開機服務的操作
Ansible 安裝nginx
思路; 先在一台機器上編輯安裝好nginx 打包,然後在用ansible發下去
cd /etc/ansible 進入ansible配置檔案目錄
mkdir nginx_install 建立一個nginx_install的目錄,友善管理
cd nginx_install;
mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
說明:roles目錄下有兩個角色,common為一些準備操作,install為安裝nginx的操作
每個角色下面又有幾個目錄,handlers下面是發生改變時要執行的操作,同城用在配置檔案發生改變,重新開機服務,files為安裝時用到的一些檔案,meta為說明資訊,說明角色依賴等資訊,tasks裡面是核心的配置檔案 。templates通常存放一些配置檔案,啟動腳本等子產品檔案,vars下為定義的變量
[root@web1 tasks]# pwd
/etc/ansible/nginx_install/roles/common/tasks
[root@web1 tasks]# ls
main.yml
cd /tec/ansible/nginx_install/roles
定義commond的tasks,nginx是需要一些依賴包的
vim ./common/tasks/main.yml 内容如下
- name: Install initaliztion require software
yum: name=` item ` state=installed
with_items:
- zlib-devel
- pcre-devel
- openssl-devel
定義變量
[root@web1 roles]# cd install/
[root@web1 install]# ls
vars
[root@web1 install]# mkdir tasks files templates
files tasks templates vars
[root@web1 install]# cp /usr/local/nginx.tar.gz files/
[root@web1 install]# cp /usr/local/nginx/conf/nginx.conf templates/
[root@web1 install]# cp /etc/init.d/nginx templates/
[root@web1 install]# vi vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
vim copy.yml
- name: Copy Nginx Software
copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
template: src=nginx.conf dest=` nginx_basedir `/conf/ owner=root group=root mode=0644
vim install.yml
- name: Create Nginx User
user: name=` nginx_user ` state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
service: name=nginx state=restarted
- name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
再建立main.yml并且把copy和install調用
vim main.yml //内容如下
-
- include: copy.yml
- include: install.yml
到此兩個roles:common和install就定義完成了,接下來要定義一個入口配置檔案
vim /etc/ansible/nginx_install/install.yml
roles:
- common
- install
執行: ansible-playbook /etc/ansible/nginx_install/install.yml
管理配置檔案
生産環境中大多時候是需要管理配置檔案的,安裝軟體包隻是在初始化環境的時候用一下。
下面我們來寫個管理nginx配置檔案的playbook
mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
其中new為更新時用到的,old為復原時用到的,files下面為nginx.conf和vhosts目錄,handlers為重新開機nginx服務的指令
關于復原,需要在執行playbook之前先備份一下舊的配置,是以對于老配置檔案的管理一定
要嚴格,千萬不能随便去修改線上機器的配置,并且要保證new/files下面的配置和線上的>配置一緻
先把nginx.conf和vhosts目錄放到files目錄下面
cd /usr/local/nginx/conf/
cp -r nginx.conf vhosts /etc/ansible/nginx_conf/roles/new/files/
[root@web1 ansible]# mkdir nginx_config
[root@web1 ansible]# cd nginx_config/
[root@web1 nginx_config]# mkdir roles
[root@web1 nginx_config]# cd roles/
[root@web1 roles]# mkdir old new
[root@web1 roles]# cd new/
[root@web1 new]# mkdir vars files tasks handlers
[root@web1 new]# cp /usr/local/nginx/conf/nginx.conf files/
[root@web1 new]# cp -r /usr/local/nginx/conf/vhosts files/
[root@web1 new]# vim vars/main.yml
[root@web1 new]# vim handlers/main.yml
- name: restart nginx
shell: /etc/init.d/nginx reload
[root@web1 new]# vim tasks/main.yml
- name: copy conf file
copy: src=` item`.`src ` dest=` nginx_basedir `/` item`.`dest ` backup=yes owner=root group=root mode=0644
- { src: nginx.conf, dest: conf/nginx.conf }
- { src: vhosts, dest: conf/ }
notify: restart nginx
[root@web1 new]# cd ..
[root@web1 roles]# cd ..
[root@web1 nginx_config]# vim update.yml
- new
[root@web1 nginx_config]# ansible-playbook update.yml
復原 再更改之前把new裡面的files 複制到 old/files下
[root@web1 roles]# rsync -av new/files/ old/files/
[root@web1 nginx_config]# cp update.yml backup.yml
[root@web1 nginx_config]# vim backup.yml
- old
如果出現異常那麼
[root@web1 nginx_config]# ansible-playbook backup.yml