ansible基礎應用

ansible基礎

一、運維主要工作

ansible基礎應用

ansible聚集以上功能于一身,能夠完整輕易的實現應用部署和批量命令功能,適用于主機數量不太多,再大的用puppet。

二、ansible特性

(1)模塊化:調用特定的模塊,完成特定任務;

(2)基于python語言實現,由paramiko,PYYAML和JINJa2三個關鍵模塊組成

(3)部署簡單:agentless,被紅帽收購,故備收入epel源

(4)支持自定義模塊

(5)支持playbook(劇本)

三、ansible組成+部署

ansible基礎應用

部署:yum -y install ansible

配置文件:/etc/ansible/ansible.cfg

主機清單:/etc/ansible/hosts

主程序:ansible、ansible paly-book、ansible-doc

四、準備工作

1、主機(要管理的)納入主機清單

ansible基礎應用

2、基于ssh的方式與要管理主機通信(密鑰)

(1)生成一對密鑰:ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''

(2)將公鑰發給要管理的主機:ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.124

3、然后進行我們的第一個ansible命令吧

簡單實用格式:ansible <host-pattern> [-m module_name] [-a args] [options]

ping管理的所有主機:ansible all -m ping

ansible基礎應用

五、ansible的常用模塊(用ansible-doc -l可以顯示)

(1)command模塊:遠程主機上運行命令

例如:ansible webservers -m command -a "ls /var"

ansible基礎應用

ansible webservers -a "useradd user1",command模塊可以省

ansible webservers -a "echo magedu |passwd –stdin user1" ,不成功,不支持管道

ansible基礎應用

(2)shell模塊:(管道可以用)遠程主機在shell進程下運行命令,支持shell特性

例如:ansible webservers -m shell -a "echo cwj1111|passwd –stdin user1"

ansible基礎應用

(3)copy模塊:把文件復制到遠程位置

例如:ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab" 可以指明mode(權限),group(組),owner(主)。

ansible基礎應用

(4)cron:管理任務計劃的

minute=,day=,month=,weekday=,hour=,job=,name=(必須要給),state=

例如:ansible all -m cron -a "minute=*/5 job='/sbin/ntpdate 192.168.1.109 &> /dev/null' name=Synctime "

ansible基礎應用

在被管理主機上使用crontab -l便可以看到

ansible all -m cron -a "state=absent name=Synctime" 就可以刪除

(5)fetch模塊:拉取文件的(從遠程主機上拉取文件到本地)

ansible-doc -s fetch 查看

(6)file模塊:設定文件屬性(屬組,屬主)

例如:ansible all -m file -a "src=/tmp/fstab path=/tmp/fstab.link state=link"

ansible基礎應用

修改屬性:path= ,owner= ,mode= ,group=

創建目錄:ansible all -m file -a "path=/tmp/tmpdir state=directory "

(7)pip模塊:管理python的模塊

(8)yum模塊:用yum包管理,管理包

例如:ansible all -m yum -a "name=httpd state=present"

ansible基礎應用

上面顯示的是我已經裝過了,就沒有改變,現在我們卸載了看看:ansible all -m yum -a "name=httpd state=absent"

ansible基礎應用

在使用rpm看一下就沒有了

ansible基礎應用

(9)service模塊:管理服務

name=,state=, started(啟動),stopped(停止),restarted(重啟), enabled=,runlevel=

例如:先查看兩臺主機的80端口,再啟動

ansible all -m shell -a "ss -tnl |grep :80 "

ansible all -m service -a "name=httpd state=started"

ansible基礎應用

啟動之后:

ansible基礎應用

(10)user模塊:管理用戶,賬號,組

name=,system=,uid=,shell=,group=,groups=,home=,passwd=,remove=(state=absent,同時刪除家目錄)

例如:ansible all -m user -a "name=user2 system=yes state=present uid=306 "

ansible基礎應用

(11)setup模塊,收集變量

六、ansible-playbook

ansible使用YAML語法描述配置文件,YAML語法以簡潔明了、結構清晰著稱。ansible的任務配置文件被稱為playbook,就是劇本,每個劇本里面包含一系列的任務,每個任務在ansible中又被稱為“戲劇”(play)。

(1)YAML語法格式

數據結構可以用類似大綱的縮排方式呈現,結構通過通過縮進來表示,連續的項目可以通過減號“-”來表示,map結構里面的key/value對用冒號“:”來分隔。如下:

house:
family:
name:Doe
parents:
    - John
    - Jane
children:
    -Paul
    -Mark
    -Simone
address:
number: 34
street:Main Street
city:Nowheretown
zipcode:12345

- hosts:webservers
vars:
    http_port:80
    max_clients:200
remote_user:root
tasks:
    -name:ensure apache is at the latest version
yum:pkg=httpd state=latest
template:src=/src/httpd.j2 dest=/etc/httpd.conf
notify:restart apache
    -name:ensure apache is running
service:name=httpd state=restarted

(2)例子

先創建一個working文件夾,再cd到working里面,把yaml文件移到里面,創建一個files目錄,把配置好的httpd.conf移到里面去,改一下監聽端口,改成8080

ansible基礎應用

ansible-playbook –check web.yaml

ansible基礎應用

測試一下:

啟動:ansible all -m service -a "name=httpd state=restarted"

ansible all -m shell -a "ss -tnl |grep :8080"

ansible基礎應用

(3)上面的有一個缺陷,我改動那個配置文件了怎么辦,這個時候就要用handlers了

在特定條件下觸發;接收到其他任務的通知時被觸發。Tasks中的任務都是有狀態的,changed或者ok。 在Ansible中,只在task的執行狀態為changed的時候,才會執行該task調用的handler。Handlers 最佳的應用場景是用來重啟服務,或者觸發系統重啟操作.除此以外很少用到了。我改了配置文件要重啟吧。

ansible基礎應用

(4)指定哪一出獨唱用tags

我只想運行playbook的某一步,其他的不運行

(5)variables變量

1、facts:可以直接調用

2、ansible-playbook命令的命令行中可以自定義變量:-e 使用變量

ansible基礎應用

ansible-playbook -e pkname=memcached –check web2.yaml

3、通過roles傳遞變量

4、Host Inventory

向不同的主機傳遞不同的變量
vim /etc/ansible/hosts
[webservers]
192.168.1.106  hname=www1
192.168.1.107  hname=www2

vim hostname.yaml
- hosts: webservers
  remote_user:root
  tasks:
  - name: set hostname
  - hostname: name = {{ hname }}

ansible-playbook --check hostname.yaml

向不同的主機中傳入相同的變量
vim /etc/ansible/hosts
[webservers]
http_port=8080

inventory參數:用于定義ansible遠程連接目標主機的參數,而非變量

vim /etc/ansible/hosts
[webservers]
ansible_ssh_user=root
ansible_ssh_pass=cwj888
……

(6)模版templates:是一個文本文件內容嵌套腳本,是使用模版編程語言編程。

Jinja2:

字面量:

        字符串:使用單引號或雙引號
        數字:整數,浮點數
        列表:[item1,item2]
        元組:(item1,item2,……)
        字典:{key1:value1,key2:value2,……}
        布爾型:true|false


算術運算:
        +,-,*,/,//,%,**

比較:
        ==,!=,>=,>,<,<=

邏輯運算:
    and or not

template模塊:基于模版方式生成一個文件復制到遠程主機

例子,

(1)首先:ansible all -m setup | grep ansibleprocessorvcpus

ansible基礎應用

(2)在working/files/ 下,編輯一個epel源,這里我們直接wget 阿里的好了:wget -O working/files/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

(3)復制過去:ansible all -m copy -a "src=files/epel.repo dest=/etc/yum.repos.d "

(4)裝上nginx:ansible all -m yum -a "name=nginx state=present "

(5)創建模版文件:cp /etc/nginx/nginx.conf files/nginx.conf.j2

vim files/nginx.conf.j2

workerprocesses {{ ansibleprocessor_vcpus }};

(6)使用template將模版復制過去:

ansible基礎應用

好了,現在做的結果是讓worker_processes跟cpu的內核數相等。它比copy更厲害的是,它配置的是模版文件,就像作文模版一樣,可以根據你的需要配置吻合不同機器一個模版文件,而copy的文件是固定不變的。

(7)條件判斷,when語句,在task中使用,支持jinja2的語法格式

tasks:
- name: install conf file centos7
template: src=files/nginx.conf.c7.j2
when: ansible_distribution_major_version=="7"
- name: install conf file centos6
template: src=files/nginx.conf.c6.j2

(8)循環,迭代,我一下裝好幾個包

- name: install some pakeages
  yum: name={{ item }} state=present
  with_items:
  - nginx
  - memcached
  - php-fpm

原創文章,作者:N24_yezi,如若轉載,請注明出處:http://www.www58058.com/65855

(0)
N24_yeziN24_yezi
上一篇 2017-01-04 18:50
下一篇 2017-01-04 20:33

相關推薦

  • Linux下常用壓縮工具總結

    gzip: 1)只能針對普通文件進行壓縮,對文件夾、符號鏈接無效。 2)如果想對多個文件一起壓縮并打包,gzip是無法辦到的,需要結合tar進行 [root@mysql-master databackup]# ll 總用量 32 drwx—— 2 root root 4096&nb…

    Linux干貨 2016-12-05
  • 馬哥教育網絡班N22期+第五周課程練習

    1、顯示當前系統上root、fedora或user1用戶的默認shell; ~]# grep -E "^(root|user1|fedora)" /etc/passwd | awk -F: '{ print $1 $7 }' root/bin/bash user1/bin/bash fedora/bin/bash 2、…

    Linux干貨 2016-10-17
  • nginx配置文件中文文檔

    Nginx配置參數中文說明。 #定義Nginx運行的用戶和用戶組user www www; #nginx進程數,建議設置為等于CPU總核心數。worker_processes 8; #全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]error_log /var/log/nginx/error…

    Linux干貨 2017-08-08
  • 網絡21期第十周博客作業

    網絡21期第十周博客作業 1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) Centos6啟動流程:       POST加電自檢 —> BOOT Sequence —> 加載內核啟動系統  &…

    Linux干貨 2016-09-15
  • How to Leverage BiWinning’s Analytical Tools

    Introduction to BiWinning’s Analytical Tools In today’s fast-paced digital world, leveraging analytics is crucial for informed decision-making. BiWinning offers a suite…

    Linux干貨 9小時前
  • file 命令

    文件類型:             – 普通文件             d 目錄文件   &nb…

    2017-07-23

評論列表(1條)

  • luoweiro
    luoweiro 2017-02-23 07:46

    很詳細,不過對于ansible后期使用更多的是結合構建發布體系整合。

欧美性久久久久