站長資訊網
        最全最豐富的資訊網站

        linux批量管理工具之ansible解析

        準備(前戲)

        安裝

          yum install ansible

        查看與ansible相關的文件信息

          rpm -ql ansible|less

        linux批量管理工具之ansible解析

        命令與選項

        hosts基本語法

        主機與組

          [webserver]  www.exaple.com  test.exaple.com    [dbserver]  one.example.com  two.example.com

        備注:端口號不是默認的時候,可以表示為

          www.exaple.com:5122

        主機別名

        如果有些靜態IP地址,希望設置一些別名,但不是在系統的host文件中做解析

          test_name ansible_ssh_port=5122 ansible_ssh_host=192.168.100.1

        主機名擴展

        可以像bash那樣設置一組名稱類似的主機

          [webserver]  web[01:50].example.com  db-[a:f].example.com

        自定連接

        對于每一個host,還可以選擇連接類型和連接用戶名

          [targets]  other1.example.com ansible_connection=ssh ansible_ssh_user=sam  other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan

        主機變量

        在hosts中給主機設置變量,這樣可以在playbook中使用

          [web]  host1 http_port=80  maxRequestsPerChild=808  host2 http_port=303 maxRequestsPerChild=909

        組變量

        通過關鍵字vars

          [webserver]  host1  host2    [webserver:vars]  ntp_server=ntp.atlanta.example.com  proxy=proxy.atlanta.example.com

        把一個組作為另個組的子成員

          [webserver]  host1  host2    [dbserver]  host3  host4    [saas:children]  webserver  dbserver    [saas:vars]  some_server=foo.southeast.example.com  halon_system_timeout=30  self_destruct_countdown=60  escape_pods=2

        Inventory參數說明

        要連接的遠程主機與設定的主機別名不同時

          ansible_ssh_host

        指定ssh端口號

          ansible_ssh_port

        指定連接用戶名

          ansible_ssh_user

        指定連接密碼(建議使用–ask-pass)

          ansible_ssh_pass

        指定sudo密碼(建議–ask-sudo-pass)

          ansibl_sudo_pass

        指定sudo執行命令的路徑

          ansible_sudo_exe

        指定與主機的連接類型

          ansible_connection

        使用指定密鑰文件

          ansible_privte_key_file

        指定目標系統的shell類型

          ansible_shell_type

        指定目標主機的python路徑(相同的方式可以指定ruby、perl)

          ansible_python_interpreter

        常用指令與選項

        ansible

          -u                            # 指定用戶名  -k                            # 提示密碼  -i                            # 使用指定主機清單  -m                            # 使用指定module,默認command  -a                            # module參數  

        ansible-doc

          -l                        # 列出所有module  -s                        # 列出指定模塊的使用方法

        常用模塊及使用方法

        (1)配置hosts文件

          [webserver]  192.168.100.131  192.168.100.132    [dbserver]  192.168.100.133  

        (2)配置ansible主機與個客戶機間密鑰認證(這里直接使用copy模塊完成)

        copy模塊

          ansible all -m copy -a 'src="/root/.ssh/id_rsa.pub" dest="/root/.ssh/authorized_keys" mode=600 backup="yes"' --ask-pass    說明:  src                            # 源文件或文件夾(如果是源文件以/結尾,則只拷貝該目錄下的內容)  dest                           # 目標文件或目錄(父目錄不存在時會報錯)  mode                           # 權限  backup                         # 是否需要備份目標目錄原來的文件

        command模塊

        該模塊是默認模塊,示例分顯示所有主機的時間

          ansible all -m command -a "date"  

        user模塊、group模塊

        例:在webserver組的每臺主機上創建一個用戶web1

          ansible webserver -m user -a "name='web1' home='/home/test' system='yes' state='present'"    說明:  name                            # 用戶名  home                            # 用戶home目錄  system                          # 是否為系統用戶  state                           # present或absent(present添加,absent刪除)

        cron模塊

        例:創建每周一12點執行echo "sam 你好"

          ansible webserver -m cron -a "minute=0 hour=12  weekday=1 job='echo "sam 你好"' name='echo' state='present'"    說明:  minute                        # 分鐘(0-59,*/2)  hour                          # 小時(0-23,*/2)  day                           # 日(1-31,*/2)  month                         # 月(1-12,*/2)  weekday                       # 星期(0-6)  name                          # 任務名稱  backup                        # 是否需要備份原來的任務

        copy模塊

        例:拷貝/etc/fstab到/tmp/fstab.ansible

          ansible webserver -m copy -a "src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=644"    說明:  src                                            # 源文件  dest                                           # 目標文件  owner                                          # 文件屬主  mode                                           # 權限  content                                        # 為目標文件指定內容,此時省略src    ansible webserver -m copy -a "content='Hello sam' dest=/tmp/sam.txt "

        file模塊

        例如:將webserver服務器/tmp/fstab.ansible 的文件權限更改為600

          ansible webserver -m file -a "path=/tmp/fstab.ansible mode=600"    說明:  path                    # 源文件(可以用name,dest)  mode                    # 權限  owner                   # 屬主  state                   # 如果是link,則表示要創建軟連接(此時源文件用src表示)  src                     # 源文件    ansible webserver -m file -a "path=/tmp/fstab.link state=link src=/tmp/fstab.ansible"

        yum模塊

        例如:在webserver組安裝httpd服務

          ansible webserver -m yum -a "name=httpd state=present "    ansible dbserver -m yum -a "name='*' state=latest"    說明:  name            # 包名稱,如果state=latest,那么name可以為*表示運行yum update -y ;如果state=present,那么name可以指定本地的一個軟件包路徑; 也可以是一個用 逗號 分隔的軟件包列表  state           # (`present' or `installed', `latest'), or remove (`absent' or `removed')  list            # 等效于yum list 

        service模塊

        例如:啟動webserver組的httpd服務,并設置為開機啟動

          ansible webserver -m service -a "name=httpd enabled=yes state=started "    說明:  name            # 服務名稱  enabled         # 是否開機啟動  state           # started,stopped,restarted,reloaded

        shell模塊

        例如:查看正在運行的httpd服務的進程號

          ansible webserver -m shell -a "ps axu|grep httpd"

        script模塊

        首先創建一個shell腳本文件test.sh,內容如下:

          #!/bin/bash  a=`pwd`  echo "This is $a"  echo "Hello sam ansible from script" >/tmp/script.txt

        然后執行ansible調用該腳本

          ansible webserver -m script -a "/tmp/test.sh"    說明:  參數為腳本名,可以是絕對路徑

        功能模塊ping、setup

          ansible webserver -m ping                說明:  測試ansible主機與其他節點的連通性,成功返回 pong    ansible webserver -m setup    說明:  返回節點的facts信息,這些變量可以在playbook中直接使用

        playbook使用與配置

        注意這里對于yml的配置文件有著嚴格的語法要求,縮進必須是2個空格(由于基礎較差坑了我2個小時,python的4空格縮進0.0)

        1、一個簡單的playbook劇本:(注意復制的時候一定注意格式,縮進2個用空格)

          - hosts: webserver                # 主機組    remote_user: root               # 遠程連接用戶    tasks:                          # 任務列表關鍵字      - name: Test connection       # 任務名稱        ping:                       # 模塊名稱

        2、在webserver組創建一個組賬戶名稱為nginx-group,創建個賬戶為nginx-user,并且nginx-user的基本組為nginx-group;然后拷貝當前主機/etc/fstab文件到dbserver組的/tmp目錄并命名為fstab-name.ansible。

          - hosts: webserver    remote_user: root    tasks:      - name: Add nginx-group        group: name=nginx-group gid=2018 system=yes      - name: Add nginx-user        user: name=nginx-user uid=2018 system=yes group=nginx-group home=/home/nginx shell=/bin/nologin  - hosts: dbserver    remote_user: root    tasks:      - name: Copy File to dbserver        copy: src=/etc/fstab dest=/tmp/fstab-name.ansible mode=600

        HANDLERS

        1、現在我們需要在webserver組安裝httpd服務,并且需要將其啟動并設置為開機自啟,并且使用我當前機器上httpd.conf文件作為其配置文件。

          - hosts: webserver    remote_user: root    tasks:      - name: Instarll httpd        yum: name=httpd state=present        - name: Configuration httpd file        copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf        - name: Start service httpd        service: name=httpd state=started enabled=yes

        2、繼續上面操我現在修改下本地的httpd.conf的監聽端口為8080,然后重新執行playbook,并查看websever服務器的httpd端口啟動情況。

          [root@localhost ~]# ansible-playbook httpd.yml     PLAY [webserver] ********************************************************************************************************************************    TASK [Gathering Facts] **************************************************************************************************************************  ok: [192.168.100.131]  ok: [192.168.100.132]    TASK [Instarll httpd] ***************************************************************************************************************************  ok: [192.168.100.131]  ok: [192.168.100.132]    TASK [Configuration httpd file] *****************************************************************************************************************  changed: [192.168.100.132]  changed: [192.168.100.131]    TASK [Start service httpd] **********************************************************************************************************************  ok: [192.168.100.131]  ok: [192.168.100.132]    PLAY RECAP **************************************************************************************************************************************  192.168.100.131            : ok=4    changed=1    unreachable=0    failed=0     192.168.100.132            : ok=4    changed=1    unreachable=0    failed=0       [root@localhost ~]# ansible webserver -a "ss -tnpl"  192.168.100.131 | SUCCESS | rc=0 >>  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port                LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=968,fd=3))  LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1079,fd=13))  LISTEN     0      128         :::80                      :::*                   users:(("httpd",pid=2550,fd=4),("httpd",pid=2549,fd=4),("httpd",pid=2548,fd=4),("httpd",pid=2547,fd=4),("httpd",pid=2546,fd=4),("httpd",pid=2545,fd=4))  LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=968,fd=4))  LISTEN     0      100        ::1:25                      :::*                   users:(("master",pid=1079,fd=14))    192.168.100.132 | SUCCESS | rc=0 >>  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port                LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=971,fd=3))  LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1078,fd=13))  LISTEN     0      128         :::80                      :::*                   users:(("httpd",pid=2519,fd=4),("httpd",pid=2518,fd=4),("httpd",pid=2517,fd=4),("httpd",pid=2516,fd=4),("httpd",pid=2515,fd=4),("httpd",pid=2514,fd=4))  LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=971,fd=4))  LISTEN     0      100        ::1:25                      :::*                   users:(("master",pid=1078,fd=14))

        如上所示,配置文件發生了更改但是httpd服務的啟動端口并未發生更改,原因在于更了配置文件,可是httpd服務并未重啟。

        這時候我們就需要用HANDLERS功能,簡單點說該功能就是在某個任務執行完成后自動觸發指定任務。據說不僅僅可以使用任務觸發還可以使用某些主機觸發(比如某些主機執行完一些任務后,主要區別在于notify的位置),下面我就用任務觸發舉個例子

          - hosts: webserver    remote_user: root    tasks:      - name: Instarll httpd        yum: name=httpd state=present        - name: Configuration httpd file        copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf        notify:                                                # handlers觸發關鍵字          - Restart httpd                                      # handlers中的任務名      - name: Start service httpd        service: name=httpd state=started enabled=yes      handlers:                                                  # 和tasks同級      - name: Restart httpd        service: name=httpd state=restarted

        playbook中使用變量

        自定義變量

        我們現在定義兩個變量,分別為package_name、service_name,然后分別給這兩個變量賦值,最后通過引用這兩個變量在執行playbook。

        1、例如:現在我需要停掉webserver組服務器的httpd服務,最后要卸載httpd服務(其實引用變量的方法很簡單就是{{變量名}})

          - hosts: webserver    remote_user: root    vars:      - package_name: httpd      - service_name: httpd    tasks:      - name: Stopped {{service_name}}        service: name={{service_name}}  state=stopped      - name: remove {{package_name}}        yum: name={{package_name}} state=removed

        facts中的變量

        1、例如,我們將webserver中每臺主機的IP地址寫到/tmp/ip.txt文件(這里借助copy模塊中content完成)

          - hosts: webserver    remote_user: root    tasks:      - name: copy file for ip address        copy: content={{ansible_all_ipv4_addresses}} dest=/tmp/ip.txt mode=600

        playbook中使用條件測試

        我們繼續上面的例子,在webserver組中,刪除ip地址為192.168.74.131主機的/tmp目錄下的ip.txt文件

          - hosts: webserver    remote_user: root    tasks:      - name: Delete ip.txt        shell: rm -rf /tmp/ip.txt        when: ansible_all_ipv4_addresses==["192.168.100.131"]

        playbook中使用迭代

        1、例如:要在webserver組分別創建web1和sam1賬戶(當然你也可以建立多個tasks任務,顯然這不是我們希望的樣子)

          - hosts: webserver    remote_user: root    tasks:      - name: Add web1 sam1        user: name={{item}} state=present        # 使用關鍵字變量item        with_items:                              # 定義關鍵字變量值的列表          - web1          - sam1

        2、除了單個變量的迭代,還可以支持成組的變量迭代,比如創建用戶web2并且所數組為web3,創建用戶sam2,所屬組為sam3

          - hosts: webserver    remote_user: root    tasks:      - name: Add group sam3 web3        group: name={{item}} state=present        with_items:          - sam3          - web3      - name: Add web1 sam1        user: name={{item.name}} group={{item.group}} state=present    # 用item引用字典的鍵做變量        with_items:                                                    # 以字典序列的形式定義變量          - {name: sam2, group: sam3}          - {name: web2, group: web3}

        在playbook中使用Templates

        簡單描述下,Templates

        (1)用定義的變量替換模板中的具體參數

        (2)在plabook中用關鍵字template代替copy模塊名

        例:我們在hosts文件中為兩個webserver分別指定不同的httpd端口,并且希望httpd配置文件中的hostname為本機的fqdn

        (1)首先我們先在INVENTORY文件中分別為兩個webserver主機創建兩個變量,httpd_port、web_root分別為httpd的端口、網站根目錄并且定義ServerName為本機主機名(引用ansible facts中的ansible_fqdn變量)

          [webserver]  192.168.100.131 http_port=9000 web_root="/home/web1"  192.168.100.132 http_port=8000 web_root="/home/web2"

        (2)在兩臺webserver分別創建網站根目錄/home/web1、/home/web2

          ansible 192.168.100.131 -a "mkdir /home/web1"  ansible 192.168.100.132 -a "mkdir /home/web2"

        我們需要在ansible服務端創建一個/tmp/httpd.conf.j2格式的模板文件,我們直接選用httpd.conf的配置文件為基礎模板,只在下面幾個特殊地方寫入變量代替

          Listen {{http_port}}  ServerName {{ansible_fqdn}}  DocumentRoot "{{web_root}}"

        playbook中使用tags(標簽)

        在playbook中可以為某個或某些任務定義一個“標簽”,在執行此playbook時,通過為ansible-playbook命令使用–tags選項能實現僅運行指定的taks而非所有的。

        例如

          - hosts: webserver    remote_user: root    tasks:      - name: Uninstarll httpd        yum: name=httpd state=absent        - name: Configuration httpd file        template: src=/tmp/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf        notify:          - Restart httpd      - name: Stop service httpd        service: name=httpd state=stopped        tags:                                    # 關鍵字          - stop                                 # 標簽名      handlers:      - name: Restart httpd        service: name=httpd state=restarted

        運行方式:

          ansible-playbook template.yml --tags='start'

        roles

        (1)目錄名同角色名

        (2)目錄結構有固定格式:files、templates、tasks、handlers、vars、meta

        (3)site.yml中定義playbook,額外也可以定義其它的yml文件。

        例:

        (1)定義個web角色

        (2)該角色包含安裝httpd服務,

        (3)定義該服務一個httpd的模板文件,使httpd的ServerName為啟動服務器的本機名

        (4)并且在vars中定義個變量http_port,web_root分別為"81","/home"

        (5)啟動該服務,并設置開機自

        (6)先使用webserver組調用該角色,然后在使用dbserver調用該角色

        (1)創建角色及相關目錄

          mkdir -pv /etc/ansible/roles/web/{tasks,files,templates,meta,handlers,vars}

        (2)編輯/etc/ansible/roles/web/tasks/main.yml文件

          - name: Install httpd    yum: name=httpd state=present    - name: Install httpd.conf    template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf    notify:      - Restart httpd    - name: Start httpd    service: name=httpd state=started enabled=yes

        (3)編輯/etc/ansible/roles/web/handlers/main.yml文件

          - name: Restart httpd    service: name=httpd state=restarted

        (4)編輯/etc/ansible/roles/web/templates/httpd.conf.j2

        我們需要在ansible服務端創建一個/tmp/httpd.conf.j2格式的模板文件,我們直接選用httpd.conf的配置文件為基礎模板,只在下面幾個特殊地方寫入變量代替

          Listen {{http_port}}  ServerName {{ansible_fqdn}}  DocumentRoot "{{web_root}}"

        (5)編輯/etc/ansible/roles/web/vars/main.yml

          http_port: 81  web_root: "/home"

        (7)編輯/etc/ansible/roles/site.yml

          - hosts: webserver    remote_user: root    roles:      - web        - hosts: dbserver    remote_user: root    roles:      - web

        (8)執行playbook

          ansible-playbook /etc/ansible/roles/site.yml

        生活真難,因為工作原因熬夜到2,連續3天才總結出來,如果你學會了點個贊吧。

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 欧美精品色精品一区二区三区| 无码国产精品一区二区免费模式 | 无码精品一区二区三区免费视频| 亚洲精品欧美二区三区中文字幕| 国产精品成人小电影在线观看| 91久久婷婷国产综合精品青草| 无码精品视频一区二区三区| 精品人妻少妇一区二区三区在线| 四虎永久在线精品884aa下载| 99久久99久久精品免费看蜜桃 | 久久夜色撩人精品国产| 欧美精品在线一区二区三区| 97热久久免费频精品99| 日产精品久久久久久久| 无码国模国产在线无码精品国产自在久国产 | 国产精品久久久久一区二区三区| 国产精品久久久久影院嫩草| 国产亚洲精品美女久久久| 亚洲精品国产精品乱码不卡√| 日韩美女18网站久久精品| 久久精品成人免费观看97| 国产精品人人做人人爽| 国产精品欧美久久久久无广告 | 久久精品国产福利国产秒| 国产成人精品手机在线观看| 久久久久人妻精品一区| 亚洲精品高清国产一线久久| 亚洲一区二区三区国产精品| 亚洲精品麻豆av| 亚洲一区无码精品色| 亚洲精品和日本精品| 亚洲欧美精品一区久久中文字幕| 人妻少妇看A偷人无码精品| 老湿亚洲永久精品ww47香蕉图片| 久久国产午夜精品一区二区三区| 久久精品无码一区二区三区免费| 精品日本一区二区三区在线观看| 久久99精品久久久久久齐齐| 免费看一级毛片在线观看精品视频 | 国产精品婷婷午夜在线观看| 国产欧美一区二区精品性色99|