天天看點

Ansible配置及使用

Ansible特點

基于python語言開發

不需要安裝用戶端,通過sshd通信

基于子產品工作,子產品支援多種語言開發

支援編寫yaml格式的playbook,配置檔案縮進2行

支援sudo

提供UI,流量器圖形化 www.ansible.com/tower

開源UI https://gitub.com/alaxli/ansible_ui

一、Ansible安裝配置

1. 環境

    master:172.16.115.157

    agent:172.16.115.202

2. 服務端安裝ansible,用戶端無需安裝,關閉selinux

    setenforce 0

    yum install -y epel-release

    yum install -y ansible

    yum install -y openssl-clients

3. 服務端生成密鑰對,目錄/root/.ssh/

    ssh-keygen -t rsa   //無需設定單獨密碼,生成公鑰和秘鑰id_rsa  id_rsa.pub

4. 将服務端公鑰内容放到客戶機和本機認證檔案中,授600權限

    scp /root/.ssh/id-rsa.pub   ip:/root/.ssh/authorized_keys

    cat /root/.ssh/id-rsa.pub >>/root/.ssh/authorized_keys  // ssh 127.0.0.1

    chmod 600 /root/.ssh/authorized_keys

5. 修改ansible配置檔案/etc/ansible/hosts,添加組以及組内IP

    [testhost]             #自定義主機組名字

    172.16.115.157     #組内機器ip或者機器域名(需先配置好/etc/hosts)

    127.0.0.1

二、指令子產品使用

1. Ansible文檔使用

    ansible-doc -l        # 列出所有子產品

    ansible-doc cron   # 檢視指定子產品文檔

2. ping指令子產品

    ansible testhost -m ping  #測試機器是否線上

3. command指令子產品

    ansible testhost -m command -a 'w'              #批量執行指令;-m 後面跟子產品名;-a 跟指令

    ansible 172.16.115.202 -m command -a 'w'   #單獨執行指令

4. shell指令子產品

    說明:shell包含command,并且支援管道和遠端執行腳本

    ansible testhost -m -a 'w'

    注:可能用到的擴充包:yum install -y libselinux-python  

5. copy指令子產品-拷貝目錄和檔案

    說明:可同時指定使用者屬主和權限,源目錄拷貝到目标目錄下面去,如果目标目錄不存在,則會自動建立

    ansible master.huangzp.com -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0755"

    ansible master.huangzp.com -m copy -a "src=/etc/passwd dest=/tmp/1.txt"

6. cron指令子產品-添加和删除計任務

    說明:name計劃名稱,job計劃動作

    ansible testhost -m -a "name='test_cron' job='/bin/touch /tmp/ansible_test02.txt day='1-30' weekday='4'"

    ansible testhost -m cron -a "name='test_cron' state='absent'"

    注:分鐘minute  小時hour  日期day  月份month  周weekday

7. yum指令子產品-安裝rpm包

    說明:rpm需寫全稱(實際,遠端執行shell指令更友善),可以填寫state=installed

    ansible testhost -m yum -a "name=iftop"

    注:雙引号中沒有單引号

8. service指令子產品-管理rpm服務

    說明:state狀态可以為:啟動start、停止stop等;enabled為開機啟動

    ansible testhost -m service -a "name=iftop state=started enabled=yes"

三、ansible playbook 

     相當于将各指令子產品内容寫進配置檔案中,然後集中執行,類似于shell腳本。例如:實際生産中,需批量管理很多機器,yum安裝,管理配置檔案、服務等

1. ansible配置檔案格式

    vim /etc/ansible/test.yml   # 以.yml結尾

<code>--- </code><code>#固定格式開頭</code>

<code> </code><code>- hosts: testhost </code><code>#目标主機清單,可以是單台主機</code>

<code>   </code><code>remote_user: root </code><code>#指定用什麼使用者登入遠端主機執行操作</code>

<code>   </code><code>tasks:  </code>

<code>   </code><code>- name: test_playbook </code><code>#任務描述,會列印出來</code>

<code>    </code><code>shell: </code><code>/bin/touch</code> <code>/tmp/ansible_test03</code><code>.txt </code><code>#具體任務</code>

2. 執行ansible playbook

    ansible-playbook test.yml

3. 搜集機器上系統相關資訊,用到setup子產品

    說明:當管理較多不同系統的主機時,可以根據擷取到的不同類型執行對應操作,如Ubuntu,使用apt-get

     ansible testhost -m setup

4. ansible變量-user指令子產品

    vim /etc/ansible/create_user.yml

<code>---</code>

<code>- name: create_user</code>

<code>  </code><code>hosts: agent.huangzp.com</code>

<code>  </code><code>gather_facts: </code><code>false</code> <code>#是否啟用setup指令子產品擷取的資訊</code>

<code>  </code><code>vars:</code>

<code>   </code><code>- user: </code><code>"test"</code> <code>#var指定user為變量,test為變量的值,需引号</code>

<code>  </code><code>tasks:</code>

<code>      </code><code>- name: create user</code>

<code>        </code><code>user: name=</code><code>"{{ user }}"</code> <code>#name為user指令子產品的參數,{{ user }}變量表示形式</code>

5. file指令子產品-ansible循環

    vim /etc/ansible/loop.yml

<code>- hosts: testhost</code>

<code>  </code><code>user: root</code>

<code>    </code><code>- name: change mode </code><code>for</code> <code>file</code>

<code>     </code><code>file</code><code>: path=</code><code>/tmp/</code><code>{{ item }} mode=600 owner=root group=root</code>

<code>     </code><code>with_items:</code>

<code>      </code><code>- 1.txt</code>

<code>      </code><code>- 2.txt</code>

6. when指令子產品-ansible條件判斷

    vim /etc/ansible/when.yml

<code>  </code><code>gather_facts: True</code>

<code>   </code><code>- name: use when</code>

<code>    </code><code>shell: </code><code>/bin/touch</code> <code>/tmp/when</code><code>.txt</code>

<code>    </code><code>when: facter_ipaddress == </code><code>"172.16.115.202"</code>

7. handlers指令子產品-條件判斷

 vim /etc/ansible/handlers.yml

    說明:隻有copy子產品執行後,再執行Handlers,與的關系。如果客戶機上已經有一個2.txt檔案,且内容和服務機上的passwd一緻,判斷之後,這一步不執行,進而也不會調用test handlers;非常适合同步配置檔案,比如:如果配置檔案更改了就重新開機服務,如果沒有更改就不重新開機

<code>- hosts: agent.huangzp.com</code>

<code>  </code><code>name: handlers </code><code>test</code>

<code>   </code><code>- name: copy </code><code>file</code>

<code>    </code><code>copy: src=</code><code>/etc/passwd</code> <code>dest=</code><code>/tmp/2</code><code>.txt</code>

<code>    </code><code>notify: </code><code>test</code> <code>handlers</code>

<code>  </code><code>handlers:</code>

<code>    </code><code>- name: </code><code>test</code> <code>handlers</code>

<code>     </code><code>shell: </code><code>echo</code> <code>"test"</code> <code>&gt;&gt; </code><code>/tmp/2</code><code>.txt</code>

     注:編輯agent.huangzp.com上/tmp/2.txt去掉test後,master上再次執行ansible-playbook handlers.yml 則不會再執行echo “test” &gt;&gt; /tmp/2.txt

本文轉自 huangzp168 51CTO部落格,原文連結:http://blog.51cto.com/huangzp/1909706,如需轉載請自行聯系原作者