日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ansible安装及使用

發(fā)布時(shí)間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ansible安装及使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、ansible介紹

1、ansible簡介

? ? ??官方的title是“Ansible is Simple IT Automation”——簡單的自動化IT工具。

??Ansible跟其他IT自動化技術(shù)的區(qū)別在于其關(guān)注點(diǎn)并非配置管理、應(yīng)用部署或IT流程工作流,而是提供一個(gè)統(tǒng)一的界面來協(xié)調(diào)所有的IT自動化功能,因此Ansible的系統(tǒng)更加易用,部署更快。

??Ansible可以讓用戶避免編寫腳本或代碼來管理應(yīng)用,同時(shí)還能搭建工作流實(shí)現(xiàn)IT任務(wù)的自動化執(zhí)行。IT自動化可以降低技術(shù)門檻及對傳統(tǒng)IT的依賴,從而加快項(xiàng)目的交付速度。

2、ansible優(yōu)缺點(diǎn)

? 優(yōu)點(diǎn):

  • 輕量級,他不需要去客戶端安裝agent,更新時(shí),只需要在操作機(jī)上進(jìn)行一次更新即可
  • 批量任務(wù)執(zhí)行可以寫成腳本,而且不用分發(fā)到遠(yuǎn)程就可以執(zhí)行
  • 使用python編寫的,維護(hù)更簡單
  • 支持sudo

? ? ? 缺點(diǎn)

  • 對于幾千臺、上萬臺機(jī)器的操作,還不清楚性能、效率情況如何,需要進(jìn)一步了解。

3、ansible架構(gòu)及工作原理

? ? ?andible基本架構(gòu)如下圖所示

?

  ansible core ansible 自身核心模塊

  host inventory: 主機(jī)庫,定義可管控的主機(jī)列表

  connection plugins: 連接插件,一般默認(rèn)基于 ssh 協(xié)議連接

  modulescore modules ( 自帶模塊 ) custom modules ( 自定義模塊 )

  playbooks :劇本,按照所設(shè)定編排的順序執(zhí)行完成安排任務(wù)?

?Ansible工作原理如下圖:

?

  1、管理端支持local sshzeromq 三種方式連接被管理端,默認(rèn)使用基于ssh的連接---這部分對應(yīng)基本架構(gòu)圖中的連接模塊;

  2、可以按應(yīng)用類型等方式進(jìn)行Host Inventory(主機(jī)群)分類,管理節(jié)點(diǎn)通過各類模塊實(shí)現(xiàn)相應(yīng)的操作---單個(gè)模塊,單條命令的批量執(zhí)行,我們可以稱之為 ? ? ad-hoc

  3、管理節(jié)點(diǎn)可以通過playbooks 實(shí)現(xiàn)多個(gè)task的集合實(shí)現(xiàn)一類功能,如web服務(wù)的安裝部署、數(shù)據(jù)庫服務(wù)器的批量備份等。playbooks我們可以簡單的理解為,系? ? ? 統(tǒng)通過組合多條ad-hoc操作的配置文件 。

Ansible默認(rèn)是通過SSH通道來管理的,也就是它所說的免客戶端方式管理, 它底層是通過 paramiko 來實(shí)現(xiàn)的。 ?

ansible執(zhí)行過程大體過程如下圖,其中暖色調(diào)的代表已經(jīng)模塊化。

二、部署安裝

名稱

IP

操作系統(tǒng)

用途

ansible-server

192.168.95.10/24

CentOS7.1

管理端

client-1

192.168.95.11/24

CentOS7.1

?

client-2

192.168.95.12/24

CentOS7.1

?

?

?

?

?

?

?1、ansible安裝方式有、源碼、pip、yum。

?1.1 源碼安裝

? ? ??源碼安裝需要python2.6以上版本,其依賴模塊paramikoPyYAMLJinja2httplib2simplejsonpycrypto模塊,以上模塊可以通過pipeasy_install 進(jìn)行安裝

?

? ?1.2pip安裝

? ? ? ??pip是專門用來管理Python模塊的工具,Ansible會將每次正式發(fā)布都更新到pip倉庫中。所以通過pip安裝或更新Ansible,會比較穩(wěn)妥的拿到最新穩(wěn)定版。

?1.3 yum安裝

? 1)安裝epel源

yum install http://mirrors.163.com/centos/7.4.1708/extras/x86_64/Packages/epel-release-7-9.noarch.rpm

? 2)查看epel源并安裝ansible

[root@node2 ~]ll /etc/yum.repos.d/epel*

[root@node2 ~]yum install ansible -y

? 3)查看ansible版本

ansible --version

2 ansible組成介紹

tree /etc/ansible/

/etc/ansible/

├── ansible.cfg ?# ansible的配置文件

├── hosts ?# ansible的主倉庫 用來存儲需要管理的遠(yuǎn)程主機(jī)的相關(guān)信息

└── roles

3、ansible七個(gè)命令

ansible?

常用參數(shù):-m 模塊

? ? ? -i?PATH, --inventory=PATH 指定庫存主機(jī)文件的路徑,默認(rèn)為/etc/ansible/hosts.

? ? ? ? ? ? ? ? ? ? ? ?-a? (arguments) 命令行參數(shù)

?

?示例:ansible test1 -m copy -a "src=/tmp/test.txt dest=/tmp/test.txt "

ansible-doc

//列出所有已安裝的模塊

# ansible-doc ?-l

//查看具體某模塊的用法,這里如查看command模塊

?

# ansible-doc -s command

absible-galaxy

ansible-galaxy 指令用于方便的從https://galaxy.ansible.com/ 站點(diǎn)下載第三方擴(kuò)展模塊,我們可以形象的理解其類似于centos下的yumpython下的pipeasy_install 。如下示例:

ansible-galaxy install aeriscloud.docker

?

ansible-link

ansible-lint是對playbook的語法進(jìn)行檢查的一個(gè)工具。用法是ansible-lint playbook.yml

?

ansible-playbook

?

該指令是使用最多的指令,其通過讀取playbook 文件后,執(zhí)行相應(yīng)的動作。

?

ansible-pull

?

適用于以下場景:你有數(shù)量巨大的機(jī)器需要配置,即使使用非常高的線程還是要花費(fèi)很多時(shí)間;你要在一個(gè)沒有網(wǎng)絡(luò)連接的機(jī)器上運(yùn)行Anisble,比如在啟動之后安裝。

?

ansible-vault

?

注意:重點(diǎn)是ansibleansible-playbook

?

4、主機(jī)組(在hosts文件內(nèi)配置)

[hangzhou]

host1

host2

[jiaxing]

host3

?

host4

5、ansible默認(rèn)配置文件

?

Ansible默認(rèn)安裝好后有一個(gè)配置文件/etc/ansible/ansible.cfg

ansible簡單使用

[root@test ansible]#?ansible test1 -m ping -k

SSH password:

?

192.168.147.101 | FAILED => Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. ?Please add this host's fingerprint to your known_hosts file to manage this host.

報(bào)錯(cuò)處理辦法:

ansible.cfg配置文件中,也會找到如下部分:

# uncomment this to disable SSH key host checking

host_key_checking = False ?

默認(rèn)host_key_checking部分是注釋的,通過找開該行的注釋,同樣也可以實(shí)現(xiàn)跳過 ssh 首次連接提示驗(yàn)證部分。

? ? 可見采用密碼方式比較麻煩,實(shí)際應(yīng)用中采用ssh 互信方式。

?

配置ssh 互信時(shí),只需要管理主機(jī)能ssh被管理機(jī)器不需要輸入密碼即可。

三、ansible常用模塊

3.1、ping

ansible test1 -m ping

3.2、setuop

? ? ? ? ?setup模塊,主要用于獲取主機(jī)信息,在playbooks里經(jīng)常會用到的一個(gè)參數(shù)gather_facts就與該模塊相關(guān)。setup模塊下經(jīng)常使用的一個(gè)參數(shù)是filter參數(shù),具體使用示例如下:

?#查看主機(jī)內(nèi)存信息

[root@test ~]# ansible test1 -m setup -a 'filter=ansible_*_mb'

?

#查看網(wǎng)卡信息

[root@test ~]# ansible test1 -m setup -a 'filter=ansible_eth0'

#將所有主機(jī)的信息輸入到/tmp/facts目錄下,每臺主機(jī)的信息輸入到主機(jī)名文件中(/etc/ansible/hosts里的主機(jī)名)

[root@test ~]# ansible all -m setup --tree /tmp/facts

3.3 file 模塊

file模塊主要用于遠(yuǎn)程主機(jī)上的文件操作。

(group、mode、owner):定義文件/目錄

path:定義文件路徑

recurse:遞歸的設(shè)置,只對目錄有效。

src:要被鏈接的源文件路徑,只應(yīng)用于state=link的情況

dest:被連接的路徑,只應(yīng)用于state=link的情況

state:directory:如果目錄不存在,創(chuàng)建目錄

file:即使文件不存在,也不會創(chuàng)建

link:創(chuàng)建軟連接

hard:創(chuàng)建硬鏈接

touch:文件不存在,則會創(chuàng)建。如果存在則會,則更新最后修改的時(shí)間。

absent:刪除目錄、文件或取消鏈接。

?

示例:

?

[root@test ~]# ansible test1 -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"

[root@test ~]#?ansible test1 -m file -a "path=/tmp/fstab state=absent"

?

?[root@test ~]# ansible test1 -m file -a "path=/tmp/fstab state=touch"

?3.4 cop模塊

? ? ?backup:在覆蓋之前將原文件備份,備份文件包含時(shí)間信息。有兩個(gè)選項(xiàng):yes|no

? ? content:用于替代"src",可以直接設(shè)定指定文件的值?

?directory_mode:遞歸的設(shè)定目錄的權(quán)限,默認(rèn)為系統(tǒng)默認(rèn)權(quán)限

? ? ?force:如果目標(biāo)主機(jī)包含該文件,但內(nèi)容不同,如果設(shè)置為yes,則強(qiáng)制覆蓋,如果為no,則只有當(dāng)目標(biāo)主機(jī)的目標(biāo)位置不存在該文件時(shí),才復(fù)制。默認(rèn)為yes

? ? ?others:所有的file模塊里的選項(xiàng)都可以在這里使用

? ? ?src:要復(fù)制到遠(yuǎn)程主機(jī)的文件在本地的地址,可以是絕對路徑,也可以是相對路徑。如果路徑是一個(gè)目錄,它將遞歸復(fù)制。在這種情況下,如果路徑使用"/"來結(jié)尾,則只復(fù)制目錄里的內(nèi)容,如果沒有使用"/"來結(jié)尾,則包含目錄在內(nèi)的整個(gè)內(nèi)容全部復(fù)制,類似于rsync。?

示例:

[root@test ~]#?ansible test1 -m copy -a "src=/tmp/test.txt dest=/tmp/test.txt owner=foo group=foo mode=0644"

3.5 command 模塊

  • creates:一個(gè)文件名,當(dāng)該文件存在,則該命令不執(zhí)行
  • free_form:要執(zhí)行的linux指令
  • chdir:在執(zhí)行指令之前,先切換到該指定的目錄
  • removes:一個(gè)文件名,當(dāng)該文件不存在,則該選項(xiàng)不執(zhí)行
  • executable:切換shell來執(zhí)行指令,該執(zhí)行路徑必須是一個(gè)絕對路徑

?

注意:command模塊不是調(diào)用的shell的指令,所以沒有bash的環(huán)境變量,也不能使用shell的一些操作方式,其他和shell沒有區(qū)別

示例:[root@test ansible]# ansible test1 -a 'df -h'

3.6 shell?模塊

??用法其本和command一樣,不過的是其是通過/bin/sh進(jìn)行執(zhí)行,所以shell 模塊可以執(zhí)行任何命令,就像在本機(jī)執(zhí)行一樣。

示例:[root@test ansible]# ansible test1 -m shell -a 'ps -ef | grep sshd'

?

3.7 raw 模塊

用法和shell 模塊一樣 ,其也可以執(zhí)行任意命令,就像在本機(jī)執(zhí)行一樣

?

注:raw模塊和comandshell 模塊不同的是其沒有chdircreatesremoves參數(shù),chdir參數(shù)的作用就是先切到chdir指定的目錄后,再執(zhí)行后面的命令,這在后面很多模塊里都會有該參數(shù) 。

?

3.8 script 模塊

?

將管理端的shell 在被管理主機(jī)上執(zhí)行,其原理是先將shell 復(fù)制到遠(yuǎn)程主機(jī),再在遠(yuǎn)程主機(jī)上執(zhí)行,原理類似于raw模塊。

3.9 service 模塊

?

  • arguments:給命令行提供一些選項(xiàng)
  • enabled:是否開機(jī)啟動 yes|no
  • name:必選項(xiàng),服務(wù)名稱
  • pattern:定義一個(gè)模式,如果通過status指令來查看服務(wù)的狀態(tài)時(shí),沒有響應(yīng),就會通過ps指令在進(jìn)程中根據(jù)該模式進(jìn)行查找,如果匹配到,則認(rèn)為該服務(wù)依然在運(yùn)行
  • runlevel:運(yùn)行級別
  • sleep:如果執(zhí)行了restarted,在則stopstart之間沉睡幾秒鐘
  • state:對當(dāng)前服務(wù)執(zhí)行啟動,停止、重啟、重新加載等操作(started,stopped,restarted,reloaded

?

?

示例:[root@test ansible]#?ansible test1 -m service -a "name=httpd state=started enabled=yes"

3.10 cron 模塊

  • backup:對遠(yuǎn)程主機(jī)上的原任務(wù)計(jì)劃內(nèi)容修改之前做備份
  • cron_file:如果指定該選項(xiàng),則用該文件替換遠(yuǎn)程主機(jī)上的cron.d目錄下的用戶的任務(wù)計(jì)劃
  • day:日(1-31**/2,……)
  • hour:小時(shí)(0-23**/2,……) ?
  • minute:分鐘(0-59**/2,……)
  • month:月(1-12**/2,……)
  • weekday:周(0-7*,……)
  • job:要執(zhí)行的任務(wù),依賴于state=present
  • name:該任務(wù)的描述
  • special_time:指定什么時(shí)候執(zhí)行,參數(shù):reboot,yearly,annually,monthly,weekly,daily,hourly
  • state:確認(rèn)該任務(wù)計(jì)劃是創(chuàng)建還是刪除
  • user:以哪個(gè)用戶的身份執(zhí)行

示例:[root@test ansible]# ansible test1 -m cron -a 'name="check dirs" hour="5,2" job="ls -alh >/dev/null"'

3.11 filesystem 模塊

  • dev:目標(biāo)塊設(shè)備
  • force:在一個(gè)已有文件系統(tǒng)的設(shè)備上強(qiáng)制創(chuàng)建
  • fstype:文件系統(tǒng)的類型
  • opts:傳遞給mkfs命令的選項(xiàng)

3.12 yum 模塊

?

  • config_fileyum的配置文件
  • disable_gpg_check:關(guān)閉gpg_check
  • disablerepo:不啟用某個(gè)源
  • enablerepo:啟用某個(gè)源
  • name:要進(jìn)行操作的軟件包的名字,也可以傳遞一個(gè)url或者一個(gè)本地的rpm包的路徑
  • state:狀態(tài)(presentabsentlatest

?

示例:[root@test ansible]# ansible test1 -m yum -a 'name=httpd state=latest'

3.13 user和group 模塊

user模塊是請求的是useradd, userdel, usermod三個(gè)指令,goup模塊請求的是groupadd, groupdel, groupmod 三個(gè)指令。

示例:

創(chuàng)建用戶:[root@test ~]# ansible test1 -m user -a 'createhome=yes home=/home/user1 password=123456 name=user1 shell=/bin/bash state=present'

?

?

刪除用戶:[root@test ~]# ansible test1 -m user -a 'remove=yes name=user1 state=absent'

3.14 synchronize 模塊

使用rsync同步文件

  • archive: 歸檔,相當(dāng)于同時(shí)開啟recursive(遞歸)linkspermstimesownergroup-D選項(xiàng)都為yes ,默認(rèn)該項(xiàng)為開啟
  • checksum: 跳過檢測sum值,默認(rèn)關(guān)閉
  • compress:是否開啟壓縮
  • copy_links:復(fù)制鏈接文件,默認(rèn)為no ,注意后面還有一個(gè)links參數(shù)
  • delete: 刪除不存在的文件,默認(rèn)no
  • dest:目錄路徑
  • dest_port:默認(rèn)目錄主機(jī)上的端口 ,默認(rèn)是22,走的ssh協(xié)議
  • dirs:傳速目錄不進(jìn)行遞歸,默認(rèn)為no,即進(jìn)行目錄遞歸
  • rsync_optsrsync參數(shù)部分
  • set_remote_user:主要用于/etc/ansible/hosts中定義或默認(rèn)使用的用戶與rsync使用的用戶不同的情況
  • mode: pushpull 模塊,push模的話,一般用于從本機(jī)向遠(yuǎn)程主機(jī)上傳文件,pull 模式用于從遠(yuǎn)程主機(jī)上取文件

示例:[root@test ansible]# ansible test1 -m synchronize -a 'src=/tmp/helloworld dest=/var/www'

3.15 mount 模塊

  • dump
  • fstype:必選項(xiàng),掛載文件的類型
  • name:必選項(xiàng),掛載點(diǎn)
  • opts:傳遞給mount命令的參數(shù)
  • src:必選項(xiàng),要掛載的文件
  • state:必選項(xiàng)
  • present:只處理fstab中的配置
  • absent:刪除掛載點(diǎn)
  • mounted:自動創(chuàng)建掛載點(diǎn)并掛載之
  • umounted:卸載

示例:

#創(chuàng)建設(shè)備

[root@test ansible]#?ansible test1 -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'

#/dev/loop1關(guān)聯(lián)

[root@test ansible]# ansible test1 -a 'losetup /dev/loop1 /disk.img'

#格式化

[root@test ansible]# ansible test1 -m filesystem -a 'fstype=ext3 force=yes opts=-F?

#掛載

[root@test ansible]#?ansible test1 -m mount -a 'name=/mnt src=/dev/loop1 fstype=ext3 state=mounted opts=rw'

?

四、ansible-playbook

4.1 playbook 構(gòu)成

  • Target section: ??定義將要執(zhí)行 playbook 的遠(yuǎn)程主機(jī)組
  • Variable section: ?定義 playbook 運(yùn)行時(shí)需要使用的變量
  • Task section: ????定義將要在遠(yuǎn)程主機(jī)上執(zhí)行的任務(wù)列表
  • Handler section: ?定義 task 執(zhí)行完成以后需要調(diào)用的任務(wù)

?

一般所需的目錄層有:(視情況可變化)?

  • vars ????變量層
  • tasks ???任務(wù)層
  • handlers 觸發(fā)條件
  • files ???文件
  • template 模板

?

4.2.1host和user

playbook中的每一個(gè)play的目的都是為了讓某個(gè)或某些主機(jī)以某個(gè)指定的用戶身份執(zhí)行任務(wù)。

hosts?用于指定要執(zhí)行指定任務(wù)的主機(jī)其可以是一個(gè)或多個(gè)由冒號分隔主機(jī)組。

user?執(zhí)行該任務(wù)組的用戶

remote_user?則用于指定遠(yuǎn)程主機(jī)上的執(zhí)行任務(wù)的用戶,與user相同。

sudo?如果設(shè)置為yes,執(zhí)行該任務(wù)組的用戶在執(zhí)行任務(wù)時(shí),獲取root權(quán)限。

?

?4.2.2 任務(wù)列表和action

?

?play的主體部分是task list

?

?示例:

tasks:

??- name: make sure apache is running

????service: name=httpd state=running

4.2.3 handlers

notify中列出的操作稱為handler也即notify中調(diào)用 handler中定義的操作。?

注意:在 notify 中定義內(nèi)容一定要和tasks中定義的 - name 內(nèi)容一樣,這樣才能達(dá)到觸發(fā)的效果,否則會不生效。

4.2.4 tags

ansible中可以對playroleincludetask打一個(gè)tag(標(biāo)簽),然后:

  • 當(dāng)命令ansible-playbook-t參數(shù)時(shí),只會執(zhí)行-t指定的tag
  • 當(dāng)命令ansible-playbook--skip-tags參數(shù)時(shí),則除了--skip-tags指定的tag外,執(zhí)行其他所有

?

?4.2.5 var

#變量定義在文件中

[root@test playbooks]# cat variables

port: 80

http: nginx

五、playbook常用模板

template

template模塊和copy類似,不同在于template會做變量替換。

?

常用參數(shù):

?

backup:如果原目標(biāo)文件存在,則先備份目標(biāo)文件

?

dest:目標(biāo)文件路徑

?

force:是否強(qiáng)制覆蓋,默認(rèn)為yes

?

group:目標(biāo)文件屬組

?

mode:目標(biāo)文件的權(quán)限

?

owner:目標(biāo)文件屬主

?

src:源模板文件路徑

?

validate:在復(fù)制之前通過命令驗(yàn)證目標(biāo)文件,如果驗(yàn)證通過則復(fù)制

?

示例:

?

-?template:?src=/mytemplates/foo.j2?dest=/etc/file.conf?owner=bin?group=wheel?mode=0644 -?template:?src=/mytemplates/foo.j2?dest=/etc/file.conf?owner=bin?group=wheel?mode="u=rw,g=r,o=r"

?

set_face

set_fact模塊可以讓你在遠(yuǎn)程受管機(jī)器上去執(zhí)行腳本的過程來計(jì)算我們需要的值,這些值可以被用在模板或者變量中。這些值有點(diǎn)類似setup模塊中的參數(shù),只不過setup模塊是以單臺主機(jī)為單位的。

示例:

tasks:

??-name: Calculate InnoDB buffer pool size

??set?facl: innodb_buffer_pool_size_mb=”{{ ansible_mentotal_mb /2 }}”

pause

暫停模塊可以讓我們在playbooks中暫停一段時(shí)間,可以知道一個(gè)時(shí)間段,或者提示用戶繼續(xù)。在命令行中沒生么有,但在playbook中,很有用處。

示例:

# Pause for 5 minutes to build app cache.

- pause: minutes=5

# Pause until you can verify updates to an application were successful.

- pause:

# A helpful reminder of what to look out for post-update.

- pause: prompt="Make sure org.foo.FooOverload exception is not present"

?

wait_for

wait_for模塊用來檢測一個(gè)tcp端口是否準(zhǔn)備好接收遠(yuǎn)程連接,這是由遠(yuǎn)程主機(jī)來完成的。

示例:

# 10秒后在當(dāng)前主機(jī)開始檢查8000端口,直到端口啟動后返回

- wait_for: port=8000 delay=10

# 檢查path=/tmp/foo直到文件存在后繼續(xù)

- wait_for: path=/tmp/foo

# 直到/var/lock/file.lock移除后繼續(xù)

- wait_for: path=/var/lock/file.lock state=absent

? -name: Wait for Tomcat to start

????wait_for: port=8080 state=started

?

assemble

assemble組裝模塊把多個(gè)受管主機(jī)的文件合并成一個(gè)文件,當(dāng)配置文件不允許包含的時(shí)候,非常有用。特別在設(shè)置root用戶的authorized_keys文件的時(shí)候。

示例:

#?Example from Ansible Playbooks

- assemble: src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf

# When a delimiter is specified, it will be inserted in between each fragment

- assemble: src=/etc/someapp/fragments dest=/etc/someapp/someapp.conf delimiter='### START FRAGMENT ###'

?

add_host

add_host添加主機(jī)模塊是playbook中一個(gè)強(qiáng)大的模塊,它可以讓你動態(tài)的添加受管主機(jī)到一個(gè)play中。

示例:

# add host to group 'just_created' with variable foo=42

- add_host: name={{ ip_from_ec2 }} groups=just_created foo=42

?

group_by

group_by模塊可以讓我們根據(jù)主機(jī)的真實(shí)特性進(jìn)行分組,真實(shí)特性可以通過add_fact來實(shí)現(xiàn)。Group_by模塊只接受一個(gè)參數(shù),key,同樣組名的機(jī)器被分到一個(gè)組里面。

示例:

# Create groups based on the machine architecture

- ?group_by: key=machine_{{ ansible_machine }}

# Create groups like 'kvm-host'

- ?group_by: key=virt_{{ ansible_virtualization_type }}_{{ ansible_virtualization_role }}

?

get_url

該模塊主要用于從httpftphttps服務(wù)器上下載文件(類似于wget),主要有如下選項(xiàng):

  sha256sum:下載完成后進(jìn)行sha256 check

  timeout:下載超時(shí)時(shí)間,默認(rèn)10s

  url:下載的URL

  url_passwordurl_username:主要用于需要用戶名密碼進(jìn)行驗(yàn)證的情況

  use_proxy:是事使用代理,代理需事先在環(huán)境變更中定義

示例:

- name: download foo.conf

??get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440

- name: download file with sha256 check

??get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c

?

debug

調(diào)試模塊,用于在調(diào)試中輸出信息
常用參數(shù):
msg:調(diào)試輸出的消息
var:將某個(gè)任務(wù)執(zhí)行的輸出作為變量傳遞給debug模塊,debug會直接將其打印輸出
verbosity:debug的級別(默認(rèn)是0級,全部顯示)

fail

用于終止當(dāng)前playbook的執(zhí)行,通常與條件語句組合使用,當(dāng)滿足條件時(shí),終止當(dāng)前play的運(yùn)行。可以直接由failed_when取代。

選項(xiàng)只有一個(gè):

msg:終止前打印出信息

示例:

-?fail:?msg="The?system?may?not?be?provisioned?according?to?the?CMDB?status."when:?cmdb_status?!=?"to-be-staged"

六、playbook的roles和include

include

1、包含task文件?

---

# possibly saved as tasks/foo.yml

- name: placeholder foo

??command: /bin/foo

?

- name: placeholder bar

??command: /bin/bar

2、包含handle文件

?

---

?# this might be in a file like handlers/handlers.yml

?- name: restart apache

? service: name=apache state=restarted

?

play末尾包含上面的handler文件:

?handlers:

??- include: handlers/handlers.yml

3、直接包含playbook文件

?

roles(角色)

roles用來組織playbook結(jié)構(gòu),以多層目錄和文件將playbook更好的組織在一起

?角色允許你將變量、文件、任務(wù)、模板、handlers放到一個(gè)文件夾中,然后包含它們。在建立好一個(gè)有效的依賴關(guān)系之后,還可以在一個(gè)角色中包含另外一個(gè)角色。

roles組織的playbook結(jié)構(gòu):

?

site.yml

webservers.yml

fooservers.yml

roles/

?????common/ ???#下面的子目錄都不是必須提供的,沒有的目錄會自動忽略,不會出現(xiàn)問題,所以你可以只有tasks/子目錄也沒問題

?????files/

?????templates/

?????tasks/

?????handlers/

?????vars/

?????meta/

?????webservers/

?

然后在playbook文件中包含commonwebservers這兩個(gè)role

---

- hosts: user_group1

??roles:

?????- common

?????- webservers

轉(zhuǎn)載于:https://www.cnblogs.com/sxchengchen/p/7765921.html

總結(jié)

以上是生活随笔為你收集整理的ansible安装及使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。