ansible模塊使用和深刻解析

今天在學習阿里雲提供的免費ansible視頻課程,特此作個筆記,雖然內容比較基礎,可是挺重要的內容。node

1.ansible 主配置文件

/etc/ansible/ansible.cfgnginx

remote_tmp 遠程臨時目錄,默認爲~/.ansible/tmpshell

local_tmp 本地臨時目錄,默認爲~/.ansible/tmp緩存

forks 併發執行的數量,默認爲5併發

host_key_checking = False 是否驗證對方的公鑰,建議取消註釋app

log_path=/var/log/ansible.log 建議開啓ansible的執行日誌ssh

2.ansible 經常使用命令

ansible --help學習

ansible-docfetch

ansible-doc -l 列出可用模塊ui

ansible-doc ping 查看模塊的文檔

ansible-doc -s ping 大體瞭解模塊的文檔

3.ansible 簡單方式使用

ansible <host-pattern> [-m module_name] [-a args]

  • 查看某個組server

ansible group1 --list

  • 在遠程機器上執行ls

ansible group -u user1 -k -m command -a 'ls /root' -b

4.基於key驗證的方式

ssh-keygen
ssh-copy-id 192.168.30.101

5.ansible 主機模式

ansible al -m ping

ansible group1 -m ping

ansible *roup1 -m ping

ansible 'group1:&group2' -m ping

ansible 'group1:!group2' -m ping

6.ansible執行過程

ansibe all -m ping -vvv 查看執行過程
加載本身的配置文件,例如ansible.cfg
加載本身的模塊文件
生成臨時的py文件,並複製到遠程
給文件加+x權限
執行並返回結果
刪除臨時的py文件,sleep 0 退出

執行狀態:
綠色:執行成功並不須要作改變
黃色:執行成功並作了修改

7.ansible經常使用模塊

command使用,特殊符號不能解析。

ansible all -m command -a 'ifconfig'
ansible all -m command -a 'chdir=/data test.sh'

shell使用,管道和引用變量等均可以解析。

ansible all -m shell -a 'chdir=/data test.sh'

script使用,將ansible主控端的腳本,在目標機執行。

ansible all -m script -a '/root/ansible/host.sh'

copy使用,將主控制機的文件copy到目標機器。

ansible all -m copy -a 'src=/root/ansible/file1 dest=/etc/cc/file1 backup=yes mode=0755'
注意:若是在遠程機器上執行copy,至關於在遠端機器本機執行cp命令,remote_src: true。
對於asible 2.6,只支持copy單個文件,不容許遞歸copy目錄及下面的文件,會拋出以下錯誤。
對於ansible 2.8 已經支持遞歸複製。

TASK [cp files below folder4 to bak1] *************************************************************
ok: [localhost] => (item=subfile1)
ok: [localhost] => (item=subfile2)
failed: [localhost] (item=subfolder1) => {"changed": false, "item": "subfolder1", "msg": "Remote co               py does not support recursive copy of directory: /apps/ansible-test/folder4/subfolder1"}
        to retry, use: --limit @/apps/ansible-test/test-cp.retry

PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=1

fetch使用,客戶端文件抓取到服務端

ansible all -m fetch -a 'src=/var/log/message des=/data/'
ansible all -m shell -a 'tar -zcvf log.tar.gz /var/log/*.log'

file使用,設置文件屬性或建立文件

  • 新建文件

ansible all -m file -a 'name=/data/file1.txt status=touch'

  • 查看文件

ansible all -m shell -a 'ls -l /data'

  • 刪除文件

ansible all -m file -a 'name=/data/file1.txt status=absent'

  • 建立文件夾

ansible all -m file -a 'name=/data/file1 status=directory'

  • 刪除文件夾

ansible all -m file -a 'name=/data/file1 status=absent'

  • 建立軟鏈接

ansible all -m file -a 'src=/etc/fstab dest=/data/fatab.link status=link'

  • 刪除軟連接

ansible all -m file -a 'dest=/data/fatab.link status=absent'

hostname模塊使用

  • 修改hostname

ansible ip1 -m hostname -a 'name=node1'

Cron 計劃任務模塊

ansible all -m cron -a 'minute=* weekday=1,3,5 job="/usr/bin/wall warning" name=warning'

  • 禁用計劃任務

ansible all -m cron -a 'disabled=true job="/usr/bin/wall warning" name=warning'

  • 啓用計劃任務

ansible all -m cron -a 'disabled=false job="/usr/bin/wall warning" name=warning'

  • 刪除計劃任務

ansible all -m cron -a ' job="/usr/bin/wall warning" name=warning status=absent'

yum 模塊的使用

  • 使用yum安裝vsftpd

ansible all -m yum -a 'name=vsftpd,httpd,'

  • 查看已經安裝的包

ansible all -m yum -a 'list=installed'

  • 將ansible core server上的yum包安裝到遠程主機
ansible all -m copy 'src=/data/ssss.rpm dest=/root/'
ansible all -m yum -a 'name=/root/ssss.rpm'
  • 更新yum緩存

ansible all -m yum -a 'name=dstat update_cache=yes'

service 模塊的使用

  • 將服務設置爲開機啓動

ansible all -m service -a 'name=vsftpd state=started enabled=yes'

User 模塊的使用

  • 建立njs帳號

ansible all -m user -a 'name=njs shell=/sbin/nologin system=yes home=/var/njs groups=root,bin uid=8888 comment=nginx'

  • 刪除njs帳號

ansible all -m user -a 'name=njs status=absent'

Group 模塊的使用

  • 建立njs組

ansible all -m group -a 'name=njs system=yes gid=8080'

  • 刪除njs組

ansible all -m group -a 'name=njs status=absend'

相關文章
相關標籤/搜索