今天在學習阿里雲提供的免費ansible視頻課程,特此作個筆記,雖然內容比較基礎,可是挺重要的內容。node
/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
ansible --help學習
ansible-docfetch
ansible-doc -l 列出可用模塊ui
ansible-doc ping 查看模塊的文檔
ansible-doc -s ping 大體瞭解模塊的文檔
ansible <host-pattern> [-m module_name] [-a args]
ansible group1 --list
ansible group -u user1 -k -m command -a 'ls /root' -b
ssh-keygen
ssh-copy-id 192.168.30.101
ansible al -m ping ansible group1 -m ping ansible *roup1 -m ping ansible 'group1:&group2' -m ping ansible 'group1:!group2' -m ping
ansibe all -m ping -vvv 查看執行過程
加載本身的配置文件,例如ansible.cfg
加載本身的模塊文件
生成臨時的py文件,並複製到遠程
給文件加+x權限
執行並返回結果
刪除臨時的py文件,sleep 0 退出
執行狀態:
綠色:執行成功並不須要作改變
黃色:執行成功並作了修改
ansible all -m command -a 'ifconfig'
ansible all -m command -a 'chdir=/data test.sh'
ansible all -m shell -a 'chdir=/data test.sh'
ansible all -m script -a '/root/ansible/host.sh'
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
ansible all -m fetch -a 'src=/var/log/message des=/data/'
ansible all -m shell -a 'tar -zcvf log.tar.gz /var/log/*.log'
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'
ansible ip1 -m hostname -a 'name=node1'
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'
ansible all -m yum -a 'name=vsftpd,httpd,'
ansible all -m yum -a 'list=installed'
ansible all -m copy 'src=/data/ssss.rpm dest=/root/' ansible all -m yum -a 'name=/root/ssss.rpm'
ansible all -m yum -a 'name=dstat update_cache=yes'
ansible all -m service -a 'name=vsftpd state=started enabled=yes'
ansible all -m user -a 'name=njs shell=/sbin/nologin system=yes home=/var/njs groups=root,bin uid=8888 comment=nginx'
ansible all -m user -a 'name=njs status=absent'
ansible all -m group -a 'name=njs system=yes gid=8080'
ansible all -m group -a 'name=njs status=absend'