S小魚仔S 網誌搜尋

顯示具有 Ansible 標籤的文章。 顯示所有文章
顯示具有 Ansible 標籤的文章。 顯示所有文章

2019年11月8日 星期五

S小魚仔S Ansible 開發 SNMP 獲取交換機數據

使用「ansible」應用「snmp_facts」模塊需要安裝「Python」依賴「pyasn1 0.4.7」、「pysnmp-4.4.1.tar.gz」,採用offline install」安裝方式因為客戶環境不一定能上網。
PS. 需要安裝「gcc」編譯程序唷

#====安裝 python 「pyasn」依賴模塊====
tar -xzf /opt/pyasn1-0.4.7.tar.gz -C /opt/
cd /opt/pyasn1-0.4.7
python setup.py install

#====安裝 python 「snmp」模塊====
tar -xzf /opt/pysnmp-4.4.1.tar.gz -C /opt/
cd /opt/pysnmp-4.4.1
python setup.py install

#===開發「ansible playbook
PS. 支持「snmp」v1/v2/v2c/v3 協議

#編輯「vi /etc/ansible/hosts

[cisco]
192.168.6.252 ansible_connection=local

#編寫「ansible playbook


- hosts: localhost
  gather_facts: no
  vars:
    snmp_host: 192.168.6.252
    snmp_community: public
    snmp_version: v2c
  tasks:
   - name: Gather facts with SNMP version 2
     snmp_facts:
       host: "{{snmp_host}}"
       version: "{{snmp_version}}"
       community: "{{snmp_community}}"
     register: snmp_log
   - name: print snmp_log
     debug:
       var: snmp_log

#顯示結果


相關參考資料
qiita.com」-「AnsibleでCisco IOSの情報収集

2019年10月31日 星期四

S小魚仔S Ansible 開發 H3C (華三) 交換機

很多初學 ansible 的都會誤以為,ansible 是 ssh 到目的主機上,然後直接運行 cli 命令,其實並不是,ansible 對管理網路設備雖然不需要安裝 agent,但是需要其擁有「python」。「ansible」主機會把需要執行的「module」(完成特定功能的python程式) 傳送到目的主機去執行,因此我們其實需要一個「moudle」能完成「ssh」到網路設備上並下發指定命令的功能。Ansible Host 安裝 Python 我們完全可以透過「Ansible Host」下發指令,這樣就不需要「ansible」通過「paramiko」傳輸「module」,只需要在設定檔(vi /etc/ansible/hosts)中指定「connection」為「local」即可。
PS. H3C 網路設備必須啟用「ssh」登入

1. 修改「vi /etc/ansible/hosts」設定「交換機」IP  為 local


2. 編輯「vi /etc/ansible/ansible.cfg」啟用「library」模組

3. 建立「my_modules」資料夾

mkdir -p /usr/share/my_modules/

4. 下載 GitHub 作者「luffycjf」( network_automation ) 開發「模塊

cd /opt ; git clone https://github.com/luffycjf/network_automation

5. 搬移「ssh_command.py」模塊,放到「ansible library」位置

mv /opt/network_automation/module/ssh_command.py /usr/share/my_modules/

6. 編寫「ansible - playbook
PS. Playbook 採用 yml 描述語言檔,縮排位置一定要對齊
- hosts: localhost
  gather_facts: no
  vars:
   - ssh_port: 22                    #ssh端口号
     ssh_username: "test"            #设备用户名
     ssh_password: "test2019"        #设备密码
     device_address: "172.21.6.2"    #设备地址
     command_interval: 0.5           #命令之间等待的最小间隔,默认0.5s,可以不改
  tasks:
    #     command: ''                                   #指定操作的命令。
    #     configfile: '/opt/h3c_switch/h3c_config.txt'  #指定操作「h3c」配置腳本。
    - name: display version
      ssh_command:
        port: "{{ssh_port}}"
        address: "{{device_address}}"
        username: "{{ssh_username}}"
        password: "{{ssh_password}}"
        command_interval: 0.1
        command: 'display ip interface brief'
        configfile: ''
      register: h3c_log
 
    - name: print h3c_log
      debug:
        var: h3c_log.stdout_lines
      with_items: h3c_log.results
7. 執行結果


參考資料
Cheng's Blog」-「网络设备配置建模 Ⅰ
Ansible Web Site」-「Ansible for Network Automation 

2019年4月12日 星期五

S小魚仔S Ansible 支持 Windows 自動化操作

經過 萬能 Google 終於找到 Ansible 自動化操作 Windows 實作案例,Ansible 需要安裝「python pywinrm」模組,搭配Windows「Powershell 4.0」和「windows winrm」兩個組件完成自動化操作,本篇採用「Server 2008 R2」為「Node」(節點)。
PS. Server 2012 R2 以上系統已經有「Powershell 4.0」。

先決條件

1. 安裝「.NET Framework 4.5.2

2. 安裝「Windows Management Framework 4.0」(Powershell 4.0)



3. 使用 Powershell 輸入「get-host」確認「版本

4. 修改「Powershell」策略允許「remotesigned
set-executionpolicy remotesigned

5. 啟用「winrm quickconfig」服務

6. 輸入「winrm set winrm/config/service/auth '@{Basic="true"}'

7. 輸入「winrm set winrm/config/service '@{AllowUnencrypted="true"}'

8. 開放「Windows」防火牆
netsh advfirewall firewall add rule name="Winrm_Tcp_5985_Port" protocol=TCP dir=in localport=5985 action=allow

~~~~ 設定「Ansible」主機 ~~~~

1. Ansible 主機 使用「pip」安装「pywinrm」模組
pip install "pywinrm>=0.2.2"

2. 檢查「pywinrm」版本
pip list | grep "pywinrm"

3. 編寫「/etc/ansible/host」加入「監控」windows node
[windows]
192.168.8.111 ansible_ssh_user="Administrator" ansible_ssh_pass="xxxxx" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

4. 使用「win_shell」模組,查詢 IP 設置
ansible 192.168.8.111 -m win_shell -a 'ipconfig'

參考資料
www.cnblogs.com」-「厉害—Ansible管理windows集群

2018年12月4日 星期二

S小魚仔S Ansible Playbook 支持 Cisco 自動化佈署

要完成這項任務首先必須學會如何搭建「Ansible」,思科交換機啟用「SSH」,接著學會寫「Ansible」腳本,這篇採用「Ansible Playbook」撰寫。

ansible - playbook 格式
1. 編輯「/etc/ansible/hosts」 ( 配置 群組 與 思科交換機 IP )

[routers]
192.168.8.107 ansible_connection=local

2. 編寫「config_cisco.yml」 (注意縮排必須一致)

- hosts: 192.168.8.107
  gather_facts: yes
  vars:
    - cisco_host_ip: 192.168.8.107
      ssh_username: usr1
      ssh_password: usr123
      enable_open: yes
      enable_password: 1000v
  roles:
     - config_cisco

3. 編寫「tasks => main.yml」 (注意縮排必須一致)

- name: cisco_description_ssh_certification
  set_fact:
    cisco_verification:
      host: "{{cisco_host_ip}}"
      username: "{{ssh_username}}"
      password: "{{ssh_password}}"
      authorize: "{{enable_open}}"
      auth_pass: "{{enable_password}}"

- name: show ip interface
  ios_command:
      commands: show ip interface brief
      provider: "{{cisco_verification}}"
  register: show_ip_log

- name: show_ip_log
  debug:
    var: show_ip_log.stdout_lines
  with_items: show_ip_log.results

執行結果

參考資料

2018年7月25日 星期三

S小魚仔S Centos 7 搭建 Ansible awx 套件 提供 UI 介面操作

Ansible Awx (免費) 提供可視化操作介面,開始進行佈署「Ansible Awx」。

#==修改「電腦」名稱 ( SVN )====
hostnamectl set-hostname ansible_awx

#==關閉「selinux 」======
#關閉「Selinux」為「disabled」才不會阻擋「服務連線」服務
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#==設定 Firewall========

#加入「防火牆」規則 Web UI
sudo firewall-cmd --add-port=80/tcp --permanent

#重新啟動「防火牆」
sudo firewall-cmd --reload


#==更新「Yum」源
yum install -y epel-release

#安裝「postgresq.rpm」更新「yum」源
yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

#安裝「postgresql」
yum install postgresql96-server -y

#安裝「rabbitmq-server」、「wget」、「memcached」、「nginx」、「ansible」
yum install -y rabbitmq-server wget memcached nginx ansible

#下載「mrmeee-awx-epel-7.repo」套件
wget -O /etc/yum.repos.d/awx-rpm.repo https://copr.fedorainfracloud.org/coprs/mrmeee/awx/repo/epel-7/mrmeee-awx-epel-7.repo

#安裝「awx」
yum install -y awx

#初始化「postgresql」資料庫
/usr/pgsql-9.6/bin/postgresql96-setup initdb

#启动rabbitmq服务
systemctl enable rabbitmq-server
systemctl start rabbitmq-server

#启动Mpostgresql服务
systemctl enable postgresql-9.6
systemctl start postgresql-9.6

#启动Memcached服务
systemctl enable memcached
systemctl start memcached

#创建Postgres用户
sudo -u postgres createuser -S awx

#创建Postgres 数据库
sudo -u postgres createdb -O awx awx

#将数据导入数据库
sudo -u awx /opt/awx/bin/awx-manage migrate

#===初始化 Ansible AWX ====
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'root@localhost', 'password')" | sudo -u awx /opt/awx/bin/awx-manage shell

sudo -u awx /opt/awx/bin/awx-manage create_preload_data

sudo -u awx /opt/awx/bin/awx-manage provision_instance --hostname=$(hostname)

sudo -u awx /opt/awx/bin/awx-manage register_queue --queuename=tower --hostnames=$(hostname)


#===配置Nginx===

#備份來源「nginx.conf」
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bkp

#替换nginx conf文件
wget -O /etc/nginx/nginx.conf https://raw.githubusercontent.com/sunilsankar/awx-build/master/nginx.conf

#啟動「Nginx」
systemctl start nginx
systemctl enable nginx


#==========啟動「Awx」相關「程序」
systemctl start awx-cbreceiver
systemctl start awx-celery-beat
systemctl start awx-celery-worker
systemctl start awx-channels-worker
systemctl start awx-daphne
systemctl start awx-web

#=========開機自動啟動「Awx」相關「程序」
systemctl enable awx-cbreceiver
systemctl enable awx-celery-beat
systemctl enable awx-celery-worker
systemctl enable awx-channels-worker
systemctl enable awx-daphne
systemctl enable awx-web

#====需要「Reboot」重開機====
reboot -h now


接下來輸入「http://ip」 ( User: admin Password: password )


Ansible Awx 安裝完成


#===Ansible AWX 有 Bug 必須使用以下指令修復 ( 2019/09/25 ) 更新 ===
PS. 如果不修復任務會一直處於「pending」狀態 

sudo -u awx scl enable rh-python36 rh-postgresql10 "awx-manage create_preload_data"
sudo -u awx scl enable rh-python36 rh-postgresql10 "awx-manage provision_instance --hostname=$(hostname)"
sudo -u awx scl enable rh-python36 rh-postgresql10 "awx-manage register_queue --queuename=tower --hostnames=$(hostname)"
  
systemctl restart awx-cbreceiver
systemctl restart awx-dispatcher
systemctl restart awx-channels-worker
systemctl restart awx-daphne
systemctl restart awx-web

參考資料

2018年7月10日 星期二

S小魚仔S Ansible Roles 簡易攻略

Ansible 提供 Roles 寫法 平常很少使用,作筆記避免忘記
什麼是「Ansible」Roels ?
Roles 是 Ansible自1.2版本引入的新特性,用於層次性,結構化地組織 Playbook 簡單來說就是讓程式井然有序。

我們先來看一下 「Roles」基本 資料夾 結構


metricbeat.yml (呼叫程序)

「tasks」 folder ( main.yml ) 執行程序

templates」 folder ( metricbeat_cfg.j2 ) 存放設定檔

vars」 folder ( main.yml ) 存放變數
# 如何呼叫 ansible 變數  => {{kafka_version}}

2018年5月23日 星期三

S小魚仔S Centos 7 搭建 Ansible 自動化運維 簡易手冊

簡單的自動化IT工具。Ansible基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。

Ansible只需要在一台普通的服務器上運行即可,不需要在被管控的服務器上安裝客戶端。因為它是基於SSH的,Linux服務器離不開SSH,所以Ansible不需要為配置工作添加額外的支持包含客戶端 (Agent) 。

Ansible在管理節點將Ansible模塊通過 SSH 協議(或者 Kerberos、LDAP)推送到被管理端執行,執行完之後自動刪除。

#==修改「電腦」名稱 ( ansible )====
hostnamectl set-hostname ansible

#==關閉「selinux 」======
#關閉「Selinux」為「disabled」才不會阻擋「服務連線」服務
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#====需要「Reboot」重開機====
reboot -h now

#=安裝「epel-release」yum 源
sudo yum install epel-release -y

#=安裝「ansible
sudo yum install ansible -y


一. 「Ansible」 加入「監控主機

#配置「hosts」文檔

vi /etc/ansible/hosts

#「servers」=「group」(群組),ansible 可以指定「群組」或「單一節點」主機

[servers]
192.168.8.50 ansible_ssh_user=test ansible_ssh_pass=test ansible_ssh_port=22
192.168.8.51 ansible_ssh_user=test ansible_ssh_pass=test ansible_ssh_port=22

#設定 「Ansible」配置文檔 ( 關閉 SSH KEY 驗證 )

vi /etc/ansible/ansible.cfg

[defaults]

host_key_checking = false

二.「Ansible」(Ad-Hoc command) 簡易「指令

#修改「單節點」主機「名稱
ansible 192.168.8.50 -m hostname -a "name=python"

#修改「單節點」主機「端口」服務
ansible 192.168.8.51 -m command -a 'netstat -ltunp'

#修改「單節點」主機「網卡
ansible 192.168.8.51 -m command -a 'ifconfig'

#修改「單節點關閉「selinux
ansible 192.168.8.51 -m selinux -a 'state=disabled'

#修改「單節點重啟「主機
ansible 192.168.8.51 -m command -a 'reboot -h now'

#測試「Ping」群組「servers」節點主機
ansible servers -m ping

#複製 來「」到 」檔案 並 賦予「權限
ansible servers -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

#下載「URL」(HTTP) 檔案
ansible servers -m get_url -a 'url=http://releases.ansible.com/ansible/ansible-1.1.tar.gz dest=/opt/'

三.「Ansible」派送「Shell」腳本

#給予「腳本」執行「權限
chmod +x /opt/ansible_test.sh

#複製 來「」到 目「」腳本 並 賦予「權限
ansible servers -m copy -a "src=/opt/ansible_test.sh dest=/opt/ owner=root group=root mode=0755"

#指定「群組」執行「ansible_test.sh」檔案
ansible servers -m shell -a "/opt/ansible_test.sh"

四.「Ansible」派送「*.yml」腳本

Ansible 使用 Playbook 進行「yml」派送,Playbooks 有五個組成結構。

Variables - 變量元素,可傳遞給Tasks/Templates使用
Tasks - 任務元素,即調用模塊完成任務
Templates - 模板元素,可根據變量動態生成配置文件
Hadlers - 處理器元素,通常指在某事件滿足時觸發的操作
Roles - 角色元素


==相關範例如下==

#建立「yml」描述檔
vi /opt/test.yml


- hosts: 192.168.8.51
  #==執行「任務
tasks:
    #===Yum「java」安裝
  - name:  yum install java
yum:
             name: java
             state: latest
    

#執行「test.yml」檔案
ansible-playbook /opt/test.yml

參考資料
运维那点事 运维开发 DBA 分享平台」-「Ansible第一篇:介绍及安装
官方網站」-「http://docs.ansible.com/
http://docs.ansible.com」-「Windows Support
运维那点事 运维开发DBA分享平台」-「Ansible第四篇:Playbook基础