Ansible常用Ad-Hoc模式
1、authorized_key
# 给主机添加密钥认证
ansible all -m authorized_key -a key="{{ lookup('file', '~/.ssh/id_rsa.pub') }} user=root" --ask-pass -u root
2、ping
ansible cdh_uat -m ping
172.17.208.73 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.17.208.72 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.17.208.74 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.17.208.75 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
3、group
# 添加组
ansible all -m group -a "gid=41000 name=ansible_test state=present system=no"
# 删除组
ansible all -m group -a "gid=41000 name=ansible_test state=absent"
4、user
# 创建用户
ansible all -m user -a 'uid=41000 name=ansible_test state=present group=ansible_test shell=/bin/bash'
5、copy
# 从文件复制
ansible all -m copy -a "src=/Users/user/Downloads/hosts dest=/etc/hosts"
#从内容复制
ansible all -m copy -a "content='hello world\n' dest=/root/hi.txt"
6、fetch
ansible 10.9.251.30 -m fetch -a 'src=/etc/hosts dest=./hosts'
10.9.251.30 | CHANGED => {
"changed": true,
"checksum": "4b8e199a4422de7f7d5854f9e5cbc8656f557162",
"dest": "/Users/user/Downloads/hosts/10.9.251.30/etc/hosts",
"md5sum": "11196d628534ecf0a5eada5b6077fdc9",
"remote_checksum": "4b8e199a4422de7f7d5854f9e5cbc8656f557162",
"remote_md5sum": null
}
7、command
不支持变量
ansible all -m command -a 'ifconfig'
ansible all -m command -a 'chdir=/tmp mkdir ansible_tmp'
8、shell
ansible all -m shell -a 'echo hello | passwd --stdin ansible_test'
9、file
# 创建指定目录
ansible all -m file -a 'path=/tmp/hello.dir state=directory'
10、cron
# 每3min同步一次时间,任务名称叫ntp date
ansible all -m cron -a 'minute=*/3 job="/usr/sbin/ntpdate time.pool.aliyun.com >/dev/null 2>&1" state=present name="ntp date"'
11、yum
ansible all -m yum -a 'name=htop state=installed'
ansible all -m yum -a 'name=nginx state=installed'
12、service(Centos6)
ansible all -m service -a 'name=nginx state=started'
13、systemd(Centos7)
ansible all -m systemd -a 'name=nginx state=started enabled=true'
来源:https://blog.csdn.net/lynnyq/article/details/102726499