Ansible安裝並帶您入門


一、簡介

       Puppet, SaltStack, Chef, Ansible都是服務器批量運維常用工具,其中Ansible最大的亮點在於”無客戶端、簡單易用和日志集中控管。”,不用幫每台機器 (instance) 預載 agent,只要有 SSH 和 Python 就可以。,本文介紹一下Ansible的基本使用。

 

二、結構

 

  1、當 Control Machine (主控端) 可以用 SSH 連上 Managed node,且被連上的機器里有預載 Python 時,Ansible 就可以運作了!

  2、Managed node 則是被 Ansible 操縱的機器。

 

三、在Control Machine中安裝Ansible

Ubuntu (Apt)

    安裝 add-apt-repository 必要套件。
    $ sudo apt-get install -y python-software-properties software-properties-common

    使用 Ansible 官方的 PPA 套件來源。
    $ sudo add-apt-repository -y ppa:ansible/ansible; sudo apt-get update

    安裝 Ansible。
    $ sudo apt-get install -y ansible


CentOS (Yum)

    新增 epel-release 第三方套件來源。
    $ sudo yum install -y epel-release

    安裝 Ansible。
    $ sudo yum install -y ansible


macOS (Homebrew)

    請先安裝 homebrew,已安裝者請略過。
    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    安裝 Ansible。
    $ brew install ansible

Python (Pip)
    Ansible 近來的釋出速度很快,若想追求較新的版本可改用 Pip 的方式進行安裝,較不建議初學者使用。
    需請先安裝 pip,已安裝者請略過。
    # Debian, Ubuntu
    $ sudo apt-get install -y python-pip


    # CentOS
    $ sudo yum install -y python-pip
    
    # macOS
    $ sudo easy_install pip
    
    升級 pip。
    $ sudo pip install -U pip
    
    安裝 Ansible。
    $ sudo pip install ansible

 

 

四、在Managed node 中安裝Python和SSH(一盤系統都自帶了,因此可忽略此步驟)

Ubuntu.
$ sudo apt-get install -y openssh-server python2.7

CentOS.
$ sudo yum install -y openssh-server python

 

五、修改配置

  1、安裝好Ansible后,我們可以在/etc/ansible目錄下看到配置文件。修改:ansible.cfg,例如:

inventory = hosts 
remote_user = root 
host_key_checking=False

   2、修改/etc/ansible/hosts文件,參考下圖,可以使用如下變量:ansible_ssh_host、ansible_ssh_port、ansible_ssh_user、ansible_ssh_pass、ansible_ssh_private_key_file。

  

  例如我測試使用的配置:

  

 

六、測試執行命令

  如:執行:

 

$ ansible localhost -m command -a 'echo Hello World.'
localhost | SUCCESS | rc=0 >>
Hello World.

   

 

 

 

 

七、測試playbook

  1、在/etc/ansible目錄下,執行:vim ./helloworld.yml 內容如下:

---

- name: say 'hello world'
  hosts: huawei
  tasks:

    - name: echo 'hello world'
      command: echo 'hello world'
      register: result

    - name: print stdout
      debug: var=result
                            

  2、執行:ansible-playbook ./helloworld.yml

  

 八、模板測試

  

 

  1、新建template文件,vi helloworld2.txt.j2,內容如下:

Hello "{{ dynamic_world }}"

  2、新建playbook文件,vi helloworld2.yml,內容如下:

---
- name: Play the template module
  hosts: all
  vars:
    dynamic_world: "World"

  tasks:
    - name: generation the hello_world.txt file
      template:
        src: helloworld2.txt.j2
        dest: /tmp/hello_world.txt

    - name: show file context
      command: cat /tmp/hello_world.txt
      register: result

    - name: print stdout
      debug:
        msg:

  3、執行playbook:

ansible-playbook helloworld2.yml

  

 

  4、指定環境變量

ansible-playbook helloworld2.yml -e "dynamic_world=SongXingzhu"

  

 

 

 

 

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM