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


ansible webserver -m yum_repository -a 'file=server name=appstream description=RHEL8 baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no'

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

文章插圖
案例8:yum模塊----yum安裝與卸載state:present、installed、latest安裝absent、removed卸載ansible all -m yum -a 'name=httpd state=installed'        ----------------安裝
ansible all -m yum -a 'name=httpd state=removed'----------------卸載
ansible使用臨時命令通過模塊來執行任務

文章插圖
案例9:service模塊
ansible使用臨時命令通過模塊來執行任務

文章插圖
重啟httpd服務并設置下次啟動生效ansible all -m service -a 'name=httpd state=started enabled=yes'
案例10:fetch—拉取文件模塊和copy工作方式類似,只不過是從遠程主機將文件拉取到本地端,存儲時使用主機名作為目錄樹,且只能拉取文件,不能拉取目錄
ansible使用臨時命令通過模塊來執行任務

文章插圖
將遠程主機的/etc/fstab文件拉取到本地來 , 存儲的名字為/tmp/node1(node2)/etc/fstabansible all -m fetch -a 'src=https://www.huyubaike.com/etc/fstab dest=/tmp'
將某臺遠程主機的/etc/fstab文件拉取到本地來,存儲的名字為/tmp/fstabansible node1 -m fetch -a 'src=https://www.huyubaike.com/etc/fstab dest=/tmp/ flat=yes'
將遠程主機的/etc/fstab文件拉取到本地來,存儲的名字為/tmp/fstab-node1(node2)ansible all -m fetch -a 'src=https://www.huyubaike.com/etc/fstab dest=/tmp/fstab-{{inventory_hostname}} flat=yes'
案例11:firewalld模塊允許http流量的傳入ansible all -m firewalld -a 'service=http   permanent=yes state=enabled immediate=yes'
富規則 允許172.16.30.0/24主機http流量的傳入ansible all -m firewalld -a ‘zone=public rich_rule="rule family=ipv4 source address=172.16.30.0/24 service name=http accept" permanent=yes state=enabled immediate=yes'
案例12:replace模塊replace模塊可以根據我們指定的正則表達式替換文件中的字符串,文件中所有被匹配的字符串都會被替換參數:path參數:2.3版本之前只能用dest、destfile、name指定操作文件,2.4版本中仍然可以用這些參數名,也可以用pathregexp參數:必須參數 , 指定一個python正則表達式,文件中與正則匹配的字符串將會被替換replace參數:指定最終要替換成的字符串backup參數:是否在修改文件之前對文件進行備份,最好設置為yes 。
將/tmp/cy文件中的“abc”替換成“yyy”ansible all -m replace -a 'path=/tmp/cy regexp="abc" replace="yyy"'
將/tmp/cy文件中的“yyy”替換成“iii”,且把替換前的/tmp/cy文件備份ansible all -m replace -a 'path=/tmp/cy regexp="yyy" replace="iii" backup=yes'
案例13:parted模塊新建擴展分區ansible node1 -m parted -a 'device=/dev/sda number=4 part_type=extended part_start=46GiB part_end=49.8GiB state=present'
新建邏輯分區ansible node1 -m parted -a 'device=/dev/sda number=5 part_type=logical part_start=46.1GiB part_end=48.2GiB state=present'
案例14:filesystem—文件系統ansible node1 -m filesystem -a 'fstype=xfs dev=/dev/sda5'
案例15:mount---掛載新建掛載點/commonansible node1 -m file -a 'path=/common state=directory'
查看/dev/sda5的UUIDansible node1 -m shell -a 'blkid /dev/sda5'
將分區/dev/sda5掛載到/common目錄ansible node1 -m mount -a 'path=/common src=https://www.huyubaike.com/biancheng/"UUID=d162b8b9-2326-4ee4-a559-80861461c4f0" fstype=xfs state=mounted'
卸載ansible node1 -m mount -a 'path=/common src=https://www.huyubaike.com/biancheng/"UUID=d162b8b9-2326-4ee4-a559-80861461c4f0" fstype=xfs state=absent'
案例16:lvg—新建卷組ansible node1 -m lvg -a 'vg=vg0 pesize=16M pvs=/dev/sda5'
案例17:lvol—新建邏輯卷ansible node1 -m lvol -a 'lv=lv0 size=1000M vg=vg0'在線擴容邏輯卷ansible node1 -m lvol -a 'lv=lv0 size=1600M vg=vg0 resizefs=yes'
案例18:sefcontext---修改context值ansible node1 -m file -a 'path=/share state=directory'修改context值ansible node1 -m sefcontext -a 'target="/share(/.*)?" setype=samba_share_t state=present'應用新的selinux 文件的context值ansible node1 -m command -a 'restorecon -irv /share'
案例19:debug用戶輸出自定義的信息,類似于echo、print等輸出命令 。ansible中的debug主要用于輸出變量值、表達式值,以及用于when條件判斷時 。使用方式非常簡單
ansible使用臨時命令通過模塊來執行任務

文章插圖
案例20:cron---計劃任務模塊

推薦閱讀