Aansible(一)基礎用法

2021-10-09 12:18:56 字數 4875 閱讀 8898

linux免密碼登陸:

ssh-keygen -t rsa

ssh-copy-id -p 34539 [email protected]

新增ansible主機組:

vim /etc/ansible/hosts

[dbservers]

db01.intranet.mydomain.net

db02.intranet.mydomain.net

10.25.1.56:22

10.25.1.57:2222

# 定義多個組,把乙個組當另外乙個組的組員

[webserver:children]

#webserver組包括兩個子組:apache nginx

apache

nginx

檢視ansible所有模組及幫助文件:

ansible-doc ping

檢視簡易幫助

ansible-doc -s ping

預設配置檔案:/etc/ansible/ansible.conf

#inventory = /etc/ansible/hosts 主機列表配置檔案

#library = /usr/share/my_modules/ 庫檔案存放目錄

#module_utils = /usr/share/my_module_utils/

#remote_tmp = ~/.ansible/tmp 臨時py命令檔案存放遠端主機目錄

#local_tmp = ~/.ansible/tmp 本機的命令執行目錄

#plugin_filters_cfg = /etc/ansible/plugin_filters.yml

#forks = 5 併發執行數

#poll_interval = 15

#sudo_user = root 預設sudo使用者

#ask_sudo_pass = true 每次執行ansible命令是否詢問ssh密碼

#ask_pass = true

#transport = smart

#remote_port = 22

#module_lang = c

#module_set_locale = false

#log_path = /var/log/ansible.log 日誌檔案

#host_key_checking = false 檢查對應伺服器的host_key,建議取消

ansible 通過ssh服務實現配置管理、應用部署、任務執行等功能,建議配置ansible端能基於秘鑰認證的方式聯絡各個被管理節點

ansible命令簡單介紹

ansible 

[-m module_name]

[-a arges]

-- version顯示版本

-m module 指定模組,預設command

-v詳細 -vv -vvv

--list_host 顯示主機列表,簡寫--list

-k --ask-pass 提示輸入ssh鏈結密碼,預設key驗證

-k --ask-become-apss 提示輸入sudo時的口令

-c --check 檢查,並不執行

-t --timeout=timeout 執行命令的超時時間 預設10s

-u --user=remote_user 執行遠端執行的使用者

-b --become 代替舊版的sudo切換

host-pattern

1. all

2. 或關係:hostlist:hostlist

3. 邏輯與:'hostlist:&hostlist'

4. 邏輯非:'hostlist:!hostlist'

#此處必須使用單引號

執行狀態:

綠色:執行成功並且不需要做改變的操作

黃色:執行成功並且對目標主機做變更

紅色:執行失敗

[colors]

#highlight = white

#verbose = blue

#warn = bright purple

#error = red

#debug = dark gray

#deprecate = purple

#skip = cyan

#unreachable = red

#ok = green

#changed = yellow

#diff_add = green

#diff_remove = red

#diff_lines = cyan

ansible常用模組:

command:

預設模組,此命令不支援$varname、<

>

|&等,需要使用shell模組

shell:

替代command使用,ansible node -m shell -a 'echo $hostname'
script:

指定指令碼在遠端主機執行,ansible node -m script -a '/test.sh'
copy :

ansible node -m copy -a 'src=/xx/***/file1 dest=/xx/xx/file1 backup=yes mode=000 owner=***'

backup備份原有檔案

mode修改檔案屬性

owner修改檔案屬主

content命令的內容直接在目標主機生成檔案

fetch:

抓取遠端節點的檔案,只支援乙個檔案

ansible node -m fetch -a 'src=/etc/fstab dest=/data'

file:

ansible node -m file -a '[path|dest|name]=/data/xx status=[directory|file|hard|link|touch|absent(default file)] '
hostname:

ansible node -m hostname -a 'name=***x'
crom

建立任務

ansible node -m cron -a 'minute=(0-59, *, */2, etc) hour=( 0-23, *, */2, etc ) day=( 1-31, *, */2, etc ) month=( 1-12, *, */2, etc ) weekday=( 0-6 for sunday-saturday, *, etc ) job=/usr/sbin/ntpdata 10.10.10.10 naem="cron_name"'

刪除、禁用、啟用任務

ansible node -m cron -a 'disabled=[yes|no|true|false] job=/usr/sbin/ntpdata 10.10.10.10 naem="cron_name"'

yum管理包

ansible node -m yum -a 'name=software1,software2 status=[install (`present' or `

installed', `latest'), or remove (

`absent' or `removed'

(default installed)

]安裝本地rpm

ansible node -m yum -a 'name=/data/***.rpm disable_gpg_check=yes'

更新yum快取

ansible node -m yum -a 'name=*** update_cache=yes'

service管理服務

ansible node -m service -a 'name=vsftpd status=[started|stoped|restarted|reloaded] enabled=[true|false]'
user

建立賬號

ansible node -m user -a 'name=nginx shell=/sbin/nologin system=yes home/xx/xx/ group=root,xx'

刪除賬號

ansible node -m user -a 'name=nginx state=absent remove=yes'

group

建立組

ansible node -m user -a 'name=nginx system=yes gid=80'

刪除組ansible node -m user -a 'name=nginx status=absent'

Git的基礎用法 一)

版本控制是一種記錄乙個或若干個檔案內容變化,以便將來查閱特定版本修訂情況的系統。在軟體開發中我們需要對儲存著軟體源 的文字檔案作版本控制管理。版本控制經歷了本地版本控制,到集中版本控制,發展到現在的分布式版本管理系統.本地版本控制早已過時,幾乎很少用到,集中版本控制系統典型的如 subversion...

js fetch基礎用法學習(一)

fetch 傳遞引數的方式 使用 fetch mdn 第乙個引數是 url 位址 第二個引數是請求的配置資訊,包含headers 請求型別 get post 是否跨域等資訊 fetch url,method post get,post,put,delete,etc.mode cors no cors...

mysql 的用法 Mysql一些基礎用法

mysql建檢視 create view view name as select from table1 alter view view name as select from table1 建觸發器 create trigger trig name before insert on table n...