ansible操做(一)

ansible晉級操做之ad-hoc命令

所謂的ad-hoc命令!

若是咱們敲入一些命令去比較快的完成一些事情,而不須要將這些執行的命令特別保存下來, 這樣的命令就叫作 ad-hoc 命令。Ansible提供兩種方式去完成任務,一是 ad-hoc 命令,一是寫 Ansible playbook.前者能夠解決一些簡單的任務, 後者解決較複雜的任務.html

在學習了 playbooks 以後,你才能體會到 Ansible 真正的強大之處在哪裏.python

在什麼情境下去使用ad-hoc 命令呢?

好比說查看ip,查看服務狀態等.那種少了命令便可實現的操做git

測試操做

[root@localhost ~]# ansible group1 -a "ls"
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
10.0.15.60 | CHANGED | rc=0 >>
test1.txt

10.0.15.66 | CHANGED | rc=0 >>
test2.txt
-------------------------------------------------------
group1是在/etc/ansible/hosts中設置的組,即操做當前組中的主機
不瞭解的朋友能夠看看我上篇博客:傳送門

#上面的警告能夠經過修改配置文件進行註釋
編輯ansible.cfg 修改deprecation_warnings 爲False便可

ansible group1 -a "ls" -u username    #-u username 表示指定的用戶執行 

ansible有許多模塊,默認是 ‘command’,也就是命令模塊
咱們能夠經過 -m 選項來指定「不一樣的模塊」.在前面所示的例子中, 由於咱們是要在 group1 組下的服務器中執行 ls 命令,由於是執行命令,因此就不須要指定command模塊。使用 默認設定就OK

注意:command 模塊不支持 shell 變量,也不支持管道等 shell 相關的東西.若是你想使用 shell相關的這些東西, 請使用’shell’ 模塊.

shell模塊使用

[root@localhost ~]# ansible group1 -m shell -a 'echo $PATH'
10.0.15.66 | CHANGED | rc=0 >>
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin 10.0.15.60 | CHANGED | rc=0 >>
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/cpgroup/jdk/bin:/usr/local/cpgroup/jdk/jre/bin:/usr/local/cpgroup/ant/dist/bin:/usr/local/cpgroup/maven/bin:/usr/local/cpgroup/python/bin:/usr/local/git/bin !!!注意使用Ansible ad-hoc 命令行接口時(與使用 Playbooks 的狀況相反)。尤爲注意shell 引號的規則。好比在上面的例子中,若是使用雙引號」echo $PATH」,求出PATH」變量在當前系統的值,而咱們想要將這個命令傳遞到其餘機器上面 [root@localhost ~]# ansible group1 -m shell -a "echo $PATH"
10.0.15.66 | CHANGED | rc=0 >>
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin 10.0.15.60 | CHANGED | rc=0 >>
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin [root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin

ansible下的scp

[root@localhost ~]# ansible group1 -m copy -a "src=/root/1.txt dest=/root/1.txt"
10.0.15.66 | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/root/1.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1550550512.6-88436779222448/source", "state": "file", "uid": 0 } 10.0.15.60 | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/root/1.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1550550512.59-19910559807601/source", "state": "file", "uid": 0 }

ansible修改文件權限

[root@localhost ~]# ansible group1 -m file -a "dest=/root/1.txt mode=777"
10.0.15.60 | CHANGED => { "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/root/1.txt", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "state": "file", "uid": 0 } 10.0.15.66 | CHANGED => { "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/root/1.txt", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "state": "file", "uid": 0 }

修改文件所屬用戶所屬組

[root@localhost ~]# ansible group1 -m file -a "dest=/root/1.txt mode=644 owner=www-data group=www-data"
10.0.15.66 | CHANGED => { "changed": true, "gid": 1000, "group": "www-data", "mode": "0644", "owner": "www-data", "path": "/root/1.txt", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "state": "file", "uid": 1000 } 10.0.15.60 | CHANGED => { "changed": true, "gid": 1001, "group": "www-data", "mode": "0644", "owner": "www-data", "path": "/root/1.txt", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "state": "file", "uid": 1001 }

建立與刪除

ansible group -m file -a "dest=/root/test state=directory"
沒有state參數默認建立文件,添加後變爲建立目錄
state=absent 這個參數表明刪除文件
在建立過程當中能夠在雙引號中添加一些其餘參數 如mode owner group等

yum的管理

Ansible 提供對 yum 和 apt 的支持shell

#查看當前包是否安裝
[root@localhost ~]# ansible group1 -m yum -a "name=net-tools state=present"
10.0.15.66 | SUCCESS => { "ansible_facts": { "pkg_mgr": "yum" }, "changed": false, "msg": "", "rc": 0, "results": [ "net-tools-2.0-0.24.20131004git.el7.x86_64 providing net-tools is already installed" ] } 10.0.15.60 | SUCCESS => { "ansible_facts": { "pkg_mgr": "yum" }, "changed": false, "msg": "", "rc": 0, "results": [ "net-tools-2.0-0.22.20131004git.el7.x86_64 providing net-tools is already installed" ] }

ansible進行用戶管理

使用 ‘user’ 模塊能夠方便的建立帳戶,刪除帳戶,或是管理現有的帳戶bash

建立用戶
[root@localhost ~]# ansible all -m user -a "name=test password=123456" [WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly. 10.0.15.66 | CHANGED => { "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/test", "name": "test", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 10.0.15.60 | CHANGED => { "changed": true, "comment": "", "create_home": true, "group": 1002, "home": "/home/test", "name": "test", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1002 } 刪除用戶 [root@localhost ~]# ansible all -m user -a "name=test state=absent" 10.0.15.60 | CHANGED => { "changed": true, "force": false, "name": "test", "remove": false, "state": "absent" } 10.0.15.66 | CHANGED => { "changed": true, "force": false, "name": "test", "remove": false, "state": "absent" } 建立組 [root@localhost ~]# ansible all -m group -a "name=test" 10.0.15.66 | CHANGED => { "changed": true, "gid": 1001, "name": "test", "state": "present", "system": false } 10.0.15.60 | CHANGED => { "changed": true, "gid": 1002, "name": "test", "state": "present", "system": false } 刪除組 [root@localhost ~]# ansible all -m group -a "name=test state=absent" 10.0.15.60 | CHANGED => { "changed": true, "name": "test", "state": "absent" } 10.0.15.66 | CHANGED => { "changed": true, "name": "test", "state": "absent" }

官方文檔:傳送門服務器

相關文章
相關標籤/搜索