ansible使用

配置文件在/etc/ansible/ansible.cfg

默认hosts文件位置在/etc/ansible/hosts
hosts
[web]
192.168.1.6
连通性测试
[root@192 ~]# ansible web -m ping(web是组名)
192.168.1.6 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
shell模块
[root@192 ~]# ansible web -m shell -a 'pwd | echo "hello"'
192.168.1.6 | CHANGED | rc=0 >>
hello

copy 模块
backup=yes 设置备份

[root@192 ~]# ansible web -m copy -a 'src=./sed_test.txt dest=/root/sed_test.txt'
192.168.1.6 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "checksum": "63bea2e3b0c7cd2d1f98bc5b7a9951eafcfead0f", 
    "dest": "/root/sed_test.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/root/sed_test.txt", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 4, 
    "state": "file", 
    "uid": 0
}

fetch 模块
该模块用于从远程某主机获取(复制)文件到本地。

dest:用来存放文件的目录
src:在远程拉取的文件,并且必须是一个file,不能是目录
[root@192 ~]# ansible web -m fetch -a 'src=/root/ao.sh dest=/root/ao.sh'
192.168.1.6 | CHANGED => {
    "changed": true, 
    "checksum": "cb2071ef6f84065bd6ac068a48e6e4c0b9e102d5", 
    "dest": "/root/ao.sh/192.168.1.6/root/ao.sh", 
    "md5sum": "c6d35da4851099ff3376063890cc6141", 
    "remote_checksum": "cb2071ef6f84065bd6ac068a48e6e4c0b9e102d5", 
    "remote_md5sum": null
}

script 模块

chmod +x /root/b.sh
[root@192 ~]# ansible web -m script -a '/root/b.sh'
192.168.1.6 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.6 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.1.6 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

你可能感兴趣的:(linux,python)