參考官方連接:html
https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html#parallelism-and-shell-commandsnginx
[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.zipgit
實際:
[root@ansible-test ~]# ansible web1 -m copy -a "src=/root/redis-5.0.3.tar.gz dest=/usr/local/src"github
查詢:
[root@ansible-test ~]# ansible web1 -m shell -a "ls /usr/local/src"
172.16.188.191 | CHANGED | rc=0 >>
redis-5.0.3.tar.gzweb
建立文件夾:
state 必定須要指定:
狀態:
建立文件夾:directory
(遞歸)刪除文件:absent
建立一個文件:touchredis
[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"
}shell
state 必定須要
狀態:
具體版本:present
最新版本:latest
name 是指定須要安裝的包名bash
[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=present"ide
卸載一個版本:
指定狀態就能夠:absent
[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=absent"memcached
查詢:
[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
[root@ansible-test ~]# ansible web1 -vvvv -m git -a "repo=https://github.com/ansible/ansible.git dest=/opt/ansibleå-source" -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
如:
"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",
yaml的語法,採用yaml語法,具體語法其實很簡單,主要是空格區分目錄。每一個目錄層級是2個空格,每一個:後面必定要須要空格,不然語法報錯