ansible學習筆記

簡介:

 在日常服務器維護中,從系統安裝到程序部署再到發布應用,在大規模的生產環境中,如果需要手動的每臺服務器進行安裝配置將會給運維人員帶來許多繁瑣而又重復的工作。這就促使了在每個運維層次中出現了不同的自動化運維工具。

常見的自動化運維工具分類有以下幾類:

 系統安裝運維工具(OS Provisioning):

   常見的有:PXE,Cobbler,Red Hat Satelite(redhat)系統專用等

 操作系統的配置運維工具(OS Config):

   常見的有:cfengine,puppet,saltsack,chef等 

 應用程序部署工具(Application Service Orchestration):

   常見的有:Func,Fabric,ControITier,Capistrano等

根據工作模式不同上面的運維工具有分為以下兩類:

   agent:基于ssl協議實現,agent工作在被監控端,例如:puppet

   agentless: 基于ssh key實現,例如:ansible

ansible介紹:

 ansible是一款輕量級自動化運維工具,由Python語言開發,結合了多種自動化運維工具的特性,實現了批量系統配置、批量程序部署、批量命令執行等功能;ansible是基于模塊化實現批量操作的。

各模塊之間的工作聯系如下圖所示:

ansible架構圖.jpgansible的特點:

 模塊化、部署簡單、工作于agentless模式、默認使用ssh協議、支持自定義模塊、支持Palybook等

一、ansible安裝以及常用的模塊介紹

1、安裝ansible

[root@node1 ~]# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto ansible

2、配置ansible的主機文件,編輯/etc/ansible/hosts,添加管理節點主機:

[test]  \\主機組名,可以任意命名
172.16.2.13 \\管理節點主機,也可以是主機名

3、ansible常用的模塊有:

command模塊:默認模塊,用于在各被管理節點運行指定的命令;

例:[root@node1 ~]# ansible all  -m command -a 'ifconfig eth0'
172.16.2.13 | success | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F8:D4:88  
          inet addr:172.16.2.13  Bcast:172.16.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fef8:d488/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8046 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2165 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1392192 (1.3 MiB)  TX bytes:201703 (196.9 KiB)

user模塊:用戶模塊,用于在各被管理節點管理用戶所使用;

例:[root@node1 ~]# ansible all  -m user -a 'name=test'
172.16.2.13 | success >> {  
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 500, 
    "home": "/home/test", 
    "name": "test", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 500
}
[root@node2 ~]# tail  -1 /etc/passwd
test:x:500:500::/home/test:/bin/bash

group模塊:用戶組模塊,用于在各被管理節點管理用戶組所使用;

例:[root@node1 ~]# ansible all  -m group  -a 'name=mylinux gid=1000'
172.16.2.13 | success >> {
    "changed": true, 
    "gid": 1000, 
    "name": "mylinux", 
    "state": "present", 
    "system": false
}

[root@node2 ~]# tail -1 /etc/gshadow
mylinux:!::

cron模塊:計劃任務模塊,用于在各被管理節點管理計劃任務;

例:[root@node1 ~]# ansible all -m cron -a  "name=time  minute='*/2' job='/usr/sbin/ntpdate 172.16.12'"
172.16.2.13 | success >> {
    "changed": true, 
    "jobs": [
        "time"
    ]
}
[root@node2 ~]# crontab  -l  \\在管理節點查看cron任務
#Ansible: time
*/2 * * * * /usr/sbin/ntpdate 172.16.12

copy模塊:復制模塊,復制文件至各管理節點;

例:[root@node1 ~]# ansible all -m copy -a 'src=/root/test dest=/tmp mode=600'
172.16.2.13 | success >> {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0600", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1439189042.77-131108212586927/source", 
    "state": "file", 
    "uid": 0
}
[root@node2 ~]# ls -l /tmp/test
-rw------- 1 root root 0 Aug 10 14:44 /tmp/test

file模塊:文件模塊,修改各個節點指定的文件屬性;

例:[root@node1 ~]# ansible all -m file -a 'path=/tmp/test mode=644 owner=test'
172.16.2.13 | success >> {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "test", 
    "path": "/tmp/test", 
    "size": 0, 
    "state": "file", 
    "uid": 500
}
[root@node2 ~]# ls -l /tmp/test
-rw-r--r-- 1 test root 0 Aug 10 14:44 /tmp/test

ping模塊:測試模塊,測試各個被管理節點是否在線;

例:[root@node1 ~]# ansible all  -m ping
172.16.2.13 | success >> {
    "changed": false, 
    "ping": "pong"
}

service模塊:管理各個節點的服務

例:[root@node1 ~]# ansible all -m service -a 'name=ntpd  enabled=true'
172.16.2.13 | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "ntpd"
}

shell模塊:與command模塊功能相同,但比command的模塊功能強大

例:[root@node1 ~]# ansible all -m shell -a 'cat /etc/passwd | grep root'
172.16.2.13 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

script模塊:自動復制腳本至遠程節點,并運行之

例:[root@node1 ~]# cat ansible.sh 
#!/bin/bash
echo  "hello word" >> /tmp/test
[root@node1 ~]# ansible all -m script -a '/root/ansible.sh'
172.16.2.13 | success >> {
    "changed": true, 
    "rc": 0, 
    "stderr": "OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug1: mux_client_request_session: master session id: 2\r\nShared connection to 172.16.2.13 closed.\r\n", 
    "stdout": ""
}
[root@node2 ~]# cat /tmp/test
hello word

setup模塊:收集ansible的facters

例:[root@node1 ~]# ansible all -m setup
172.16.2.13 | success >> {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.16.2.13"
        ], 
       ...............

yum模塊:用于在各個管理節點安裝軟件所使用

例:[root@node1 ~]# ansible all -m yum -a 'name=httpd state=present'
172.16.2.13 | success >> {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base: mirrors.yun-idc.com\n * extras: mirrors.yun-idc.com\n * updates: mirrors.yun-idc.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-45.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-45.el6.centos for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-45.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package             Arch         Version                      Repository  Size\n================================================================================\nInstalling:\n httpd               x86_64       2.2.15-45.el6.centos         base       829 k\nInstalling for dependencies:\n apr-util-ldap       x86_64       1.3.9-3.el6_0.1              base        15 k\n httpd-tools         x86_64       2.2.15-45.el6.centos         base        77 k\n mailcap             noarch       2.1.31-2.el6                 base        27 k\n\nTransaction Summary\n================================================================================\nInstall       4 Package(s)\n\nTotal download size: 947 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                           977 kB/s | 947 kB     00:00     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : httpd-tools-2.2.15-45.el6.centos.x86_64                      1/4 \n\r  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/4 \n\r  Installing : mailcap-2.1.31-2.el6.noarch                                  3/4 \n\r  Installing : httpd-2.2.15-45.el6.centos.x86_64                            4/4 \n\r  Verifying  : mailcap-2.1.31-2.el6.noarch                                  1/4 \n\r  Verifying  : httpd-2.2.15-45.el6.centos.x86_64                            2/4 \n\r  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/4 \n\r  Verifying  : httpd-tools-2.2.15-45.el6.centos.x86_64                      4/4 \n\nInstalled:\n  httpd.x86_64 0:2.2.15-45.el6.centos                                           \n\nDependency Installed:\n  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        \n  httpd-tools.x86_64 0:2.2.15-45.el6.centos                                     \n  mailcap.noarch 0:2.1.31-2.el6                                                 \n\nComplete!\n"
    ]
}
[root@node2 ~]# rpm -q httpd
httpd-2.2.15-45.el6.centos.x86_64

4、ansible使用幫助

ansbile-doc -l \\列出ansible的所有模塊
ansible-doc -s module_name \\查看模塊的屬性信息
例:查看service模塊的屬性信息;
[root@node1 ~]# ansible-doc -s service
less 436
Copyright (C) 1984-2009 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: M a n a g e   s e r v i c e s .
  action: service
      arguments              # Additional arguments provided on the command line
      enabled                # Whether the service should start on boot. *At least one of state and enabled are required.*
      name=                  # Name of the service.
      pattern                # If the service does not respond to the status command, name a substring to look for as would be found in the output of the `ps' command 
      runlevel               # For OpenRC init scripts (ex: Gentoo) only.  The runlevel that this service belongs to.
      sleep                  # If the service is being `restarted' then sleep this many seconds between the stop and start command. This helps to workaround badly beha
      state                  # `started'/`stopped' are idempotent actions that will not run commands unless necessary.  `restarted' will always bounce the service.  `r

原創文章,作者:馬行空,如若轉載,請注明出處:http://www.www58058.com/6941

(0)
馬行空馬行空
上一篇 2015-08-17 11:27
下一篇 2015-08-17 11:29

相關推薦

  • CA證書服務搭建與申請

    服務端根CA創建證書 進入固定目錄,創建所需要的文件 cd /etc/pki/CA/ touch /etc/pki/CA/index.txt 生成證書索引數據庫文件 echo 01 > /etc/pki/CA/serial 指定第一個頒發證書的序列號 生成秘鑰 (umask 066;openssl genrsa -out /etc/pki/CA/pri…

    2017-09-11
  • Linux文件管理及bash腳本特性

    馬哥教育網絡班23期+第2周課程練習 Linux文件管理及bash腳本特性 概述,經過前三天的學習,想必我們已經對Linux 有了一個初步的了解,接下來這講我們要講述一下Linux至關重要的文件管理和bash腳本特性等知識要點 一、Linux 文件管理 1.1 原理概述   文件管理對于Linux系統來說至關重要,因為Linux 的哲學思想就是一切…

    Linux干貨 2016-09-19
  • N25第5周作業(grep和find使用)

    顯示當前系統上root, fedora 或user1的默認shell 找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello(); 使用echo命令輸出一個絕對路徑,使用grep取出基名,擴展取出其路徑名 找出IFCONIFG命令結果的1-255之間的數字 挑戰題:寫一個模式,能匹配出合理的IP地址 挑戰題…

    Linux干貨 2016-12-30
  • hello! 我的博客第一站

    大家好!  這是我進博客的第一天,一個剛進來的新司機。在這里我就不秀我的車技了,只希望各位老司機開車不要太快,我暈車      —— 生命不息,奮斗不止

    Linux干貨 2017-07-11
  • SED基本用法和在文本中的使用

    sed命令行格式為: sed [-nefri] ‘command’ 輸入文本/文件 常用選項: -n∶取消默認的輸出,使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN的資料一般都會被列出到屏幕上。但如果加上 -n 參數后,則只有經過sed 特殊處理的那一行(或者動作)才會被列出來 -e∶進行多項編輯,…

    Linux干貨 2017-05-05
  • 軟鏈接與硬鏈接的分析

    Linux引用硬鏈接與軟鏈接,是為了實現文件的共享,更有隱藏文件路徑、增加權限安全及節省存儲等的好處。很多新手不知道軟鏈接與硬鏈接的區別,今天大家一起總結它們的區別吧^_^ 一,硬鏈接 硬鏈接的特性可以體現出什么是硬鏈接: 通過索引節點來進行鏈接,文件要有相同的inode及data block 不允許跨分區創建 只有在同一文件系統中的文件之間才可以,不能交叉…

    2017-07-22
欧美性久久久久