美文网首页
ansible日常使用命令

ansible日常使用命令

作者: frankie_cheung | 来源:发表于2021-11-11 17:22 被阅读0次

    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*"

    相关文章

      网友评论

          本文标题:ansible日常使用命令

          本文链接:https://www.haomeiwen.com/subject/dcrgzltx.html