macos下使用aria2_macOS下 ansible简单安装及基础使用
macOS下 ansible簡單安裝及基礎使用,其實命令是相通的,我這篇測試基本都是在macOS下執行的。在Linux操作系統下幾乎同樣的辦法。
ansible是一種自動化運維工具,基于Python開發,實現了批量系統配置、批量程序部署、批量運行命令等功能
我測系統環境:
OS: macOS Catalina 10.15.7
Python2個版本:我準備基于Python3來按照
? ~ which python
/usr/bin/python
? ~ /usr/bin/python -V
Python 2.7.16
? ~ /usr/local/Cellar/python@3.9/3.9.0/bin/python3 -V
Python 3.9.0
? ~ /usr/local/Cellar/python@3.9/3.9.0/bin/pip3 -V
pip 20.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
? ~ /usr/bin/pip3 -V
pip 19.2.3 from /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8
ansible可以通過源碼,yum,pip,brew等方式安裝,本文采用pip安裝方式,首先ansible基于python環境
? ~ /usr/local/Cellar/python@3.9/3.9.0/bin/pip3 -Vpip 20.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)? ~ /usr/local/Cellar/python@3.9/3.9.0/bin/pip3 install ansibleCollecting ansibleansible --version 命令 檢查安裝是否成功
? ~ /usr/local/bin/ansible --version
ansible 2.10.2
config file = None
configured module search path = ['/Users/lex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.9.0 (default, Oct 6 2020, 04:17:54) [Clang 12.0.0 (clang-1200.0.32.2)]
pip安裝是沒有config file文件的 我們可以將官網的默認文件上傳到服務器
https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfgmac下安裝后缺少 /etc/ansible 目錄,需要手動添加
sudo mkdir /etc/ansible? ansible pwd/etc/ansible? ansible ls? ansible sudo touch ansible.cfg編輯內容:? ansible cat ansible.cfg[defaults]remote_tmp = $HOME/.ansible/tmplocal_tmp = $HOME/.ansible/tmpremote_port = 22host_key_checking = Falseremote_user = rootmodule_name = commandexecutable = /bin/bashprivate_key_file = /Users/lex/.ssh/id_rsa[privilege_escalation][paramiko_connection][ssh_connection][accelerate][selinux][colors]再次查看版本:/etc/ansible/ansible.cfg可以看到了
? ~ ansible --version
ansible 2.10.2
config file = /etc/ansible/ansible.cfg
configured module search path = ['/Users/lex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.9.0 (default, Oct 6 2020, 04:17:54) [Clang 12.0.0 (clang-1200.0.32.2)]
我來測試一個hosts,很重要的一點就是需要配置ssh打通本地主機和服務器。這里不再贅述。
/Users/lex/Downloads/docker/rancher/rancher.txt? rancher cat /Users/lex/Downloads/docker/rancher/rancher.txt# SSH Keys configuration[rancher:vars]ansible_ssh_private_key_file = /Users/lex/.ssh/id_rsaansible_ssh_user=root#My Test Rancher[rancher]10.211.55.4:2210.211.55.5:2210.211.55.6:2210.211.55.7:22下面貼幾個常用的參數:
-m MODULE_NAME, --module-name MODULE_NAME module name to execute (default=command) -i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY specify inventory host path or comma separated host list. --inventory-file is deprecated -a MODULE_ARGS, --args MODULE_ARGS module arguments命令開測:
? ansible -i /Users/lex/Downloads/docker/rancher/rancher.txt -m ping rancher
? ansible -i /Users/lex/Downloads/docker/rancher/rancher.txt -m ping all
? ansible -i /Users/lex/Downloads/docker/rancher/rancher.txt -m raw -a "w" rancher
結果大致如下:
0.211.55.4 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}10.211.55.6 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}下面是w的結果 10:36:23 up 19 min, 1 user, load average: 0.20, 0.16, 0.16USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 10.211.55.2 10:36 0.00s 0.01s 0.00s wShared connection to 10.211.55.6 closed.下面簡單了解幾個模塊:
遠程命令模塊
模塊包括command、script、shell,都可以實現遠程shell命令運行。
command 是Ansible的默認模塊,可以運行遠程權限范圍內的所有shell命令
~ ansible -i /Users/lex/Downloads/docker/rancher/rancher.txt -m shell -a "df -h" rancher~ ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m command -a "ifconfig"~ ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m command -a "w"shell 是執行遠程主機的shell腳本文件ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m shell -a "/home/test.sh"get_url:實現在遠程主機下載指定URL到服務器本地
ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m get_url -a "url=https://www.toutiao.com/i6887741380542759437/ dest=/tmp/index.html mode=0400 force=yes"script 是在遠程主機執行主控端存儲的shell腳本文件,相當于scp+shell組合
做個測試:
? rancher chmod 755 test.sh
? rancher cat /Users/lex/Downloads/docker/rancher/test.sh
#!/bin/bash
date +%F-%T >> /tmp/abc
echo "abc" >> /tmp/abc
執行命令
ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m script -a "/Users/lex/Downloads/docker/rancher/test.sh"執行OK之后可以看到服務器上有/tmp/abc生成
stat模塊
獲取遠程文件狀態信息,包括atime、ctime、mtime、md5、uid、gid等等
ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m stat -a "path=/tmp/test.sh"copy模塊
實現主控端向目標主機拷貝文件,類似scp,下面例子實現拷貝/Users/lex/Downloads/docker/rancher/test.sh文件到對應的各主機的 /tmp/ 目錄下 ,并更新文件屬組及權限
ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m copy -a "src=/Users/lex/Downloads/docker/rancher/test.sh dest=/tmp/ owner=root group=root mode=0755"結果輸出類似:10.211.55.6 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "0608eba2095775a66bb3de241e56fc2027b50611", "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "592b2d653b8559239bd075fedffb1c28", "mode": "0755", "owner": "root", "size": 59, "src": "/root/.ansible/tmp/ansible-tmp-1603778968.826366-45718-266518561249784/source", "state": "file", "uid": 0}對于拷貝上去的那個文件,也可以單獨使用file模塊實現權限修改
ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m file -a "path=/tmp/test.sh owner=root group=root mode=0775"輸出節略:10.211.55.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0775", "owner": "root", "path": "/tmp/test.sh", "size": 59, "state": "file", "uid": 0}yum模塊
軟件包管理操作,常見有yum、apt(pkg和name是要安裝的包名)
下面這個可以在支持apt的Linux上測試
? ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m apt -a "name=iptraf state=latest"下面這個可以在支持Yum的linux主機測試。
ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m yum -a "name=sysstat state=latest"ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m yum -a "pkg=dstat state=latest"10.211.55.6 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "changes": { "installed": [], "updated": [] }, "msg": "", "rc": 0, "results": [ "All packages providing dstat are up to date", "" ]}ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m yum -a "pkg=iptraf state=latest"10.211.55.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "iptraf" ], "updated": [] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirror.bit.edu.cn * updates: mirrors.aliyun.comResolving Dependencies--> Running transaction check---> Package iptraf-ng.x86_64 0:1.1.4-7.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================ Package Arch Version Repository Size================================================================================Installing: iptraf-ng x86_64 1.1.4-7.el7 base 301 kTransaction Summary================================================================================Install 1 PackageTotal download size: 301 kInstalled size: 644 kDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : iptraf-ng-1.1.4-7.el7.x86_64 1/1 Verifying : iptraf-ng-1.1.4-7.el7.x86_64 1/1 Installed: iptraf-ng.x86_64 0:1.1.4-7.el7 Complete!" ]}可以看到服務器上的大概日志:
Oct 27 14:36:28 node1 systemd: Started Session 34 of user root.Oct 27 14:36:28 node1 systemd-logind: New session 34 of user root.Oct 27 14:36:29 node1 python: ansible-ansible.legacy.setup Invoked with filter=ansible_pkg_mgr gather_subset=['!all'] fact_path=/etc/ansible/facts.d gather_timeout=10Oct 27 14:36:29 node1 python: ansible-ansible.legacy.yum Invoked with lock_timeout=30 update_cache=False disable_excludes=None exclude=[] allow_downgrade=False disable_gpg_check=False conf_file=None use_backend=auto state=latest disablerepo=[] releasever=None skip_broken=False autoremove=False download_dir=None enable_plugin=[] installroot=/ install_weak_deps=True name=['sysstat'] download_only=False bugfix=False list=None install_repoquery=True update_only=False disable_plugin=[] enablerepo=[] security=False validate_certs=Trueservice模塊
系統服務管理
state(started/stopped/restarted/reloaded)ansible all -i /Users/lex/Downloads/docker/rancher/rancher.txt -m service -a "name=sshd state=restarted"日志大致如下:
Oct 27 14:40:39 node1 systemd: Started Session 36 of user root.Oct 27 14:40:40 node1 python: ansible-ansible.legacy.setup Invoked with filter=ansible_service_mgr gather_subset=['!all'] fact_path=/etc/ansible/facts.d gather_timeout=10Oct 27 14:40:41 node1 python: ansible-ansible.legacy.systemd Invoked with no_block=False force=None name=sshd daemon_reexec=False enabled=None daemon_reload=False state=restarted masked=None scope=system user=NoneOct 27 14:40:41 node1 systemd: Stopping OpenSSH server daemon...Oct 27 14:40:41 node1 systemd: Stopped OpenSSH server daemon.Oct 27 14:40:41 node1 systemd: Starting OpenSSH server daemon...Oct 27 14:40:41 node1 systemd: Started OpenSSH server daemon.Oct 27 14:41:41 node1 systemd-logind: Removed session 36.再更加詳細的使用方式。大家可以檢索的看看。
1 962 ansible controller,compute -a "systemctl status libvirtd && systemctl restart libvirtd" 1 954 ansible controller,compute -m shell -a "systemctl stop openstack-ceilometer*" 1 953 ansible controller,compute -m shell -a "systemctl start openstack-ceilometer*" 1 952 ansible controller,compute -m shell -a "systemctl stop openstack-ceilometer*" 1 951 ansible controller,compute -m shell -a "openstack-service status"總結
以上是生活随笔為你收集整理的macos下使用aria2_macOS下 ansible简单安装及基础使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: h5禁用浏览器下载视频_Flash正式被
- 下一篇: python 多关键字匹配_使用djan