最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

ansible安裝及使用2

2023-02-17 10:05 作者:bili_39183997178  | 我要投稿

ansible模塊

yum_repository

????????????? 用于配置yum

????????????? 常用選項:

–??????????? file: 指定文件名

–??????????? 其他選項,請與文件內(nèi)容對照

# 在test組中的主機上,配置yum
[root@control ansible]# ansible test -m yum_repository -a "file=myrepo name=myApp description='My App' baseurl=ftp://192.168.4.254/rhel8/AppStream gpgcheck=no enabled=yes"

[root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
[myApp]
baseurl = ftp://192.168.4.254/rhel8/AppStream
enabled = 1
gpgcheck = 0
name = My App

[root@control ansible]# ansible test -m yum_repository -a "file=myrepo name=BaseOS description='Base OS' baseurl=ftp://192.168.4.254/rhel8/BaseOS gpgcheck=no enabled=yes"

[root@node1 ~]# cat /etc/yum.repos.d/myrepo.repo
[myApp]
baseurl = ftp://192.168.4.254/rhel8/AppStream
enabled = 1
gpgcheck = 0
name = My App

[BaseOS]
baseurl = ftp://192.168.4.254/rhel8/BaseOS
enabled = 1
gpgcheck = 0
name = Base OS

yum模塊

????????????? 用于rpm軟件包管理,如安裝、升級、卸載

????????????? 常用選項:

–??????????? name:包名

–??????????? state:狀態(tài)。present表示安裝,如果已安裝則忽略;latest表示安裝或升級到最新版本;absent表示卸載。

# 在test組中的主機上安裝tar
[root@control ansible]# ansible test -m yum -a "name=tar state=present"

# 在test組中的主機上安裝wget、net-tools
[root@control ansible]# ansible test -m yum -a "name=wget,net-tools"

# 在test組中的主機上卸載wget
[root@control ansible]# ansible test -m yum -a "name=wget state=absent"

service模塊

????????????? 用于控制服務。啟動、關(guān)閉、重啟、開機自啟。

????????????? 常用選項:

–??????????? name:控制的服務名

–??????????? state:started表示啟動;stopped表示關(guān)閉;restarted表示重啟

–??????????? enabled:yes表示設置開機自啟;no表示設置開機不要自啟。

# 在test主機上安裝httpd
[root@control ansible]# ansible test -m yum -a "name=httpd state=latest"

#? 在test主機上啟動httpd,并設置它開機自啟
[root@control ansible]# ansible test -m service -a "name=httpd state=started enabled=yes"

邏輯卷相關(guān)模塊

????????????? 邏輯卷可以動態(tài)管理存儲空間。可以對邏輯卷進行擴容或縮減。

????????????? 可以把硬盤或分區(qū)轉(zhuǎn)換成物理卷PV;再把1到多個PV組合成卷組VG;然后在VG上劃分邏輯卷LV。LV可以像普通分區(qū)一樣,進行格式化、掛載。

????????????? 關(guān)閉虛擬機node1,為其添加2塊20GB的硬盤

????????????? LINUX下KVM虛擬機新加的硬盤,名稱是/dev/vdb和/dev/vdc

????????????? vmware虛擬機新加的硬盤,名稱是/dev/sdb和/dev/sdc

????????????? 如果選nvme硬盤,名稱可能是/dev/nvme0n1和/dev/nvme0n2

[root@node1 ~]# lsblk??? # 可以查看到新加的硬盤vdb和vdc
NAME?? MAJ:MIN RM? SIZE RO TYPE MOUNTPOINT
sr0???? 11:0??? 1 1024M? 0 rom?
vda??? 253:0??? 0?? 30G? 0 disk
`-vda1 253:1??? 0?? 20G? 0 part /
vdb??? 253:16?? 0?? 20G? 0 disk
vdc??? 253:32?? 0?? 20G? 0 disk

lvg模塊

????????????? 創(chuàng)建、刪除卷組,修改卷組大小

????????????? 常用選項:

–??????????? vg:定義卷組名。vg:volume group

–??????????? pvs:由哪些物理卷構(gòu)成。pvs:physical volumes

# 在test組中的主機上安裝lvm2,state不寫,默認是present
[root@control ansible]# ansible test -m yum -a "name=lvm2"

# 手工在node1上對vdb進行分區(qū)
[root@node1 ~]# fdisk /dev/vdb
Command (m for help): g??? # 創(chuàng)建GPT分區(qū)表
Command (m for help): n??? # 新建分區(qū)
Partition number (1-128, default 1):??? # 回車,使用1號分區(qū)
First sector (2048-41943006, default 2048):?? # 起始位置,回車
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943006, default 41943006): +5G?? # 結(jié)束位置+5G

Command (m for help): n?? # 新建分區(qū)
Partition number (2-128, default 2):?? # 回車,使用2號分區(qū)
First sector (10487808-41943006, default 10487808): # 起始位置,回車
Last sector, +sectors or +size{K,M,G,T,P} (10487808-41943006, default 41943006): # 結(jié)束位置,回車,分區(qū)到結(jié)尾
Command (m for help): w?? # 存盤

[root@node1 ~]# lsblk??? # vdb被分出來了兩個分區(qū)
NAME?? MAJ:MIN RM? SIZE RO TYPE MOUNTPOINT
sr0???? 11:0??? 1 1024M? 0 rom?
vda??? 253:0??? 0?? 30G? 0 disk
`-vda1 253:1??? 0?? 20G? 0 part /
vdb??? 253:16?? 0?? 20G? 0 disk
|-vdb1 253:17?? 0??? 5G? 0 part
`-vdb2 253:18?? 0?? 15G? 0 part
vdc??? 253:32?? 0?? 20G? 0 disk

# 在test組中的主機上創(chuàng)建名為myvg的卷組,該卷組由/dev/vdb1組成
[root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/vdb1"

# 在node1上查看卷組
[root@node1 ~]# vgs
? VG?? #PV #LV #SN Attr?? VSize? VFree
? myvg?? 1?? 0?? 0 wz--n- <5.00g <5.00g

# 擴容卷組。卷組由PV構(gòu)成,只要向卷組中加入新的PV,即可實現(xiàn)擴容
[root@control ansible]# ansible test -m lvg -a "vg=myvg pvs=/dev/vdb1,/dev/vdb2"

[root@node1 ~]# vgs? # 在node1上查看卷組
? VG?? #PV #LV #SN Attr?? VSize? VFree
? myvg?? 2?? 0?? 0 wz--n- 19.99g 19.99g

lvol模塊

????????????? 創(chuàng)建、刪除邏輯卷,修改邏輯卷大小

????????????? 常用選項:

–??????????? vg:指定在哪個卷組上創(chuàng)建邏輯卷

–??????????? lv:創(chuàng)建的邏輯卷名。lv:logical volume

–??????????? size:邏輯卷的大小,不寫單位,以M為單位

# 在test組中的主機上創(chuàng)建名為mylv的邏輯卷,大小為2GB
[root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=2G"

# 在node1上查看邏輯卷
[root@node1 ~]# lvs
? LV?? VG?? Attr?????? LSize Pool Origin Data%? Meta%? Move Log Cpy%Sync Convert
? mylv myvg -wi-a----- 2.00g??
?
# mylv擴容至4GB
[root@control ansible]# ansible test -m lvol -a "vg=myvg lv=mylv size=4G"

[root@node1 ~]# lvs? # 在node1上查看邏輯卷
? LV?? VG?? Attr?????? LSize Pool Origin Data%? Meta%? Move Log Cpy%Sync Convert
? mylv myvg -wi-a----- 4.00g??

filesystem模塊

????????????? 用于格式化,也就是創(chuàng)建文件系統(tǒng)

????????????? 常用選項:

–??????????? fstype:指定文件系統(tǒng)類型

–??????????? dev:指定要格式化的設備,可以是分區(qū),可以是邏輯卷

#? 在test組中的主機上,把/dev/myvg/mylv格式化為xfs
[root@control ansible]# ansible test -m filesystem -a "fstype=xfs dev=/dev/myvg/mylv"

# 在node1上查看格式化結(jié)果
[root@node1 ~]# blkid /dev/myvg/mylv
/dev/myvg/mylv: UUID="46c0af72-e517-4b15-9e53-ec72fbe1d96e" TYPE="xfs"

mount模塊

????????????? 用于掛載文件系統(tǒng)

????????????? 常用選項:

–??????????? path:掛載點。如果掛載點不存在,自動創(chuàng)建。

–??????????? src:待掛載的設備

–??????????? fstype:文件系統(tǒng)類型

–??????????? state:mounted,表示永久掛載

# 在test組中的主機上,把/dev/myvg/mylv永久掛載到/data
[root@control ansible]# ansible test -m mount -a "path=/data src=/dev/myvg/mylv state=mounted fstype=xfs"

# 在node1上查看
[root@node1 ~]# tail -1 /etc/fstab
/dev/myvg/mylv /data xfs defaults 0 0
[root@node1 ~]# df -h /data/
Filesystem???????????? Size? Used Avail Use% Mounted on
/dev/mapper/myvg-mylv? 4.0G?? 61M? 4.0G?? 2% /data


# 在test組中的主機上,卸載/dev/myvg/mylv
[root@control ansible]# ansible test -m mount -a "path=/data state=absent"

# 在test組中的主機上,強制刪除/dev/myvg/mylv
[root@control ansible]# ansible test -m lvol -a "lv=mylv state=absent vg=myvg force=yes"?? # force是強制

# 在test組中的主機上,刪除myvg卷組
[root@control ansible]# ansible test -m lvg -a "vg=myvg state=absent"

Playbook劇本

????????????? 常用于復雜任務的管理,以及管理經(jīng)常要完成的任務

????????????? playbook也是通過模塊和它的參數(shù),在特定主機上執(zhí)行任務

????????????? playbook是一個文件,該文件中需要通過yaml格式進行書寫

YAML

????????????? YAML Ain't a Markup Language:YAML不是一個標記語言

yaml語法規(guī)范

1.????????? yaml文件的文件名,一般以yml或yaml作為擴展名

2.????????? 文件一般以---作為第一行,不是必須的,但是常用

3.????????? 鍵值對使用冒號:表示,冒號后面必須有空格。

4.????????? 數(shù)組使用-表示,-后面必須有空格。

5.????????? 相同的層級必須有相同的縮進。如果縮進不對,則有語法錯誤。每一級縮進,建議2個空格。

6.????????? 全文不能使用tab,必須使用空格。

配置vim適應yaml語法

# 文件位置和名字是固定的,用于設置vim的格式
[root@control ansible]# vim ~/.vimrc
set ai??????? # 設置自動縮進
set ts=2????? # 設置按tab鍵,縮進2個空格
set et??????? # 將tab轉(zhuǎn)換成相應個數(shù)的空格

編寫playbook

????????????? 一個劇本(即playbook),可以包含多個play

????????????? 每個play用于在指定的主機上,通過模塊和參數(shù)執(zhí)行相應的任務

????????????? 每個play可以包含多個任務。

????????????? 任務有模塊和參數(shù)構(gòu)成。

# 編寫用于測試連通性的playbook,相當于執(zhí)行ansible all -m ping
[root@control ansible]# vim test.yml
---
- hosts: all
? tasks:
??? - ping:

[root@control ansible]# ansible-playbook test.yml? # 執(zhí)行playbook

# 以上更規(guī)范的寫法如下:
[root@control ansible]# vim test.yml
---
- name: test network??? # play的名字,可選項
? hosts: all??????????? # 作用于所有的主機
? tasks:??????????????? # 任務
??? - name: task 1????? # 第1個任務的名字,可選項
????? ping:???????????? # 第1個任務使用的模塊

[root@control ansible]# ansible-playbook test.yml? # 執(zhí)行playbook


# 在test組的主機和node2上創(chuàng)建/tmp/demo目錄,權(quán)限是0755。將控制端/etc/hosts拷貝到目標主機的/tmp/demo中
[root@control ansible]# vim fileop.yml
---
- name: create dir and copy file
? hosts: test,node2??? # 這里的名稱,必須出現(xiàn)在主機清單文件中
? tasks:
??? - name: create dir
????? file:
??????? path: /tmp/demo
??????? state: directory
??????? mode: '0755'
???
??? - name: copy file
????? copy:
??????? src: /etc/hosts
? ??????dest: /tmp/demo/hosts

# 執(zhí)行playbook
[root@control ansible]# ansible-playbook fileop.yml


# 在test組中的主機上,創(chuàng)建用戶bob,附加組是adm;在node2主機上,創(chuàng)建/tmp/hi.txt,其內(nèi)容為Hello World.
[root@control ansible]# vim two.yml
---
- name: create user
? hosts: test
? tasks:
??? - name: create bob
????? user:
??????? name: bob
??????? groups: adm

- name: create file
? hosts: node2
? tasks:
??? - name: make file
????? copy:
??????? dest: /tmp/hi.txt
??????? content: "Hello World"

[root@control ansible]# ansible-playbook two.yml

????????????? |和>的區(qū)別:|它保留換行符,>把多行合并為一行

# 通過copy模塊創(chuàng)建/tmp/1.txt,文件中有兩行內(nèi)容,分別是Hello World和ni hao
[root@control ansible]# vim f1.yml
---
- name: play 1
? hosts: test
? tasks:
??? - name: mkfile 1.txt
????? copy:
??????? dest: /tmp/1.txt
??????? content: |
????????? Hello World!
????????? ni hao.

[root@control ansible]# ansible-playbook f1.yml
# 查看結(jié)果
[root@node1 ~]# cat /tmp/1.txt
Hello World!
ni hao.


# 通過copy模塊創(chuàng)建/tmp/2.txt,文件中有一行內(nèi)容,分別是Hello World! ni hao
[root@control ansible]# vim f2.yml
---
- name: play 1
? hosts: test
? tasks:
??? - name: mkfile 2.txt
????? copy:
??????? dest: /tmp/2.txt
??????? content: >
????????? Hello World!
????????? ni hao.

[root@control ansible]# ansible-playbook f2.yml
[root@node1 ~]# cat /tmp/2.txt
Hello World! ni hao.

????????????? playbook示例

# 在test組中的主機上創(chuàng)建john用戶,它的uid是1040,主組是daemon,密碼為123
[root@control ansible]# vim user_john.yml
---
- name: create user
? hosts: test
? tasks:
??? - name: create user john
????? user:
??????? name: john
??????? uid: 1040
??????? group: daemon
??????? password: "{{'123'|password_hash('sha512')}}"
[root@control ansible]# ansible-playbook user_john.yml

# 在test組中的主機上刪除用戶john
[root@control ansible]# vim del_john.yml
---
- name: delete user
? hosts: test
? tasks:
??? - name: delete user john
????? user:
??????? name: john
?????? ?state: absent
[root@control ansible]# ansible-playbook del_john.yml

硬盤管理

????????????? 常用的分區(qū)表類型有:MBR(主引導記錄)、GPT(GUID分區(qū)表)

????????????? MBR最多支持4個主分區(qū),或3個主分區(qū)加1個擴展分區(qū)。最大支持2.2TB左右的硬盤

????????????? GPT最多支持128個主分區(qū)。支持大硬盤

parted模塊

????????????? 用于硬盤分區(qū)管理

????????????? 常用選項:

–??????????? device:待分區(qū)的設備

–??????????? number:分區(qū)編號

–??????????? state:present表示創(chuàng)建,absent表示刪除

–??????????? part_start:分區(qū)的起始位置,不寫表示從開頭

–??????????? part_end:表示分區(qū)的結(jié)束位置,不寫表示到結(jié)尾

# 在test組中的主機上,對/dev/vdc進行分區(qū),創(chuàng)建1個1GB的主分區(qū)
[root@control ansible]# vim disk.yml
---
- name: disk manage
? hosts: test
? tasks:
??? - name: create a partition
????? parted:
??????? device: /dev/vdc
??????? number: 1
??????? state: present
??????? part_end: 1GiB

[root@control ansible]# ansible-playbook disk.yml

# 在目標主機上查看結(jié)果
[root@node1 ~]# lsblk
NAME?? MAJ:MIN RM? SIZE RO TYPE MOUNTPOINT
.. ...
vdc??? 253:32?? 0?? 20G? 0 disk
`-vdc1 253:33?? 0 1023M? 0 part

# 繼續(xù)編輯disk.yml,對/dev/vdc進行分區(qū),創(chuàng)建1個新的5GB的主分區(qū)
[root@control ansible]# vim disk.yml
... ...
??? - name: add a new partition
????? parted:
??????? device: /dev/vdc
??????? number: 2
??????? state: present
??????? part_start: 1GiB
??????? part_end: 6GiB

[root@control ansible]# ansible-playbook disk.yml
[root@node1 ~]# lsblk
NAME?? MAJ:MIN RM? SIZE RO TYPE MOUNTPOINT
... ...
vdc??? 253:32?? 0?? 20G? 0 disk
|-vdc1 253:33?? 0 1023M? 0 part
`-vdc2 253:34?? 0??? 5G? 0 part

# 繼續(xù)編輯disk.yml,創(chuàng)建名為my_vg的卷組,它由上面創(chuàng)建的vdc1和vdc2構(gòu)成
[root@control ansible]# vim disk.yml
... ...
??? - name: create my_vg
????? lvg:
??????? vg: my_vg
??????? pvs: /dev/vdc1,/dev/vdc2

# 繼續(xù)編輯disk.yml,在my_vg卷組上創(chuàng)建名為my_lv的邏輯卷,大小1G
[root@control ansible]# vim disk.yml
... ...
??? - name: create my_lv
????? lvol:
??????? vg: my_vg
??????? lv: my_lv
??????? size: 1G


# 繼續(xù)編輯disk.yml,格式化my_lv為ext4
[root@control ansible]# vim disk.yml
... ...
??? - name: mkfs my_lv
????? filesystem:
??????? dev: /dev/my_vg/my_lv
??????? fstype: ext4


# 繼續(xù)編輯disk.yml,將my_lv掛載到/data
[root@control ansible]# vim disk.yml
... ...
??? - name: mount my_lv
????? mount:
??????? path: /data
??????? src: /dev/my_vg/my_lv
??????? fstype: ext4
??????? state: mounted

# 完整的disk.yml如下
---
- name: disk manage
? hosts: test
? tasks:
??? - name: create a partition
????? parted:
??????? device: /dev/vdc
??????? number: 1
??????? state: present
??????? part_end: 1GiB

??? - name: add a new partition
????? parted:
??????? device: /dev/vdc
??????? number: 2
??????? state: present
??????? part_start: 1GiB
??????? part_end: 6GiB

??? - name: create my_vg
????? lvg:
??????? vg: my_vg
??????? pvs: /dev/vdc1,/dev/vdc2

??? - name: create my_lv
????? lvol:
??????? vg: my_vg
??????? lv: my_lv
??????? size: 1G
???????
??? - name: mkfs my_lv
????? filesystem:
??????? dev: /dev/my_vg/my_lv
??????? fstype: ext4

??? - name: mount my_lv
????? mount:
??????? path: /data
??????? src: /dev/my_vg/my_lv
??????? fstype: ext4
??????? state: mounted

?

?


ansible安裝及使用2的評論 (共 條)

分享到微博請遵守國家法律
吉隆县| 沧州市| 汤阴县| 宝兴县| 奎屯市| 朝阳市| 株洲市| 贵南县| 霸州市| 浙江省| 麻城市| 修文县| 思茅市| 哈密市| 都安| 清水河县| 闸北区| 东辽县| 马鞍山市| 黄山市| 古田县| 泉州市| 奉新县| 明溪县| 黑河市| 房产| 蓬溪县| 和平县| 龙口市| 安达市| 汉沽区| 吴旗县| 长葛市| 靖宇县| 乐都县| 四会市| 莆田市| 新营市| 保靖县| 肃宁县| 平昌县|