Ad-Hoc之經常使用模塊上篇

使用 ansible-doc [模塊名字] 查看模塊幫助信息也能夠訪問官方文檔查看
官方模塊文檔
html

toc

command 執行命令(默認)

command模塊執行的命令不能有管道,但shell模塊能夠web

[root@Ansible ~]# ansible hosts -m command -a "hostnamectl"
localhost | CHANGED | rc=0 >>
   Static hostname: Ansible
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 611eb9146733482c813dee0118e98c2e
           Boot ID: 842cefc3079845d0948229dd97ca0c56
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.3.2.el7.x86_64
      Architecture: x86-64
nfs1 | CHANGED | rc=0 >>
   Static hostname: Client2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 611eb9146733482c813dee0118e98c2e
           Boot ID: 3dd31b653cf6496aba7d59e0705889e0
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.3.2.el7.x86_64
      Architecture: x86-64
web1 | CHANGED | rc=0 >>
   Static hostname: Client1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 611eb9146733482c813dee0118e98c2e
           Boot ID: f2d91614d04a4ae0b8ef94136738b70a
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.3.2.el7.x86_64
      Architecture: x86-64

shell 執行命令

執行任何 shell 命令,可使用管道,但對一些複雜的命令太不支持,好比 awkdocker

[root@Ansible ~]# ansible hosts -m shell -a "ifconfig eth0|grep 'inet'"
localhost | CHANGED | rc=0 >>
        inet 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255
        inet6 fe80::862b:ec1d:da56:b3f5 prefixlen 64 scopeid 0x20<link>
web1 | CHANGED | rc=0 >>
        inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
        inet6 fe80::fe0a:ef25:6dce:b81a prefixlen 64 scopeid 0x20<link>
nfs1 | CHANGED | rc=0 >>
        inet 192.168.1.3 netmask 255.255.255.0 broadcast 192.168.1.255
        inet6 fe80::fe0a:ef25:6dce:b81a prefixlen 64 scopeid 0x20<link>
        inet6 fe80::1a2a:995:6f5a:ffe8 prefixlen 64 scopeid 0x20<link>
## 不太支持awk
[root@Ansible ~]# ansible hosts -m shell -a "df -h|awk '/\/$/{print $5}'"
localhost | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 3.0G 14G 18% /
web1 | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 3.1G 14G 18% /
nfs1 | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 3.1G 14G 18% /

get_url 聯網下載

  • url ---文件在網絡上的具體位置
  • dest ---下載到被控端的哪一個目錄下
  • checksum ---校驗(md5 sha256)
## 添加阿里雲的base倉庫
[root@Ansible ~]# ansible web -m get_url -a "url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo"
web1 | CHANGED => {
    "changed": true, 
    "checksum_dest": null, 
    "checksum_src": "a149656624ecdf9c5549bf419925b1d8adddefb6", 
    "dest": "/etc/yum.repos.d/CentOS-Base.repo", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "13151789a512213f1695a5b427b1a9ab", 
    "mode": "0644", 
    "msg": "OK (2523 bytes)", 
    "owner": "root", 
    "size": 2523, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557748542.57-71249069620018/tmpFjRxR8", 
    "state": "file", 
    "status_code": 200, 
    "uid": 0, 
    "url": "http://mirrors.aliyun.com/repo/Centos-7.repo"
}
## 添加阿里雲的epel倉庫
[root@Ansible ~]# ansible web -m get_url -a "url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo"
web1 | CHANGED => {
    "changed": true, 
    "checksum_dest": null, 
    "checksum_src": "2feedd589b72617f03d75c4b8a6e328cc1aad918", 
    "dest": "/etc/yum.repos.d/epel.repo", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "bddf35db56cf6be9190fdabeae71c801", 
    "mode": "0644", 
    "msg": "OK (664 bytes)", 
    "owner": "root", 
    "size": 664, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557748737.47-206113667370211/tmpEBlLWk", 
    "state": "file", 
    "status_code": 200, 
    "uid": 0, 
    "url": "http://mirrors.aliyun.com/repo/epel-7.repo"
}
## 生成yum緩存
[root@Ansible ~]# ansible web -m command -a "yum makecache"

yum 安裝軟件模塊

  • name ---軟件包名稱
  • state ---指定使用yum的方法
    • installed,present ---安裝軟件包
    • removed,absent ---移除軟件包
    • latest ---安裝最新軟件包
## 安裝httpd服務(顯示有點亂,主要是全部換行都顯示\n)
[root@Ansible ~]# ansible hosts -m yum -a "name=httpd state=installed"
nfs1 | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-89.el7.centos updates 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-89.el7.centos \n\nComplete!\n"
    ]
}
web1 | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-89.el7.centos updates 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-89.el7.centos \n\nComplete!\n"
    ]
}
localhost | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-89.el7.centos updates 2.7 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.7 M\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-89.el7.centos \n\nComplete!\n"
    ]
}
## 卸載httpd服務(顯示有點亂,主要是全部換行都顯示\n)
[root@Ansible ~]# ansible hosts -m yum -a "name=httpd state=removed"
nfs1 | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "已加載插件:fastestmirror\n正在解決依賴關係\n--> 正在檢查事務\n---> 軟件包 httpd.x86_64.0.2.4.6-89.el7.centos 將被 刪除\n--> 解決依賴關係完成\n\n依賴關係解決\n\n================================================================================\n Package 架構 版本 源 大小\n================================================================================\n正在刪除:\n httpd x86_64 2.4.6-89.el7.centos @updates 9.4 M\n\n事務概要\n================================================================================\n移除 1 軟件包\n\n安裝大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在刪除 : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n 驗證中 : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n\n刪除:\n httpd.x86_64 0:2.4.6-89.el7.centos \n\n完畢!\n"
    ]
}
web1 | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "已加載插件:fastestmirror\n正在解決依賴關係\n--> 正在檢查事務\n---> 軟件包 httpd.x86_64.0.2.4.6-89.el7.centos 將被 刪除\n--> 解決依賴關係完成\n\n依賴關係解決\n\n================================================================================\n Package 架構 版本 源 大小\n================================================================================\n正在刪除:\n httpd x86_64 2.4.6-89.el7.centos @updates 9.4 M\n\n事務概要\n================================================================================\n移除 1 軟件包\n\n安裝大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在刪除 : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n 驗證中 : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n\n刪除:\n httpd.x86_64 0:2.4.6-89.el7.centos \n\n完畢!\n"
    ]
}
localhost | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "已加載插件:fastestmirror\n正在解決依賴關係\n--> 正在檢查事務\n---> 軟件包 httpd.x86_64.0.2.4.6-89.el7.centos 將被 刪除\n--> 解決依賴關係完成\n\n依賴關係解決\n\n================================================================================\n Package 架構 版本 源 大小\n================================================================================\n正在刪除:\n httpd x86_64 2.4.6-89.el7.centos @updates 9.4 M\n\n事務概要\n================================================================================\n移除 1 軟件包\n\n安裝大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在刪除 : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n 驗證中 : httpd-2.4.6-89.el7.centos.x86_64 1/1 \n\n刪除:\n httpd.x86_64 0:2.4.6-89.el7.centos \n\n完畢!\n"
    ]
}

copy 配置模塊

  • src --- 推送數據的源文件信息
  • dest --- 推送數據的目標路徑
  • backup --- 對原來的文件,會放到 /var/lib/docker/overlay2/ 目錄下
  • content --- 直接批量在被管理端文件中添加內容
  • group --- 將本地文件推送到遠端,指定文件屬組信息
  • owner --- 將本地文件推送到遠端,指定文件屬主信息
  • mode --- 將本地文件推送到遠端,指定文件權限信息
## 把/etc/hosts文件推送到全部主機/tmp/test.txt
[root@Ansible ~]# ansible hosts -m copy -a "src=/etc/hosts dest=/tmp/test.txt owner=www group=www mode=644"
localhost | FAILED! => {
    "changed": false, 
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "msg": "chown failed: failed to look up user www", 
    "owner": "root", 
    "path": "/tmp/test.txt", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 158, 
    "state": "file", 
    "uid": 0
}
web1 | FAILED! => {
    "changed": false, 
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "msg": "chown failed: failed to look up user www", 
    "owner": "root", 
    "path": "/tmp/test.txt", 
    "size": 158, 
    "state": "file", 
    "uid": 0
}
nfs1 | FAILED! => {
    "changed": false, 
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "msg": "chown failed: failed to look up user www", 
    "owner": "root", 
    "path": "/tmp/test.txt", 
    "size": 158, 
    "state": "file", 
    "uid": 0
}
## 被覆蓋的文件會備份到/var/lib/docker/overlay2/
[root@Ansible ~]# [root@Ansible ~]# ansible hosts -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
-bash: [root@Ansible: 未找到命令
[root@Ansible ~]# ansible hosts -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
localhost | SUCCESS => {
    "changed": false, 
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/etc/hosts", 
    "secontext": "system_u:object_r:net_conf_t:s0", 
    "size": 158, 
    "state": "file", 
    "uid": 0
}
web1 | CHANGED => {
    "backup_file": "/etc/hosts.4087.2019-05-12@20:24:56~", 
    "changed": true, 
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "54fb6627dbaa37721048e4549db3224d", 
    "mode": "0644", 
    "owner": "root", 
    "size": 158, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557663893.54-81828959416663/source", 
    "state": "file", 
    "uid": 0
}
nfs1 | CHANGED => {
    "backup_file": "/etc/hosts.3795.2019-05-12@20:24:56~", 
    "changed": true, 
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "54fb6627dbaa37721048e4549db3224d", 
    "mode": "0644", 
    "owner": "root", 
    "size": 158, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557663893.44-149096126702721/source", 
    "state": "file", 
    "uid": 0
}
## 直接向遠端文件內寫入數據信息,而且會覆蓋遠端文件內原有數據信息
[root@Ansible ~]# ansible hosts -m copy -a "content='###songguoyou' dest=/etc/hosts"
localhost | CHANGED => {
    "changed": true, 
    "checksum": "4c42e5babe87ef18dc6edb66c4ce4c03dd8908e7", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "12c74c48e83ac492c160f647d64a91e5", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:net_conf_t:s0", 
    "size": 13, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557664064.75-189921694361228/source", 
    "state": "file", 
    "uid": 0
}
web1 | CHANGED => {
    "changed": true, 
    "checksum": "4c42e5babe87ef18dc6edb66c4ce4c03dd8908e7", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "12c74c48e83ac492c160f647d64a91e5", 
    "mode": "0644", 
    "owner": "root", 
    "size": 13, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557664064.74-223362295723281/source", 
    "state": "file", 
    "uid": 0
}
nfs1 | CHANGED => {
    "changed": true, 
    "checksum": "4c42e5babe87ef18dc6edb66c4ce4c03dd8908e7", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "12c74c48e83ac492c160f647d64a91e5", 
    "mode": "0644", 
    "owner": "root", 
    "size": 13, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557664064.79-185916316363979/source", 
    "state": "file", 
    "uid": 0
}
相關文章
相關標籤/搜索