ansible


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 >>

cat: /etc/passwd|grep: No such file or directory

cat: root: No such file or directory




可以 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 >>

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin





    拷貝文件或目錄

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

  user: root

  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

  user: root

  gather_facts: True

  tasks:

    - name: use when

      shell: touch /tmp/when.txt

      when: facter_ipaddress == "192.168.1.122"

~



playbook中的handlers

執行task之後,服務其發生變化之後要執行一些操作,比如修改了配置文件後,需要重啓一下服務

---

- hosts: testhost

  remote_user: root

  tasks:

    - 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

[root@web1 install]# ls

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

---

- hosts: testhost

  remote_user: root

  gather_facts: True

  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

  with_items:

    - { 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

---

- hosts: testhost

  user: root

  roles:

  - 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

[root@web1 nginx_config]# vim backup.yml

---

- hosts: testhost

  user: root

  roles:

  - old


[root@web1 nginx_config]# ansible-playbook update.yml

如果出現異常那麼


[root@web1 nginx_config]# ansible-playbook backup.yml


git clone git://github.com/dl528888/ansible-examples.git


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章