ansible日常使用命令

2021-11-11  本文已影响0人  frankie_cheung

1.host文件

[hosts]

ip1
ip2
ip3

[hosts:vars]
ansible_ssh_user=mysql
ansible_ssh_pass='kks!kj90Sjf2K0'

2.copy

从本地分发文件到远程主机
ansible -i hosts文件 hosts -m copy -a "src=/local_path/file dest=/remote_path/file mode=0755"

3.执行sh

执行一些远程的命令
ansible -i hosts文件 hosts -m shell -a "bash /remote_path/xxx.sh"
ansible -i hosts文件 hosts -m shell -a "ls -h /remote_path/file"

4.拉取文件到本地

远程服务器有些文件,需要拉取到ansible所在主机
ansible -i mysql_hosts hosts -m fetch -a "src=/remote_path/file dest=/local_path/{{inventory_hostname}}_file flat=yes "

5.sudo或者root用户

有时候服务器为了安全,禁止root ssh远程跳转到其他主机的,所以执行一些高权限的命令,需要sudo权限的。

上面的host文件需要修改为如下

ansible_ssh_user=sudo_user
ansible_ssh_pass='aaajf%2020,0707'
ansible_become=yes
ansible_become_user=root
ansible_become_method=sudo

sudo_user : 可以sudo的用户
示例如下,普通用户无法yum ,需要sudo或者root去yum
ansible -i host文件 hosts -b --become-method su --become-user root -m shell -a "yum install per*"

上一篇下一篇

猜你喜欢

热点阅读