目錄
- 寫在前面
- 4. Ansible Modules 模塊
- 4.1 Ansible常用模塊
- 4.1.1 Command模塊
- 4.1.2 shell模塊
- 4.1.3 scrpit模塊
- 4.1.4 file模塊
- 4.1.5 copy模塊
- 4.1.6 lineinfile模塊
- 4.1.7 systemd模塊
- 4.1.8 yum模塊
- 4.1.9 get_url模塊
- 4.1.10 yum_repository模塊
- 4.1.11 user模塊
- 4.1.12 group模塊
- 4.1.13 mount模塊
- 4.1.14 cron模塊
- 4.1.15 setup模塊
- 4.1.16 synchronize模塊
- 4.1.x 官網
寫在前面
這是Ansible系列第二篇,內容為Ansible Modules的介紹、使用
序號連續上篇筆記 Ansible01-Ansible的概述、實驗環境初始化、Inventory
回顧一下Ansible架構:
4. Ansible Modules 模塊
Modules,類似于Linux中的命令,如:yum模塊->yum命令;file模塊;user模塊
- Ansible格式:
ansible -i 指定清單 -m 指定模塊 -a 指定模塊中的選項
- 查看ansible所有模塊:
ansible-doc -l
- 查看ansible某模塊的參數選項:
ansible-doc -s 模塊名# -s, --snippet Show playbook snippet for these plugin types: inventory, lookup,module
4.1 Ansible常用模塊
模塊類型 | 模塊名 | 模塊功能 |
---|---|---|
命令和腳本模塊 | command模塊 | 默認的模塊,執行簡單命令,不支持特殊符號 |
shell模塊 | 類似command模塊,但支持特殊符號 | |
script模塊 | 分發腳本并執行 | |
文件 | file | 創建目錄,文件,軟連接 |
copy | 遠程分發文件,修改權限,所有者,備份 | |
服務 | systemd | 服務管理 |
service | 服務管理,centos7之前使用 | |
軟件包 | yum_repository | yum源 |
yum | yum命令 | |
get_url | 下載軟件 | |
系統管理 | mount | 掛載 |
cron | 定時任務 | |
用戶管理 | group | 管理用戶組 |
user | 管理用戶 | |
其他 | ping | 檢查 ansible與其他節點連通性 |
debug | 用于檢查/顯示 變量 | |
docker/k8s/zabbix/grafana | 管理docker/k8s/zabbix/grafana | |
unarchive/rsync/mysql_db/mysql_user | 壓縮解壓縮/同步數據/數據庫模塊 | |
… | … |
4.1.1 Command模塊
默認的模塊,執行簡單命令,不支持特殊符號
ansible -s command
:
選項 | 描述 |
---|---|
argv | 將命令作為列表而非字符串傳遞。使用argv可避免對原本會被誤解的值(例如用戶名中帶空格的情況)進行引用。只能提供字符串或列表形式之一,不能同時提供。 |
chdir | 在執行命令前切換到此目錄。 |
cmd | 要執行的命令。 |
creates | 如果該文件已經存在,此步驟不會執行。 |
removes | 如果該文件存在,此步驟將會執行。 |
stdin | 直接將指定的值設置為命令的stdin。 |
stdin_add_newline | 如果設置為yes,在stdin數據末尾追加換行符。 |
strip_empty_ends | 從stdout/stderr結果的結尾去除空行。 |
warn | 啟用或禁用任務警告。 |
[root@manager ~]# ansible all -m command -a 'ip a s ens33'
192.168.100.149 | CHANGED | rc=0 >>
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:e1:55:bb brd ff:ff:ff:ff:ff:ffaltname enp2s1inet 192.168.100.149/24 brd 192.168.100.255 scope global dynamic noprefixroute ens33valid_lft 1737sec preferred_lft 1737secinet6 fe80::20c:29ff:fee1:55bb/64 scope linkvalid_lft forever preferred_lft forever#切換到指定目錄
[root@manager ~]# ansible front -m command -a 'chdir=/root ls'
192.168.100.148 | CHANGED | rc=0 >>
anaconda-ks.cfg
test1.log
4.1.2 shell模塊
類似command模塊,不過支持特殊符號
[root@manager ~]# ansible all -m shell -a "ip a s ens33 | awk -F'[/ ]+' 'NR==4{print \$3}'"
192.168.100.150 | CHANGED | rc=0 >>
192.168.100.150
192.168.100.148 | CHANGED | rc=0 >>
192.168.100.148
192.168.100.149 | CHANGED | rc=0 >>
192.168.100.149
4.1.3 scrpit模塊
用于執行腳本的模塊
#編寫shell腳本
[root@manager ~]# cat sys_info.sh
#!/bin/bash
#author: tassel
#desc: ansible script modules test shellscript
hostname
hostname -I
ip a s ens33 |awk -F'[ /]+' 'NR==4{print $3}'
uptime
whoami
date +%F
sleep 10 #用于可以ps -ef和pstree查看ansible狀態#測試
[root@manager ~]# ansible all -m script -a '/root/sys_info.sh'
192.168.100.149 | CHANGED => {"changed": true,"rc": 0,"stderr": "Shared connection to 192.168.100.149 closed.\r\n","stderr_lines": ["Shared connection to 192.168.100.149 closed."],"stdout": "backup\r\n192.168.100.149 \r\n192.168.100.149\r\n 20:05:14 up 24 min, 1 user, load average: 0.68, 0.58, 0.33\r\nroot\r\n2024-05-26\r\n","stdout_lines": ["backup","192.168.100.149 ","192.168.100.149"," 20:05:14 up 24 min, 1 user, load average: 0.68, 0.58, 0.33","root","2024-05-26"]
}#ps和pstree查看
ps -ef | grep ansible
root 2679 1728 55 20:07 pts/0 00:00:01 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
root 2682 2679 5 20:07 pts/0 00:00:00 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
root 2683 2679 4 20:07 pts/0 00:00:00 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
root 2684 2679 4 20:07 pts/0 00:00:00 /usr/bin/python3.11 /usr/bin/ansible all -m script -a /root/sys_info.sh
## 不難發現,使用python運行
#pstree 也可以看到,是ssh執行腳本
─sshd(921)─┬─sshd(1721)───sshd(1726)───bash(1728)│ ├─sshd(1725)───sshd(1758)───sftp-server(1759)│ ├─sshd(2526)───sshd(2531)───bash(2533)───pstree(2674)│ └─sshd(2530)───sshd(2561)───sftp-server(2564)
4.1.4 file模塊
file模塊管理文件、目錄、軟連接。
相當于結合了touch、mkdir、rm、ln-s
- 常用參數介紹:
參數 | 解釋 |
---|---|
src | Ansible端源文件或者目錄,一般用于軟連接; |
follow | 支持link文件拷貝; |
force | 覆蓋遠程主機不一致的內容; |
group | 設定遠程主機文件夾的組名; |
mode | 指定遠程主機文件及文件夾的權限; |
owner | 設定遠程主機文件夾的用戶名; |
path(必寫) | 目標路徑,也可以用dest,name代替; |
state | 狀態包括:file(默認)更新文件,不存在不創建、link創建軟連接、directory創建目錄、hard、touch創建文件、absent遞歸刪除; |
attributes | 文件或者目錄特殊屬性。 |
# 創建文件
[root@manager ~]# ansible front -m file -a 'path=/tmp/0526.txt state=touch'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"dest": "/tmp/0526.txt","gid": 0,"group": "root","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 0,"state": "file","uid": 0
}[root@front ~]# ls /tmp
0526.txt ######遞歸創建目錄
[root@manager ~]# ansible front -m file -a 'path=/tmp/a/b/c/d/e state=directory'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/tmp/a/b/c/d/e","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 0
}[root@front ~]# tree -F /tmp
/tmp
├── 0526.txt
├── a/
│ └── b/
│ └── c/
│ └── d/
│ └── e/######創建軟連接
[root@manager ~]# ansible front -m file -a 'path=/tmp/hosts_sl src=/etc/hosts state=link'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"dest": "/tmp/hosts_sl","gid": 0,"group": "root","mode": "0777","owner": "root","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 10,"src": "/etc/hosts","state": "link","uid": 0
}[root@manager ~]# ansible front -m shell -a 'ls -l /tmp/hosts*'
192.168.100.148 | CHANGED | rc=0 >>
lrwxrwxrwx. 1 root root 10 May 26 20:40 /tmp/hosts_sl -> /etc/hosts######修改權限
[root@manager ~]# ansible front -m file -a 'path=/tmp/ansible-test mode=700 owner=nobody group=nobody state=directory'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 65534,"group": "nobody","mode": "0700","owner": "nobody","path": "/tmp/ansible-test","secontext": "unconfined_u:object_r:user_tmp_t:s0","size": 6,"state": "directory","uid": 65534
}[root@manager ~]# ansible front -m shell -a 'ls -ld /tmp/ansible-test' 192.168.100.148 | CHANGED | rc=0 >>
drwx------. 2 nobody nobody 6 May 26 20:44 /tmp/ansible-test###### 刪除目錄(遞歸,若刪除高層目錄,其子目錄也被刪除)
[root@manager ~]# ansible front -m file -a 'path=/tmp/a/b/c/ state=absent'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"path": "/tmp/a/b/c/","state": "absent"
}[root@manager ~]# ansible front -m shell -a 'tree -F /tmp'
192.168.100.148 | CHANGED | rc=0 >>
/tmp
├── 0526.txt
├── a/
│ └── b/
4.1.5 copy模塊
批量分發,類似于scp:1個節點(管理節點)發送文件或壓縮包到所有被管理端.
注意:copy是單向的傳輸。
還有一個fetch 批量拉取
選項 | 解釋 |
---|---|
src | source 源文件,管理端的某個文件. |
dest | destination 目標,被管理端的目錄/文件. |
backup | backup=yes 則會在覆蓋前進行備份,文件內容要有變化或區別. |
mode | 修改權限 |
owner | 修改為指定所有者 |
group | 修改為指定用戶組 |
# 傳輸/etc/hosts到所有主機組的主機
[root@manager ~]# ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'
192.168.100.150 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"backup_file": "/etc/hosts.4177.2024-05-26@20:53:57~","changed": true,"checksum": "54e822ead7405808873d7efeb6ae02f1b58b1962","dest": "/etc/hosts","gid": 0,"group": "root","md5sum": "a608fd387bb46d36a21adb75b01490c0","mode": "0644","owner": "root","secontext": "system_u:object_r:net_conf_t:s0","size": 183,"src": "/root/.ansible/tmp/ansible-tmp-1716728034.4922798-3386-265280371596300/source","state": "file","uid": 0
}[root@manager ~]# ansible front -m shell -a 'ls /etc/hosts*'
192.168.100.148 | CHANGED | rc=0 >>
/etc/hosts
/etc/hosts.7761.2024-05-26@20:53:57~
4.1.6 lineinfile模塊
修改配置文件使用,類似sed 's///g’和sed ‘cai’
4.1.7 systemd模塊
systemd模塊類似systemctl命令
service模塊,是centos7之前的service命令
systemd模塊 | 說明 |
---|---|
name(必須寫) | 用于指定服務名稱 |
enabled | yes開機自啟動 (yes/no) |
state | 表示服務開,關,重啟 started 開啟、state=stopped 關閉、state=reloaded 重讀配置文件(服務支持)、state=restarted 重啟(關閉再開啟) |
daemon-reload | yes是否重新加載對應的服務的管理配置文件(書寫systemctl配置文件) |
#開啟并開機自啟 nfs
[root@manager ~]# ansible storage -m systemd -a 'name=nfs-server enabled=yes state=started'
192.168.100.150 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"enabled": true,"name": "nfs-server","state": "started",# 關閉并開機不自啟 firewalld
[root@manager ~]# ansible front -m systemd -a 'name=firewalld enabled=no state=stopped'
192.168.100.148 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"enabled": false,"name": "firewalld","state": "stopped",# 重啟nfs
[root@manager ~]# ansible storage -m systemd -a 'name=nfs-utils state=reloaded'
192.168.100.150 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"name": "nfs-utils","state": "started",
4.1.8 yum模塊
yum安裝軟件
選項 | 描述 |
---|---|
name | 必選項,所安裝的包的名稱 |
state | 安裝-> present(installed) ; 安裝最新版本的-> latest ;卸載包->absent(removed) |
update_cache | 強制更新yum的緩存 |
conf_file | 指定遠程yum安裝時所依賴的配置文件(安裝本地已有的包)。 |
disable_pgp_check | 是否禁止GPG checking,只用于presentor latest。 |
disablerepo | 臨時禁止使用yum庫。 只用于安裝或更新時。 |
enablerepo | 臨時使用的yum庫。只用于安裝或更新時。 |
skip_borken | 跳過異常軟件節點 |
autoremove | 當設置為yes且狀態為absent時,自動移除不再被任何已安裝包依賴的包。 |
#安裝軟件
ansible all -m yum -a 'name=tree,lrzsz,sshpass state=present'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"msg": "","rc": 0,"results": ["Installed: lrzsz-0.12.20-43.el8.x86_64","Installed: sshpass-1.09-4.el8.x86_64"]
}#更新軟件包
ansible all -m yum -a "name=rsync state=latest"
4.1.9 get_url模塊
相當于wget命令,需要所有主機均能訪問互聯網
建議在管理節點下載好軟件包后,copy分發
選項 | 功能 |
---|---|
url | 指定要下載的地址 |
dest | 下載到哪個目錄 |
#下載zabbix
https://mirrors.aliyun.com/zabbix/zabbix/6.5/centos/8/x86_64/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm[root@manager ~]# ansible all -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/6.5/centos/8/x86_64/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm dest=/tmp/'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"checksum_dest": null,"checksum_src": "b65b658d3246bac2783436b45e499fa57c68a5ef","dest": "/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm","elapsed": 0,"gid": 0,"group": "root","md5sum": "cba9f70da1ef01574bd2608b43d39b31","mode": "0644","msg": "OK (599492 bytes)","owner": "root","secontext": "unconfined_u:object_r:admin_home_t:s0","size": 599492,"src": "/root/.ansible/tmp/ansible-tmp-1716729618.362924-3810-161846721092095/tmpb1f4j0t0","state": "file","status_code": 200,"uid": 0,"url": "https://mirrors.aliyun.com/zabbix/zabbix/6.5/centos/8/x86_64/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm"
}[root@manager ~]# ansible all -m shell -a 'ls /tmp/zabbix*'
192.168.100.148 | CHANGED | rc=0 >>
/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm
192.168.100.149 | CHANGED | rc=0 >>
/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm
192.168.100.150 | CHANGED | rc=0 >>
/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm#調用yum模塊安裝ansible all -m yum -a 'name=/tmp/zabbix-agent-7.0.0-alpha3.release1.el8.x86_64.rpm state=present'
4.1.10 yum_repository模塊
更推薦寫好yum配置文件,再copy推送
選項 | 解釋 |
---|---|
name | yum源中名字 [epel] |
description | yum源的注釋說明 對應的 是name的內容 |
baseurl | yum源中 baseurl 下載地址 |
enabled | 是否啟動這個源 yes/no |
gpgcheck | 是否啟動gpgcheck功能 no |
file | 指定yum源的文件 自動添加 .repo 默認與模塊名字一致. |
- yum配置文件與yum-repository對比:
yum配置文件 | yum-repository選項 |
---|---|
[epel] | name=epel #默認yum源文件的名字與這個一致. |
name=Extra Pxxxxx | description=“Extra xxxxxxx” |
baseurl=http://mirrors.aliyun.com/epel/7/$basearch | baseurl=“http://mirrors.aliyun.com/epel/7/$basearch” |
enabled=1 | enabled=yes |
gpgcheck=0 | gpgcheck=no |
4.1.11 user模塊
選項 | 描述 |
---|---|
comment | 用戶的描述信息 |
create_home | 是否創建家目錄(yes/no) |
force | 在使用state=absent時, 行為與userdel –force一致 |
group | 指定基本組 |
groups | 指定附加組,如果指定為(groups=)表示刪除所有組 |
home | 指定用戶家目錄 |
move_home | 如果設置為home=時, 試圖將用戶主目錄移動到指定的目錄 |
name(重要) | 指定用戶名 |
non_unique | 該選項允許改變非唯一的用戶ID值 |
password(重要) | 指定用戶密碼 password={{‘密碼’ |
remove | 在使用state=absent時, 行為是與userdel –remove一致 |
shell(重要) | 指定默認shell /bin/bash,/sbin/nologin |
state(重要) | 設置帳號狀態,默認為present表示新建用戶,指定值為absent表示刪除 |
system | 當創建一個用戶,設置這個用戶是系統用戶。這個設置不能更改現有用戶 |
uid(重要) | 指定用戶的uid |
update_password | 更新用戶密碼 |
expires | 指明密碼的過期時間 |
append | 添加一個新的組 |
#創建一個用戶,uid2000,nologin
[root@manager ~]# ansible all -m user -a 'name=ans uid=2000 shell=/sbin/nologin create_home=no state=present'
192.168.100.149 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"comment": "","create_home": false,"group": 2000,"home": "/home/ans","name": "ans","shell": "/sbin/nologin","state": "present","system": false,"uid": 2000
}# 更新密碼
[root@manager ~]# ansible all -m user -a "name=test password={{'1'|password_hash('sha512','hashsalt')}} state=present"
[DEPRECATION WARNING]: Encryption using the Python crypt module is deprecated. The Python
crypt module is deprecated and will be removed from Python 3.13. Install the passlib library
for continued encryption functionality. This feature will be removed in version 2.17.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"comment": "","create_home": true,"group": 2001,"home": "/home/test","name": "test","password": "NOT_LOGGING_PASSWORD","shell": "/bin/bash","state": "present","system": false,"uid": 2001
}
4.1.12 group模塊
類比user模塊
4.1.13 mount模塊
mount選項 | 說明 |
---|---|
fstype | 文件系統類型,指定文件系統,如xfs, ext4, iso9660, nfs等 |
src | 源地址,例如NFS地址(例如172.16.1.31/data) |
path | 注意這里不是dest,掛載點,即要把源掛載到的目標路徑 |
state | absent 卸載并修改fstab;unmounted 卸載不修改/etc/fstab;present 僅修改/etc/fstab 不掛載;mounted 掛載并修改/etc/fstab;remounted 重新掛載 |
#掛載nfs#1. 安裝nfs
[root@manager ~]# ansible front -m yum -a 'name=nfs-utils state=present'
192.168.100.148 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"msg": "Nothing to do","rc": 0,"results": []
}#2.創建掛載文件夾
[root@manager ~]# ansible front -m file -a 'path=/ans-upload state=directory'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/ans-upload","secontext": "unconfined_u:object_r:default_t:s0","size": 6,"state": "directory","uid": 0
}#3.掛載nfs并檢查
[root@manager ~]# ansible front -m mount -a 'src=192.168.100.150:/nfsupload/ path=/ans-upload/ fstype=nfs state=mounted'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"backup_file": "","boot": "yes","changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "nfs","name": "/ans-upload/","opts": "defaults","passno": "0","src": "192.168.100.150:/nfsupload/"
}
[root@manager ~]# ansible front -a 'df -h /ans-upload'
192.168.100.148 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
192.168.100.150:/nfsupload 64G 2.3G 62G 4% /ans-upload
[root@manager ~]# ansible front -a 'grep upload /etc/fstab'
192.168.100.148 | CHANGED | rc=0 >>
192.168.100.150:/nfsupload/ /ans-upload/ nfs defaults 0 0# 4. 卸載nfs并刪除fstab
[root@manager ~]# ansible front -m mount -a 'src=192.168.100.150:/nfsupload/ path=/ans-upload/ fstype=nfs state=absent'
192.168.100.148 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"backup_file": "","boot": "yes","changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "nfs","name": "/ans-upload/","opts": "defaults","passno": "0","src": "192.168.100.150:/nfsupload/"
}
[root@manager ~]# ansible front -a 'df -h /ans-upload' 192.168.100.148 | FAILED | rc=1 >>
df: /ans-upload: No such file or directorynon-zero return code
[root@manager ~]# ansible front -a 'grep upload /etc/fstab' 192.168.100.148 | FAILED | rc=1 >>
non-zero return code
4.1.14 cron模塊
管理系統的定時任務
cron模塊選項 | 說明 |
---|---|
name | 定時任務名字(一定要加上),對應下面注釋的內容 |
minute | 分鐘,例如 "*" 表示每分鐘,"5" 表示第5分鐘 |
hour | 小時,例如 "*" 表示每個小時,"12" 表示中午12點 |
day | 日期,例如 "*" 表示每天,"1" 表示每月1日 |
month | 月份,例如 "*" 表示每個月,"6" 表示6月 |
week | 周幾,例如 "*" 表示每天,"0" 表示周日 |
job | 指定命令或腳本(重定向到空),例如 "/sbin/ntpdate ntp1.aliyun.com &>/dev/null" |
state | present 表示添加定時任務(默認);absent 表示刪除 |
# 設置每3分鐘定時同步時間
[root@manager ~]# ansible all -m cron -a 'name="sync time" minute="*/3" job="/sbin/ntpdate ntp1.aliyun.com &> /dev/null" state=present'
192.168.100.149 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"envs": [],"jobs": ["sync time"]
}[root@manager ~]# ansible all -a 'crontab -l'
192.168.100.149 | CHANGED | rc=0 >>
#Ansible: sync time
*/3 * * * * /sbin/ntpdate ntp1.aliyun.com &> /dev/null#刪除
[root@manager ~]# ansible all -m cron -a 'name="sync time" state=absent' 192.168.100.149 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"envs": [],"jobs": []
}[root@manager ~]# ansible all -a 'crontab -l'
192.168.100.149 | CHANGED | rc=0 >>
4.1.15 setup模塊
用于收集遠程主機的信息,并將這些信息以facts的形式返回給Ansible控制節點。
這些facts可以包括系統變量(如操作系統類型、架構、網絡配置、已安裝軟件包等),并且在Playbook執行期間可以被其他任務使用。
選項 | 描述 |
---|---|
fact_path | 用于存放本地Ansible事實(*.fact文件)的路徑。此目錄下的文件如果可執行,將被執行,其結果會被添加到ansible_local事實中;如果文件不可執行,則會被讀取。適用于從2.1版本開始。文件/結果格式可以是JSON或INI格式。默認的fact_path可以在ansible.cfg中為自動調用setup作為gather_facts一部分時指定。Windows環境下有特定選項,請查看注釋。 |
filter | 如果提供,僅返回匹配此shell風格(fnmatch通配符)的變量。這允許篩選出特定的facts進行查看或使用。 |
gather_subset | 如果提供,限制收集的額外事實子集。可能的值包括:all(全部)、min(最小集合)、hardware(硬件信息)、network(網絡信息)、virtual(虛擬化信息)、ohai(類似Chef Ohai的擴展信息)、facter(使用Facter收集的信息)。可以指定值的列表來定義更大的子集。值前可加!來排除特定子集的收集,例如:!hardware,!network,!virtual,!ohai,!facter。如果指定!all,則只收集最小集合。要避免收集最小集合,可以指定!all,!min。要僅收集特定事實,使用!all,!min并指定特定的事實子集。如果只想隱藏某些收集到的事實,使用filter參數。 |
gather_timeout | 設置單個事實收集的默認超時時間(以秒為單位)。這有助于控制事實收集過程,避免因個別慢速收集導致整個任務超時。 |
#查看內存
[root@manager ~]# ansible front -m setup -a "filter='*mem*'"
192.168.100.148 | SUCCESS => {"ansible_facts": {"ansible_memfree_mb": 3028,"ansible_memory_mb": {"nocache": {"free": 3371,"used": 269},"real": {"free": 3028,"total": 3640,"used": 612},"swap": {"cached": 0,"free": 4027,"total": 4027,"used": 0}},"ansible_memtotal_mb": 3640,"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false
}
4.1.16 synchronize模塊
使用rsync進行文件和目錄同步的功能
選項 | 描述 |
---|---|
compress | 開啟壓縮,默認為開啟 |
archive | 是否采用歸檔模式同步,保證源和目標文件屬性一致 |
checksum | 是否效驗文件的校驗和 |
dirs | 以非遞歸的方式傳輸目錄 |
links | 同步鏈接文件 |
recursive | 是否遞歸同步子目錄,yes/no |
rsync_opts | 使用rsync的額外參數 |
copy_links | 同步的時候是否復制連接 |
delete | 刪除源中沒有而目標存在的文件 |
src | 源目錄及文件 |
dest | 目標目錄及文件 |
dest_port | 目標接受的端口 |
rsync_path | 服務的路徑,指定rsync命令來在遠程服務器上運行 |
rsync_timeout | 指定rsync操作的IP超時時間 |
set_remote_user | 設置遠程用戶名 |
–exclude=.log | 忽略同步.log結尾的文件 |
mode | 同步的模式,rsync同步的方式PUSH、PULL,默認都是推送push |
#將本地的/tmp同步到front組的/tmp
ansible front -m synchronize -a "src=/tmp/ dest=/tmp/"#完全同步,類似rsync的完全同步
ansible front -m synchronize -a 'src=/tmp/ dest=/tmp/ delete=yes'
4.1.x 官網
docs.ansible.com