ansible的模塊使用說明


參考官方鏈接:

https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html#parallelism-and-shell-commands

  • shell模塊使用
    提權操作:

[root@ansible-test ~]# ansible test1 -m shell -a "ls /root" -u zhouyuqiang -k --become -K
SSH password:
SUDO password[defaults to SSH password]:
test1 | CHANGED | rc=0 >>
anaconda-ks.cfg
GateOne-master
master.zip

  • copy操作:
    官方:
    $ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

實際:
[root@ansible-test ~]# ansible web1 -m copy -a "src=/root/redis-5.0.3.tar.gz dest=/usr/local/src"

查詢:
[root@ansible-test ~]# ansible web1 -m shell -a "ls /usr/local/src"
172.16.188.191 | CHANGED | rc=0 >>
redis-5.0.3.tar.gz


  • file模塊:

創建文件夾:
state 一定需要指定:
狀態:
創建文件夾:directory
(遞歸)刪除文件:absent
創建一個文件:touch

[root@ansible-test ansible-yaml]# ansible web -m file -a "dest=/etc/yum.repos.d/nginx.repo state=absent"
test2 | CHANGED => {
"changed": true,
"path": "/etc/yum.repos.d/nginx.repo",
"state": "absent"
}
test1 | CHANGED => {
"changed": true,
"path": "/etc/yum.repos.d/nginx.repo",
"state": "absent"
}
test3 | CHANGED => {
"changed": true,
"path": "/etc/yum.repos.d/nginx.repo",
"state": "absent"
}


  • yum模塊

state 一定需要
狀態:
具體版本:present
最新版本:latest
name 是指定需要安裝的包名

[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=present"

卸載一個版本:
指定狀態就可以:absent
[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=absent"


  • user模塊
    創建一個用戶並設置密碼:
    [root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"

查詢:
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"
刪除用戶:
指定狀態即可:absent
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 state=absent"

指定創建用戶不能登錄,一般是服務用戶:
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=123 shell=/sbin/nologin "
如下:
user1❌1002:1003::/home/user1:/sbin/nologin


  • git 模塊
    需要指定用戶:-uroot
    repo指定git倉庫地址
    目標主機需要有git命令,沒有進行安裝
    [root@ansible-test ~]# ansible web1 -m yum -a "name=git state=latest"

[root@ansible-test ~]# ansible web1 -vvvv -m git -a "repo=https://github.com/ansible/ansible.git dest=/opt/ansibleå-source" -uroot


  • Managing Services啟動服務模塊
    啟動服務:
    state=started
    state=started
    [root@ansible-test ~]# ansible web -m service -a "name=memcached state=started" -uroot

關閉服務:
state=stopped
[root@ansible-test ~]# ansible web -m service -a "name=memcached state=stopped" -uroot

重新啟動:
state=restarted

[root@ansible-test ~]# ansible web -m service -a "name=memcached state=restarted" -uroot

加入開機啟動:
enabled=true

[root@ansible-test ~]# ansible web -m service -a "name=memcached enabled=true" -uroot


  • setup模塊
    [root@ansible-test ~]# ansible web -m setup

如:
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "VMware, Inc.",
"ansible_uptime_seconds": 82564,
"ansible_user_dir": "/root",
"ansible_user_gecos": "root",
"ansible_user_gid": 0,
"ansible_user_id": "root",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 0,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",

  進行過濾操作:
  
  "filter=ansible_user_shell"
  
  [root@ansible-test ~]# ansible web -m setup  -a "filter=ansible_user_shell"

test2 | SUCCESS => {
"ansible_facts": {
"ansible_user_shell": "/bin/bash"

    [root@ansible-test ~]# ansible web -m setup  -a "filter=ansible_all_ipv4_addresses"

test2 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.188.191"
]
},
"changed": false
}

可以通過統配符進行匹配:

如:filter=ipv4

[root@ansible-test ~]# ansible web -m setup -a "filter=ipv4"
test2 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.188.191"
],
"ansible_default_ipv4": {
"address": "172.16.188.191",
"alias": "eth0",
"broadcast": "172.16.188.255",
"gateway": "172.16.188.1",


  • playbook的模塊

yaml的語法,采用yaml語法,具體語法其實很簡單,主要是空格區分目錄。每個目錄層級是2個空格,每個:后面一定要需要空格,否則語法報錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM