溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ansible常用命令使用

發(fā)布時間:2020-07-06 17:26:22 來源:網(wǎng)絡(luò) 閱讀:457 作者:simon_ymc 欄目:系統(tǒng)運維
  1. ansible命令格式

# 查看ansible幫助

ansible -h

# 命令格式

Usage: ansible <host-pattern> [options]


-a MODULE_ARGS ?# 模塊參數(shù)

-C, --check ?# 干跑,白跑

-f FORKS, --forks=FORKS ?# 指定并發(fā),默認5個

--list-hosts ?# 列出主機

-m MODULE_NAME ?# 模塊名稱

--syntax-check ?# 檢查語法

-k ?# 密碼


2.ansible鏈接機器

rpm -ql ansible|more # 查看ansible生成的文件


/etc/ansible

/etc/ansible/ansible.cfg ?# 配置文件

/etc/ansible/hosts ?# 配置管控機

/etc/ansible/roles ?# 空文件夾

# 在hosts文件中將主機地址一個個添加進去


ansible 主機地址 -m ping -k ?# ansible底層通過ssh實現(xiàn)


# 生成密鑰

ssh-keygen

# 將公鑰拷貝至目標主機

ssh-copy-id root@主機地址

# 通過ssh即可鏈接目標主機

ssh 'root@主機地址'

exit退出


# 完成上面的操作后 再去探測機器是否在線就不要再加-k

ansible 主機地址 -m ping


"""測試機器是否在線的方式及分組管理"""

ansible 192.168.226.101 -m ping ?# 單獨機器的ping

ansible 192.168.226.101,192.168.226.102 -m ping ?# 多個機器的ping

ansible all -m ping ?# 全部機器

# 分組:比如有N多臺機器,有些是負責數(shù)據(jù)庫的,有些是uwsgi的,有些是redis的等

ansible web -m ping ?# 單個的組

ansible web,db -m ping ?# 多個組的并集

ansible 'web:&db' -m ping ?# 多個組的交集 ?必須是單引號 雙引號不行

ansible 'web:!db' -m ping ?# 多個組的差集,在前面但是不在后面


3.ansible模塊介紹

ansible-doc -l |wc -l ?# 查看ansible總模塊數(shù)


4.ansible-doc 命令格式

-j ?# 以json的方式返回數(shù)據(jù)

-l, --list ?# 列出所有的模塊

-s, --snippet # 以片段式顯示模塊信息?

# 常用命令

ansible-doc -s 模塊名 ?# 查看模塊幫助信息


模塊介紹

  • command?

分組機器批量執(zhí)行系統(tǒng)命令


ansible-doc -s command


ansible web -m command -a "pwd"

ansible web -m command -a "ls"

ansible web -m command -a "chdir=/tmp pwd" #切換目錄并執(zhí)行命令

ansible web -m command -a "creates=/tmp pwd" #因為tmp目錄存在,pwd不會執(zhí)行

ansible web -m command -a "creates=/tmp2 pwd" #因為tmp2不存在,pwd執(zhí)行

ansible web -m command -a "removes=/tmp2 pwd" #因為tmp2不存在pwd不執(zhí)行

ansible web -m command -a "removes=/tmp pwd" #因為tmp目錄存在,pwd會執(zhí)行


# 創(chuàng)建用戶

ansible web -m command -a 'useradd jason'

# 設(shè)置密碼(本機設(shè)置) ?并需二次確認

passwd jason

# 設(shè)置密碼(本機設(shè)置) ?無需二次確認

echo "123" |passwd --stdin jason #設(shè)置用戶的密碼



  • shell

當命令中包含$home和常見符號(<,>,|,;.&)等的時候,需要使用shell才能生效


ansible-doc -s shell


ansible web -m shell -a "echo '123' |passwd --stdin jason"

ansible web -m shell -a "chdir=/tmp pwd" shabang

ansible 192.168.226.101 -m shell -a "bash a.sh" #執(zhí)行shell腳本

ansible 192.168.226.101 -m shell -a "/root/a.sh" # 執(zhí)行shell腳本,文件要有執(zhí)行的權(quán)限

ansible 192.168.226.101 -m shell -a "/root/a.py" #執(zhí)行Python文件


  • script

在遠程機器上執(zhí)行本地腳本

ansible-doc -s script

ansible db -m script -a "/root/a.sh" ?# 執(zhí)行本地的文件,管控機的文件

ansible db -m script -a "creates=/root/a.sh /root/a.sh" ?# 判斷被控機上的文件是否存在,如果不存在,就執(zhí)行,如果存在,就跳過

ansible db -m script -a "creates=/tmp /root/a.sh" ?# 判斷被控機上的文件


  • copy

將本地的文件拷貝到遠程機器

ansible-doc -s copy


backup ?# 創(chuàng)建一個備份文件,以時間戳結(jié)尾

content ?# 直接往文件里面寫內(nèi)容

dest ?# 目標地址

group ?# 屬組

mode ?# 文件的權(quán)限 W 2 R 4 X 1

owner ?# 屬主

src ?# 源地址

ansible web -m copy -a "src=/etc/fstab dest=/tmp/f" ?# 復制本地文件到遠程主機,并修改文件名,多次執(zhí)行不會改變,因為checksum值是一樣的

ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes" ?# 復制本地文件,并備份

ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes group=alex mode=755" ?# 復制本地文件到遠程主機,并指定屬組和權(quán)限

ansible web -m copy -a "src=/etc/init.d dest=/tmp backup=yes group=alex mode=755" ?# 復制本地的目錄到遠程主機,修改目錄權(quán)限,則目錄里面的文件也會跟著變更

ansible web -m copy -a "src=/etc/init.d/ dest=/tmp backup=yes group=alex mode=755" ?#復制本地目錄下的所有文件,

ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私語,嘈嘈切切錯 雜彈,大珠小珠落玉盤' dest=/tmp/b" ?# 直接往文件里面寫內(nèi)容,覆蓋寫,慎用


  • File

文件屬組、權(quán)限、硬軟鏈等


group ?# 屬組

mode ?# 權(quán)限

owner ?# 屬主

path ?# 路徑

src ?# 軟硬鏈接

state =link

state =hard

state

directory ?# 目錄

file ?

touch ?# 空文件

absent ?# 刪除

link ?# 軟連接

hard ?# 硬鏈接

??

ansible web -m file -a "path=/jason5 state=directory owner=jason" #創(chuàng)建目錄,并制定屬主

ansible web -m file -a "path=/tmp/jason.txt state=touch mode=777" #創(chuàng)建文件,并指定權(quán)限

ansible web -m file -a "path=/tmp/cron src=/var/log/cron state=link" #創(chuàng)建軟鏈接,鏈接的是自己的文件

ansible web -m file -a "path=/tmp/cron state=absent" # 刪除軟連接

ansible web -m file -a "path=/jason5 state=absent" #刪除文件夾



軟連接 ? 快捷方式(windows) ? ln -s ? 源文件修改軟連接修改 ? ? 源文件刪除軟連接失效 ? ?可以跨分區(qū)?

硬鏈接 ? 硬盤的位置 ln ? ? ? 源文件修改硬鏈接修改 ?? ? ? 源文件刪除硬鏈接不變 ? ?不可以跨分區(qū)

復制 ? ? 開辟新空間 cp ? ? ? 源文件修改cp的不變 ? ? ? ? 源文件刪除不變 ? ? ? 可以跨分區(qū)


  • fetch

拉取遠程主機文件

dest 目標地址

src 源地址

ansible web -m fetch -a "src=/var/log/cron dest=/tmp" ?# 拉取遠程主機的文件,并以主機ip地址或者主機名為目錄,并且保留了原來的目錄結(jié)構(gòu)


  • yum模塊

ansible-doc -s yum


disablerepo ?# 禁用某個源

enablerepo ?# 啟用某個源

name ?# 包名

state

install

remove

yum install -y python2-pip ?# 在epel源中

# 將主機中下載好的pip拷貝到其他被管控機上

ansible all -m copy -a 'src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo'


ansible web -m yum -a "name=python2-pip" ?# 安裝單個軟件包


# 查看是否安裝 對應機器上執(zhí)行下述命令

rpm -q 包名

# eg:rpm -q pip/python-pip/python2-pip/redis


ansible web -m yum -a "name=python2-pip,redis" ?# 安裝多個軟件包

ansible web -m yum -a "name='@Development Tools'" ?# 安裝包組

ansible web -m yum -a "name=nginx state=absent" ?# 卸載


  • pip模塊

python安裝第三方模塊的工具


pip freeze > requirements.txt ? # 將本地環(huán)境導出到某個文件中,文件中不寫版本信息默認安裝最新版本

pip install -r requirements.txt ?# 安裝所有的包(******)


pip list ?# 查看所有的包

pip uninstall flask ?# 卸載


# 下載包

Python setup.py build?

python setup.py install


  • pip模塊

ansible-doc -s pip


chdir ?# 切換目錄

name ?# 包名

requirements ?# 導出的文件

virtualenv ?# 虛擬環(huán)境


# 安裝django(時間較長~)

ansible web -m pip -a "name=django==1.11"


  • service模塊

ansible-doc -s service

# 關(guān)鍵參數(shù)

enabled #開機啟動

name #服務(wù)名稱

state ?

? ? started

? ? stopped

? ? restarted

? ? reloaded

ansible web -m service -a "name=redis state=started" #啟動

ansible web -m service -a "name=redis state=stopped" #關(guān)閉

ansible web -m service -a "name=redis enabled=yes" #設(shè)置開機自啟動


  • cron計劃任務(wù)模塊

# 格式

* * * * * job

分 時 日 月 周 任務(wù)


ansible-doc -s cron


day ?# 天

disabled ?# 禁用crontab,表現(xiàn)形式加#

hour ?# 小時

job ?# 任務(wù)

minute ?# 分鐘

month ?# 月

name ?# 名字,描述信息

user ?# 用戶

weekday ?# 周


# 添加時名字必須不同,不加名稱為None(crontab -l查看計劃任務(wù))

ansible web -m cron -a "minute=12 name=touchfile job='touch /tmp/xiaoqiang.txt'" ?# 創(chuàng)建

ansible web -m cron -a "name=touchfile state=absent" ?# 刪除

ansible web -m cron -a "minute=12 name=touchfile2 job='touch /tmp/jason.txt' disabled=yes" ?# 注釋

ansible web -m cron -a "name=None state=absent" ?# 刪除名稱為空的計劃任務(wù)


  • user模塊

ansible-doc -s user


group # 屬組

groups ?# 附加組

home ?# 設(shè)置家目錄

name ?# 用戶名

remove ?# 刪除用戶并刪除用戶的家目錄

shell ?# 用戶登錄后的shell

system ?# 系統(tǒng)用戶

uid ?# 用戶的id

ansible web -m user -a "name=alex10 shell=/sbin/nologin home=/opt/alex10 uid=3000 groups=root" #創(chuàng)建用戶,并指定用戶的shell,家目錄,uid,以及附加組

ansible web -m user -a "name=alex11 shell=/sbin/nologin home=/opt/alex11"

ansible web -m user -a "name=alex12 system=yes" #創(chuàng)建系統(tǒng)用戶

ansible web -m user -a "name=alex12 state=absent" #刪除用戶,單不刪除家目錄

ansible web -m user -a "name=alex11 state=absent remove=yes" # 刪除用戶并刪除用戶的家目錄


  • group模塊

ansible-doc -s group


gid ?# 組id

system ?# 系統(tǒng)組

name ?# 名稱

ansible web -m group -a "name=jason10 system=yes gid=5000" ?# 創(chuàng)建系統(tǒng)組

ansible web -m group -a "name=jason11" ?# 創(chuàng)建普通的組

ansible web -m group -a "name=jason11 state=absent" ?# 刪除組


  • setup模塊

收集遠程主機的一些基本信息

# 查看詳細信息

ansible 主機名 -m setup|more


ansible_all_ipv4_addresses ?# 所有的ipv4地址

ansible_all_ipv6_addresses ?# 所有的ipv6地址

ansible_architecture ?# 系統(tǒng)的架構(gòu)

ansible_date_time ?# 系統(tǒng)時間

ansible_default_ipv4 ?# 默認的ipv4地址

address ?# ip地址

alias ?# 網(wǎng)卡名稱

broadcast ?# 廣播地址

gateway ?# 網(wǎng)關(guān)

netmask ?# 子網(wǎng)掩碼

network ?# 網(wǎng)段

ansible_default_ipv6 ?# 默認的ipv6地址

ansible_device_links ?# 系統(tǒng)的磁盤信息

ansible_distribution ?# 系統(tǒng)名稱

ansible_distribution_file_variety ?# 系統(tǒng)的基于公司

ansible_distribution_major_version ?# 系統(tǒng)的主版本

ansible_distribution_version ?# 系統(tǒng)的全部版本

ansible_dns ?# 系統(tǒng)的dns 默認udp 端口53

ansible_domain ?# 系統(tǒng)的域 ldap

ipv4 ?# ipv4地址

ansible_env ?# 系統(tǒng)的環(huán)境

ansible_fqdn ?# 系統(tǒng)的完整主機名

ansible_hostname ?# 系統(tǒng)的簡寫主機名

ansible_kernel ?# 系統(tǒng)的內(nèi)核版本

ansible_machine ?# 系統(tǒng)的架構(gòu)

ansible_memtotal_mb ?# 系統(tǒng)的內(nèi)存

ansible_memory_mb ?# 系統(tǒng)的內(nèi)存使用情況

ansible_mounts ?# 系統(tǒng)的掛載信息

ansible_os_family ?# 系統(tǒng)家族

ansible_pkg_mgr ?# 系統(tǒng)的包管理工具

ansible_processor ?# 系統(tǒng)的cpu

ansible_processor_cores ?# 每顆cpu的核數(shù)

ansible_processor_count ?# cpu的顆數(shù)

ansible_processor_vcpus ?# cpu的個數(shù)=cpu的顆數(shù)*每顆cpu的核數(shù)

ansible_python ?# 系統(tǒng)python信息

ansible_python_version ?# 系統(tǒng)python的版本

ansible_system ?# 系統(tǒng)名字


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI