<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ansible常用命令使用

          共 7511字,需瀏覽 16分鐘

           ·

          2021-03-21 15:34

          ansible命令格式

          # 查看ansible幫助

          ansible -h

          # 命令格式

          Usage: ansible <host-pattern> [options]

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

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

          -f FORKS, --forks=FORKS # 指定并發(fā),默認(rèn)5個(gè)

          --list-hosts # 列出主機(jī)

          -m MODULE_NAME # 模塊名稱

          --syntax-check # 檢查語法

          -k # 密碼

          ansible鏈接機(jī)器

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

          /etc/ansible

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

          /etc/ansible/hosts # 配置管控機(jī)

          /etc/ansible/roles # 空文件夾

          # 在hosts文件中將主機(jī)地址一個(gè)個(gè)添加進(jìn)去

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

          # 生成密鑰

          ssh-keygen

          # 將公鑰拷貝至目標(biāo)主機(jī)

          ssh-copy-id root@主機(jī)地址

          # 通過ssh即可鏈接目標(biāo)主機(jī)

          ssh 'root@主機(jī)地址'

          exit退出

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

          ansible 主機(jī)地址 -m ping

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

          ansible 192.168.226.101 -m ping # 單獨(dú)機(jī)器的ping

          ansible 192.168.226.101,192.168.226.102 -m ping # 多個(gè)機(jī)器的ping

          ansible all -m ping # 全部機(jī)器

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

          ansible web -m ping # 單個(gè)的組

          ansible web,db -m ping # 多個(gè)組的并集

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

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

          ansible模塊介紹

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

          ansible-doc 命令格式

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

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

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

          # 常用命令

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

          模塊介紹

          command

          分組機(jī)器批量執(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" #因?yàn)閠mp目錄存在,pwd不會執(zhí)行

          ansible web -m command -a "creates=/tmp2 pwd" #因?yàn)閠mp2不存在,pwd執(zhí)行

          ansible web -m command -a "removes=/tmp2 pwd" #因?yàn)閠mp2不存在pwd不執(zhí)行

          ansible web -m command -a "removes=/tmp pwd" #因?yàn)閠mp目錄存在,pwd會執(zhí)行

          # 創(chuàng)建用戶

          ansible web -m command -a 'useradd jason'

          # 設(shè)置密碼(本機(jī)設(shè)置) 并需二次確認(rèn)

          passwd jason

          # 設(shè)置密碼(本機(jī)設(shè)置) 無需二次確認(rèn)

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

          shell

          當(dāng)命令中包含$home和常見符號(<,>,|,;.&)等的時(shí)候,需要使用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

          在遠(yuǎn)程機(jī)器上執(zhí)行本地腳本

          ansible-doc -s script

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

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

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

          copy

          將本地的文件拷貝到遠(yuǎn)程機(jī)器

          ansible-doc -s copy

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

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

          dest # 目標(biāo)地址

          group # 屬組

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

          owner # 屬主

          src # 源地址

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

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

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

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

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

          ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私語,嘈嘈切切錯(cuò) 雜彈,大珠小珠落玉盤' 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ū)

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

          fetch

          拉取遠(yuǎn)程主機(jī)文件

          dest 目標(biāo)地址

          src 源地址

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

          yum模塊

          ansible-doc -s yum

          disablerepo # 禁用某個(gè)源

          enablerepo # 啟用某個(gè)源

          name # 包名

          state

          install

          remove

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

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

          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" # 安裝單個(gè)軟件包

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

          rpm -q 包名

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

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

          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)境導(dǎo)出到某個(gè)文件中,文件中不寫版本信息默認(rè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 # 導(dǎo)出的文件

          virtualenv # 虛擬環(huán)境

          # 安裝django(時(shí)間較長~)

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

          service模塊

          ansible-doc -s service

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

          enabled #開機(jī)啟動

          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è)置開機(jī)自啟動

          cron計(jì)劃任務(wù)模塊

          # 格式

          * * * * * job

          分 時(shí) 日 月 周 任務(wù)

          ansible-doc -s cron

          day # 天

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

          hour # 小時(shí)

          job # 任務(wù)

          minute # 分鐘

          month # 月

          name # 名字,描述信息

          user # 用戶

          weekday # 周

          # 添加時(shí)名字必須不同,不加名稱為None(crontab -l查看計(jì)劃任務(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" # 刪除名稱為空的計(jì)劃任務(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模塊

          收集遠(yuǎn)程主機(jī)的一些基本信息

          # 查看詳細(xì)信息

          ansible 主機(jī)名 -m setup|more

          ansible_all_ipv4_addresses # 所有的ipv4地址

          ansible_all_ipv6_addresses # 所有的ipv6地址

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

          ansible_date_time # 系統(tǒng)時(shí)間

          ansible_default_ipv4 # 默認(rèn)的ipv4地址

          address # ip地址

          alias # 網(wǎng)卡名稱

          broadcast # 廣播地址

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

          netmask # 子網(wǎng)掩碼

          network # 網(wǎng)段

          ansible_default_ipv6 # 默認(rèn)的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 默認(rèn)udp 端口53

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

          ipv4 # ipv4地址

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

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

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

          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的個(gè)數(shù)=cpu的顆數(shù)*每顆cpu的核數(shù)

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

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

          ansible_system # 系統(tǒng)名字


          瀏覽 64
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  九一无码| 亚洲天堂AV网站 | 日本女人性高潮视频 | 天天狠狠干 | 欧洲成人性爱视频 |