03.Ansible常用Ad-Hoc模式

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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值