Ansible常用模块详解
Ansible常用模塊
2015年底270多個模塊,2016年達到540個,2018年01月12日有1378個模塊,2018年07月15日1852個模塊,2019年05月25日(ansible 2.7.10)時2080個模塊,2020年03月02日有3387個模塊
雖然模塊眾多,但最常用的模塊也就2,30個而已,針對特定業務只用10幾個模塊
常用模塊幫助文檔參考:
https://docs.ansible.com/ansible/latest/modules/modules_by_category.htmlCommand 模塊
功能:在遠程主機執行命令,此為默認模塊,可忽略-m選項
注意:此命令不支持 $VARNAME < > | ; & 等,用shell模塊實現
范例:
[root@ansible ~]#ansible websrvs -m command -a 'chdir=/etc cat centos-release' 10.0.0.7 | CHANGED | rc=0 >> CentOS Linux release 7.7.1908 (Core) 10.0.0.8 | CHANGED | rc=0 >> CentOS Linux release 8.1.1911 (Core) [root@ansible ~]#ansible websrvs -m command -a 'chdir=/etc creates=/data/f1.txt cat centos-release' 10.0.0.7 | CHANGED | rc=0 >> CentOS Linux release 7.7.1908 (Core) 10.0.0.8 | SUCCESS | rc=0 >> skipped, since /data/f1.txt exists [root@ansible ~]#ansible websrvs -m command -a 'chdir=/etc removes=/data/f1.txt cat centos-release' 10.0.0.7 | SUCCESS | rc=0 >> skipped, since /data/f1.txt does not exist 10.0.0.8 | CHANGED | rc=0 >> CentOS Linux release 8.1.1911 (Core)ansible websrvs -m command -a ‘service vsftpd start’ ansible websrvs -m command -a ‘echo magedu |passwd --stdin wang’ ansible websrvs -m command -a 'rm -rf /data/' ansible websrvs -m command -a 'echo hello > /data/hello.log' ansible websrvs -m command -a "echo $HOSTNAME"Shell模塊
功能:和command相似,用shell執行命令
范例:
[root@ansible ~]#ansible websrvs -m shell -a "echo HOSTNAME" 10.0.0.7 | CHANGED | rc=0 >> ansible 10.0.0.8 | CHANGED | rc=0 >> ansible [root@ansible ~]#ansible websrvs -m shell -a 'echoHOSTNAME' 10.0.0.7 | CHANGED | rc=0 >> centos7.wangxiaochun.com 10.0.0.8 | CHANGED | rc=0 >> centos8.localdomain[root@ansible ~]#ansible websrvs -m shell -a 'echo centos | passwd --stdin wang' 10.0.0.7 | CHANGED | rc=0 >> Changing password for user wang. passwd: all authentication tokens updated successfully. 10.0.0.8 | CHANGED | rc=0 >> Changing password for user wang. passwd: all authentication tokens updated successfully. [root@ansible ~]#ansible websrvs -m shell -a 'ls -l /etc/shadow' 10.0.0.7 | CHANGED | rc=0 >> ---------- 1 root root 889 Mar 2 14:34 /etc/shadow 10.0.0.8 | CHANGED | rc=0 >> ---------- 1 root root 944 Mar 2 14:34 /etc/shadow [root@ansible ~]#ansible websrvs -m shell -a 'echo hello > /data/hello.log' 10.0.0.7 | CHANGED | rc=0 >>10.0.0.8 | CHANGED | rc=0 >>[root@ansible ~]#ansible websrvs -m shell -a 'cat /data/hello.log' 10.0.0.7 | CHANGED | rc=0 >> hello 10.0.0.8 | CHANGED | rc=0 >> hello注意:調用執行命令 類似 cat /tmp/test.md | awk -F‘|’ ‘{print 1,1,1,2}’ &> /tmp/example.txt 這些復雜命令,即使使用shell也可能會失敗,解決辦法:寫到腳本時,copy到遠程,執行,再把需要的結果拉回執行命令的機器
范例:將shell模塊代替command,設為模塊
[root@ansible ~]#vim /etc/ansible/ansible.cfg #修改下面一行 module_name = shellScript模塊
功能:在遠程主機上運行ansible服務器上的腳本
范例:
ansible websrvs -m script -a /data/test.shCopy模塊
功能:從ansible服務器主控端復制文件到遠程主機
#如目標存在,默認覆蓋,此處指定先備份 ansible websrvs -m copy -a “src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes” #指定內容,直接生成目標文件 ansible websrvs -m copy -a "content='test line1\ntest line2' dest=/tmp/test.txt" #復制/etc/下的文件,不包括/etc/目錄自身 ansible websrvs -m copy -a “src=/etc/ dest=/backup”Fetch模塊
功能:從遠程主機提取文件至ansible的主控端,copy相反,目前不支持目錄
范例:
ansible websrvs -m fetch -a ‘src=/root/test.sh dest=/data/scripts’范例:
[root@ansible ~]#ansible all -m fetch -a 'src=/etc/redhat-release dest=/data/os' [root@ansible ~]#tree /data/os/ /data/os/ ├── 10.0.0.6 │ └── etc │ └── redhat-release ├── 10.0.0.7 │ └── etc │ └── redhat-release └── 10.0.0.8└── etc└── redhat-release6 directories, 3 filesFile模塊
功能:設置文件屬性
范例:
#創建空文件 ansible all -m file -a 'path=/data/test.txt state=touch' ansible all -m file -a 'path=/data/test.txt state=absent' ansible all -m file -a "path=/root/test.sh owner=wang mode=755“ #創建目錄 ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql" #創建軟鏈接 ansible all -m file -a ‘src=/data/testfile dest=/data/testfile-link state=link’unarchive模塊
功能:解包解壓縮
實現有兩種用法:
1、將ansible主機上的壓縮包傳到遠程主機后解壓縮至特定目錄,設置copy=yes
2、將遠程主機上的某個壓縮包解壓縮到指定路徑下,設置copy=no
常見參數:
- copy:默認為yes,當copy=yes,拷貝的文件是從ansible主機復制到遠程主機上,如果設置為copy=no,會在遠程主機上尋找src源文件
- remote_src:和copy功能一樣且互斥,yes表示在遠程主機,不在ansible主機,no表示文件在ansible主機上
- src:源路徑,可以是ansible主機上的路徑,也可以是遠程主機上的路徑,如果是遠程主機上的路徑,則需要設置copy=no
- dest:遠程主機上的目標路徑
- mode:設置解壓縮后的文件權限
范例:
ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo' ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777' ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'Archive模塊
功能:打包壓縮
范例:
ansible websrvs -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=wang mode=0600'3.4.9 Hostname模塊
功能:管理主機名
范例:
ansible node1 -m hostname -a “name=websrv” ansible 192.168.100.18 -m hostname -a 'name=node18.magedu.com'Cron模塊
功能:計劃任務
支持時間:minute,hour,day,month,weekday
范例:
#備份數據庫腳本 [root@centos8 ~]#cat mysql_backup.sh mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_date +%F_%T.sql.gz #創建任務 ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh' ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime" #禁用計劃任務 ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=yes" #啟用計劃任務 ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=no" #刪除任務 ansible websrvs -m cron -a "name='backup mysql' state=absent" ansible websrvs -m cron -a 'state=absent name=Synctime'Yum模塊
功能:管理軟件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
范例:
ansible websrvs -m yum -a ‘name=httpd state=present’ #安裝 ansible websrvs -m yum -a ‘name=httpd state=absent’ #刪除Service模塊
功能:管理服務
范例:
ansible all -m service -a 'name=httpd state=started enabled=yes' ansible all -m service -a 'name=httpd state=stopped' ansible all -m service -a 'name=httpd state=reloaded’ ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf" ansible all -m service -a 'name=httpd state=restarted'User模塊
功能:管理用戶
范例:
#創建用戶 ansible all -m user -a 'name=user1 comment=“test user” uid=2048 home=/app/user1 group=root'ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'#刪除用戶及家目錄等數據 ansible all -m user -a 'name=nginx state=absent remove=yes'Group模塊
功能:管理組
范例:
#創建組 ansible websrvs -m group -a 'name=nginx gid=88 system=yes' #刪除組 ansible websrvs -m group -a 'name=nginx state=absent'Lineinfile模塊
ansible在使用sed進行替換時,經常會遇到需要轉義的問題,而且ansible在遇到特殊符號進行替換時,存在問題,無法正常進行替換 。其實在ansible自身提供了兩個模塊:lineinfile模塊和replace模塊,可以方便的進行替換
功能:相當于sed,可以修改文件內容
范例:
ansible all -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=enforcing'" ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"'Replace模塊
該模塊有點類似于sed命令,主要也是基于正則進行匹配和替換
范例:
ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'" ansible all -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='\1'"Setup模塊
功能: setup 模塊來收集主機的系統信息,這些 facts 信息可以直接以變量的形式使用,但是如果主機較多,會影響執行速度,可以使用gather_facts: no 來禁止 Ansible 收集 facts 信息
范例:
ansible all -m setup ansible all -m setup -a "filter=ansible_nodename" ansible all -m setup -a "filter=ansible_hostname" ansible all -m setup -a "filter=ansible_domain" ansible all -m setup -a "filter=ansible_memtotal_mb" ansible all -m setup -a "filter=ansible_memory_mb" ansible all -m setup -a "filter=ansible_memfree_mb" ansible all -m setup -a "filter=ansible_os_family" ansible all -m setup -a "filter=ansible_distribution_major_version" ansible all -m setup -a "filter=ansible_distribution_version" ansible all -m setup -a "filter=ansible_processor_vcpus" ansible all -m setup -a "filter=ansible_all_ipv4_addresses" ansible all -m setup -a "filter=ansible_architecture" ansible all -m setup -a "filter=ansible_processor*"范例:
[root@ansible ~]#ansible all -m setup -a 'filter=ansible_python_version' 10.0.0.7 | SUCCESS => {"ansible_facts": {"ansible_python_version": "2.7.5","discovered_interpreter_python": "/usr/bin/python"},"changed": false } 10.0.0.6 | SUCCESS => {"ansible_facts": {"ansible_python_version": "2.6.6","discovered_interpreter_python": "/usr/bin/python"},"changed": false } 10.0.0.8 | SUCCESS => {"ansible_facts": {"ansible_python_version": "3.6.8","discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false } [root@ansible ~]#本文鏈接:http://www.yunweipai.com/34676.html
總結
以上是生活随笔為你收集整理的Ansible常用模块详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ansible相关工具介绍、实例演示
- 下一篇: Ansible Playbook详解