ansible基礎

ansible 介紹:官網,百科之類的



ansible的部署

   centos 6.5上的部署.

2.2.2.11    node1.king.com  ansible
2.2.2.15    node3.king.com
2.2.2.12    node2.king.com


tar xf ansible-1.5.4.tar.gz
cd ansible-1.5.4
python setup.py build
python setup.py install
mkdir /etc/ansible
cp -r examples/* /etc/ansible

ansible的用法:

ansible <host-pattern> [options]

實例:配置匹配的主機

  ansible 基於SSH協議,因此需要添加ssh互信,如果不添加則需要手動輸入密碼.

ssh互信.針對主機名互信.並且在/etc/hosts文件中添加對應的地址


ssh-keygen -t rsa -P '' -f .ssh/id_rsa
ssh-copy-id -i .ssh/id_rsa.pub 2.2.2.12
[root@node1 ~]# cat /etc/ansible/hosts
[hanodes]
node1.king.com
node2.king.com
node3.king.com


ansible命令的用用法:

 配置好主機後,我們先執行下聯通性

[root@node1 ~]# ansible all -m ping
node1.king.com | success >> {
    "changed": false,
    "ping": "pong"
}
node3.king.com | success >> {
    "changed": false,
    "ping": "pong"
}
node2.king.com | success >> {
    "changed": false,
    "ping": "pong"
}

   默認試用的模塊爲command

[root@node1 ~]# ansible all -a 'date'
node1.king.com | success | rc=0 >>
Sat May  3 18:08:53 CST 2014
node2.king.com | success | rc=0 >>
Sat May  3 18:08:54 CST 2014
node3.king.com | success | rc=0 >>
Sat May  3 18:08:55 CST 2014
[root@node1 ~]# ansible all -m command -a 'date'
node1.king.com | success | rc=0 >>
Sat May  3 18:09:25 CST 2014
node3.king.com | success | rc=0 >>
Sat May  3 18:09:26 CST 2014
node2.king.com | success | rc=0 >>
Sat May  3 18:09:25 CST 2014
ansible-doc xx 顯示指定的模塊的文檔


ansible-doc -l 顯示用那些模塊


[root@node1 ~]# ansible all -m yum  -a "name=httpd state=present"
node1.king.com | success >> {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "httpd-2.2.15-29.el6.centos.x86_64 providing httpd is already installed"
    ]
}
node3.king.com | success >> {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "httpd-2.2.15-29.el6.centos.x86_64 providing httpd is already installed"
    ]
}
node2.king.com | success >> {
    "changed": false,
    "msg": "",
    "rc": 0,
    "results": [
        "httpd-2.2.15-29.el6.centos.x86_64 providing httpd is already installed"
    ]
}

上面就是我們安裝httpd的用法


ansible這個命令是不是太低端,我們有高級的用法ansible-playbook,需要配置yaml語法


yaml

AML是一個可讀性高的用來表達資料序列的格式。YAML參考了其他多種語言,包括:XML、C語言、Python、Perl以及電子郵件格式RFC2822等。Clark Evans在2001年在首次發表了這種語言,另外Ingy dt Net與Oren Ben-Kiki也是這語言的共同設計者。
YAML Ain't Markup Language,即YAML不是XML。不過,在開發的這種語言時,YAML的意思其實是:"Yet Another Markup Language"(仍是一種標記語言)。其特性:
    YAML的可讀性好
    YAML和腳本語言的交互性好
    YAML使用實現語言的數據類型
    YAML有一個一致的信息模型
    YAML易於實現
    YAML可以基於流來處理
    YAML表達能力強,擴展性好


5.1 playbook基礎組件
5.1.1 Hosts和Users
                                                                               
    playbook中的每一個play的目的都是爲了讓某個或某些主機以某個指定的用戶身份執行任務。hosts用於指定要執行指定任務的主機,其可以是一個或多個由冒號分隔主機組;remote_user則用於指定遠程主機上的執行任務的用戶。如上面示例中的
        -hosts: webnodes
         remote_user: root
    不過,remote_user也可用於各task中。也可以通過指定其通過sudo的方式在遠程主機上執行任務,其可用於play全局或某任務;此外,甚至可以在sudo時使用sudo_user指定sudo時切換的用戶。
        - hosts: webnodes
          remote_user: mageedu
          tasks:
            - name: test connection
              ping:
              remote_user: mageedu
              sudo: yes
5.1.2 任務列表和action
    play的主體部分是task list。task list中的各任務按次序逐個在hosts中指定的所有主機上執行,即在所有主機上完成第一個任務後再開始第二個。在運行自下而下某playbook時,如果中途發生錯誤,所有已執行任務都將回滾,因此,在更正playbook後重新執行一次即可。
    task的目的是使用指定的參數執行模塊,而在模塊參數中可以使用變量。模塊執行是冪等的,這意味着多次執行是安全的,因爲其結果均一致。
    每個task都應該有其name,用於playbook的執行結果輸出,建議其內容儘可能清晰地描述任務執行步驟。如果未提供name,則action的結果將用於輸出。
    定義task的可以使用“action: module options”或“module: options”的格式,推薦使用後者以實現向後兼容。如果action一行的內容過多,也中使用在行首使用幾個空白字符進行換行。
        tasks:
          - name: make sure apache is running
            service: name=httpd state=running
        在衆多模塊中,只有command和shell模塊僅需要給定一個列表而無需使用“key=value”格式,例如:
            tasks:
              - name: disable selinux
                command: /sbin/setenforce 0
        如果命令或腳本的退出碼不爲零,可以使用如下方式替代:
            tasks:
              - name: run this command and ignore the result
                shell: /usr/bin/somecommand || /bin/true      
        或者使用ignore_errors來忽略錯誤信息:
            tasks:
              - name: run this command and ignore the result
                shell: /usr/bin/somecommand
                ignore_errors: True   
5.1.3 handlers
                                                                               
    用於當關注的資源發生變化時採取一定的操作。
    “notify”這個action可用於在每個play的最後被觸發,這樣可以避免多次有改變發生時每次都執行指定的操作,取而代之,僅在所有的變化發生完成後一次性地執行指定操作。在notify中列出的操作稱爲handler,也即notify中調用handler中定義的操作。
        - name: template configuration file
          template: src=template.j2 dest=/etc/foo.conf
          notify:
             - restart memcached
             - restart apache 
    handler是task列表,這些task與前述的task並沒有本質上的不同。
        handlers:
            - name: restart memcached
              service:  name=memcached state=restarted
            - name: restart apache
              service: name=apache state=restarted
                                                                               
案例:
    heartbeat.yaml
    - hosts: hbhosts
      remote_user: root
      tasks:
        - name: ensure heartbeat latest version
          yum: name=heartbeat state=present
        - name: authkeys configure file
          copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys
        - name: authkeys mode 600
          file: path=/etc/ha.d/authkeys mode=600
          notify:
            - restart heartbeat
        - name: ha.cf configure file
          copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf
          notify:
            - restart heartbeat
      handlers:
        - name: restart heartbeat
          service: name=heartbeat state=restarted



案例:
    heartbeat.yaml  這個是yaml文件
    - hosts: hbhosts 這個是主機組
      remote_user: root 這個是遠程執行的用戶
      tasks:             任務.
        - name: ensure heartbeat latest version  這個是幹活的名稱
          yum: name=heartbeat state=present       具體幹活
        - name: authkeys configure file
          copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys
        - name: authkeys mode 600
          file: path=/etc/ha.d/authkeys mode=600
          notify:
            - restart heartbeat
        - name: ha.cf configure file
          copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf
          notify:         通知handlers幹活
            - restart heartbeat
      handlers:
        - name: restart heartbeat
          service: name=heartbeat state=restarted

我們來測試這個ansible幹活不

- hosts: hanodes
  remote_user: root
  vars:
    crmsh: crmsh-1.2.6-4.el6.x86_64.rpm
    pssh: pssh-2.3.1-2.el6.x86_64.rpm
  tasks:
    - name: corosync installing
      yum: name=corosync state=present
    - name: pacemaker installing
      yum: name=pacemaker state=present
    - name: crmsh rpm packages
      copy: src=/ansible/corosync/packages/` crmsh ` dest=/tmp/` crmsh `
    - name: pssh rpm packages
      copy: src=/ansible/corosync/packages/` pssh ` dest=/tmp/` pssh `
    - name: crmsh installing
      command: yum -y install /tmp/` crmsh ` /tmp/` pssh `
    - name: authkey configure file
      copy: src=/ansible/corosync/conf/authkey dest=/etc/corosync/authkey
    - name: authkey mode 400
      file: path=/etc/corosync/authkey mode=400
      notify:
        - restart corosync
    - name: corosync.conf configure file
      copy: src=/ansible/corosync/conf/corosync.conf dest=/etc/corosync/corosync.conf
      tags:
        - conf
      notify:
        - restart corosync
    - name: ensure the corosync service startup on boot
      service: name=corosync state=started enabled=yes
  handlers:
    - name: restart corosync
      service: name=corosync state=restarted


下面是顯示:

[root@node1 ~]# time ansible-playbook /ansible/corosync/corosync.yaml
PLAY [hanodes] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [node1.king.com]
ok: [node2.king.com]
ok: [node3.king.com]
TASK: [corosync installing] ***************************************************
ok: [node1.king.com]
failed: [node3.king.com] => {"changed": false, "failed": true, "rc": 0, "results": []}
msg: The following packages have pending transactions: corosync-x86_64
failed: [node2.king.com] => {"changed": false, "failed": true, "rc": 0, "results": []}
msg: The following packages have pending transactions: corosync-x86_64

  上面的綠色是表示幹活成功,紅色是表示幹活失敗

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