ansible使用臨時命令通過模塊來執行任務

使用臨時命令通過模塊來執行任務一、查看系統上安裝的所有模塊ansible-doc -l
查看ping模塊幫助文檔ansible-doc ping
1、ansible模塊文件模塊:copy:將本地文件復制到受控主機file:設置文件的權限和其他屬性lineinfile:確保特定行是否在文件中,也就是說修改文件內容synchronize:使用rsync同步內容
軟件包模塊package:使用操作系統本機的自動檢測軟件包管理器管理軟件包yum:使用yum軟件包管理器管理軟件包apt:使用apt軟件包管理器管理軟件包dnf:使用dnf軟件包管理器管理軟件包pip:從PyPI管理Python軟件包
系統模塊firewalld:使用firewalld管理任意端口和服務reboot:重新啟動計算機service:管理服務user:添加、刪除和管理用戶賬戶
Net Tools模塊get_url:通過http、https或者ftp下載文件nmcli:管理網絡uri:與WEB服務交互
語法:ansible  bgx      -m      command      -a      'df -h'命令   主機名稱  指定模塊   模塊名稱   模塊動作   具體命令
執行的狀態返回信息:綠色:執行成功并且不需要做改變的動作黃色:執行成功并且對目標主機做變更紅色:執行失敗
常用模塊案例1:user

ansible使用臨時命令通過模塊來執行任務

文章插圖
臨時命令使用user模塊來確保newbie用戶存在于node1.example.com上,并且其UID為4000
【ansible使用臨時命令通過模塊來執行任務】[galaxy@server ~]$ ansible server1 -m user -a 'name=newbie uid=4000 state=present'創建用戶并指定密碼,如果該用戶存在 , 仍然修改密碼
[galaxy@server ~]$ openssl passwd -1 linux$1$bChlQ4jX$97x50MlATs0PA6UsObqN1.[galaxy@server ~]$ ansible all -m user -a 'name=chenyu state=present password="$1$bChlQ4jX$97x50MlATs0PA6UsObqN1." update_password=always'創建用戶并指定密碼,但是如果改用戶存在,則不修改密碼
[galaxy@server ~]$ openssl passwd -1 redhat$1$zcVeWQiB$dIsAdkcv91mTjrCaayN3F/[galaxy@server ~]$ ansible all -m user -a 'name=chenyu12 state=present password="$1$zcVeWQiB$dIsAdkcv91mTjrCaayN3F/"  update_password=on_create'案例2:shell臨時命令使用shell模塊來刪除node1.example.com節點中的用戶newbieansible server1 -m shell -a ‘userdel -r newbie’
案例3:copyansible webserver -m copy -a ‘src=https://www.huyubaike.com/etc/fstab dest=/var/tmp/fstab’
ansible webserver -m copy -a ‘src=https://www.huyubaike.com/etc/fstab dest=/var/tmp/fstab group=chenyu owner=chenyu’
ansible使用臨時命令通過模塊來執行任務

文章插圖
案例4:template模塊---template模塊用法和copy模塊用法基本一致,它主要用于復制配置文件
ansible使用臨時命令通過模塊來執行任務

文章插圖
ansible all -m template -a 'src=https://www.huyubaike.com/usr/share/doc/httpd/httpd-vhosts.conf dest=/etc/httpd/conf.d/httpd-vhosts.conf group=root owner=root mode=0644 '
案例5:file   修改文件的權限屬性和context值ansible webserver -m file -a 'path=/var/tmp/fstab mode=g+w mode=o+w group=galaxy owner=galaxy setype=samba_share_t'
mode:設置權限可以是mode=g+w 也可以是mode=666group:設置文件的所屬組owner:設置文件的所有者setype:修改文件的context值
ansible使用臨時命令通過模塊來執行任務

文章插圖
新建文件ansible webserver -m file -a 'path=/var/tmp/bbb state=touch'
新建目錄ansible webserver -m file -a 'path=/var/tmp/cc state=directory'
刪除文件或者目錄ansible webserver -m file -a 'path=/var/tmp/cc state=absent'
創建軟鏈接ansible webserver -m file -a 'dest=/var/tmp/chenyu src=https://www.huyubaike.com/var/tmp/bbb state=link'
創建硬鏈接ansible webserver -m file -a 'dest=/var/tmp/chenyu1 src=https://www.huyubaike.com/var/tmp/aaa state=hard'
案例6:lineinfile把abc開頭的一行換成 bbbbbansible webserver -m lineinfile -a 'dest=/tmp/cy regexp=abc line=bbbbb'
在某一行前面插入一行新數據---insertbeforeansible webserver -m lineinfile -a 'dest=/tmp/cy insertbefore="aa(.*)" line=chenyu'
在某一行后面插入一行新數據---insertafteransible webserver -m lineinfile -a 'dest=/tmp/cy insertafter="aaaa(.*)" line=bbbb'
刪除某一行ansible webserver -m lineinfile -a 'dest=/tmp/cy regexp="aaa(.*)" state=absent'
案例7:yum_repository模塊-----配置yum倉庫ansible webserver -m yum_repository -a 'file=server name=baseos description=rhel8 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no'

推薦閱讀