ansible 劇本高階 角色

2022-07-31 20:42:13 字數 4255 閱讀 3652

ansible cache -m setup

ansible_all_ipv4_addresses # ipv4的所有位址

ansible_all_ipv6_addresses # ipv6的所有位址

ansible_date_time # 獲取到控制節點時間

ansible_default_ipv4 # 預設的ipv4位址

ansible_distribution # 系統

ansible_distribution_major_version # 系統的大版本

ansible_distribution_version # 系統的版本號

ansible_domain #系統所在的域

ansible_env #系統的環境變數

ansible_hostname #系統的主機名

ansible_fqdn #系統的全名

ansible_machine #系統的架構

ansible_memory_mb #系統的記憶體資訊

ansible_os_family # 系統的家族

ansible_pkg_mgr # 系統的包管理工具

ansible_processor_cores #系統的cpu的核數(每顆)

ansible_processor_count #系統cpu的顆數

ansible_processor_vcpus #系統cpu的總個數=cpu的顆數*cpu的核數

ansible_python # 系統上的python

ansible cache -m setup -a '

filter=*processor*

' # 用來搜尋

補充內容(正規表示式)

*匹配數量,表示0或者多次

?匹配數量,表示0或者1次

. 除換行符以外的所有字元

+至少一次

[123abc] 匹配內容,or

() 分組

次數,出現m次

至少m次

出現m-n次

a*.b

-hosts: db

remote_user: root

tasks:

-name: createfile

copy: content="

大弦嘈嘈如急雨

" dest=/tmp/a.txt

when: a=="3"

-name: cratefile

copy: content="

小弦切切如私語

" dest=/tmp/a.txt

when: a=="

4"

擴充套件:ubuntu 安裝包的方式是apt-get

-hosts: db

tasks:

-name: wadong

tieqiao: wadong

-name: tk

dong: tk

tags: tk

-hosts: web

tasks:

-name: installnginx

yum: name=nginx

-name: copyfile

copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf

tags: copyfile

-name: start

service: name=nginx state=started

ansible-playbook -t copyfile p7.yml

一次性建立多個

-hosts: gb

tasks:

-name: gbqc

dong: }

with_items:

-qbqc

-cyf

-ssfj

-hosts: web

tasks:

-name: crateuser

user: name=}

with_items:

-alex20

-alex21

-alex22

~

-hosts: web

tasks:

-name: crateuser

user: name=}

with_items:

-alex30

-alex31

-alex32

-name: crategroup

group: name=}

with_items:

-wulaoshi20

-wulaoshi21

-wulaoshi22

~

-hosts: web

tasks:

-name: crategroup

group: name=}

with_items:

-wulaoshi30

-wulaoshi31

-wulaoshi32

-name: createuser

user: name=} group=}

with_items:

- -

-

template:

jinja2

redis安裝

-hosts: web

tasks:

-name: installredis

yum: name=redis

-name: copyfile

template: src=/etc/redis.conf dest=/etc/redis.conf

-name: start

service: name=redis state=started

配置檔案: bind }

copy和tamplate的區別

-hosts: web

tasks:

-name: installredis

yum: name=redis

-name: copyfile

template: src=redis.conf dest=/etc/redis.conf

-name: start

service: name=redis state=started

ps:寫相對路徑: 在當前目錄下新建乙個templates目錄,然後把檔案放在templates目錄裡面

-hosts: web

tasks:

-name: installredis

yum: name=redis

-name: copyfile

template: src=redis.conf dest=/etc/redis.conf

tags: copyfile

notify: restart

-name: start

service: name=redis state=started

handlers:

-name: restart

service: name=redis state=restarted

回顧 playbook

傳參條件判斷 when

迴圈 with_items item

巢狀迴圈 字典 通過點來取值

標籤 tags -t 來傳遞標籤

模板 template

handlers 不會執行, notify

roles資料夾

資料夾裡面是要建立的每乙個角色,每乙個角色乙個資料夾

每乙個角色裡面都有tasks(必須的),templates,files,handlers,vars目錄

每個目錄都要有main.yml檔案,通過import_tasks來呼叫

其中templates資料夾中的檔案可以通過相對路徑來呼叫

其中files資料夾中的檔案是否可以通過相對路徑來呼叫?

mysql my

mariadb

hadoop 大資料

setenforce 0 #用來臨時關閉selinux

iptables -f # 臨時關閉防火牆

/etc/selinux/config  # 永久關閉

用roles 來安裝nginx+uwsgi+mariadb + redis

ansible劇本編寫簡介

一 劇本檔案組成部分 劇本的角色 play 定義的是主機資訊 劇本的任務 task 定義的是具體任務 playbook組成部分 1 乙個playbook有多個play組成 2 乙個play可以包含多個task任務 3 簡單理解為使用多個模組功能完成一件事 二 劇本檔案優勢特點 1 實現自動化功能更加...

ansible程式roles角色

劇本編寫完問題 1.目錄結構不夠規範 ok 2.編寫好的任務如何重複呼叫 3.服務端配置檔案改動,客戶端引數資訊也自動變化 4.彙總劇本中沒有顯示主機角色資訊 5.乙個劇本內容資訊過多,不容易進行閱讀,如何進行拆分 ok 第乙個歷程 規範目錄結構 cd etc ansible roles mkdir...

ansible 角色登陸

用ansible 來管理遠端的主機,最大的好處是方便,ansible不用在遠端的主機上安裝ansible的客戶端,ansible只要能通過ssh連線上遠端主機就 能對它進行管理。也就是說ansible是通過ssh來連線遠端主機的,於是就引出了連線到遠端主機的兩種認證方式,一種是使用者名稱 密碼,另一...