命令实例
copy模块
** 拷贝也可以直接拷贝目录
** “*”星号不用加转义
** 拷贝/data01/yc_files/ansible_test/1/1.txt到yc机器的/data01/yc_files/ansible_test/2/目录下
ansible yc -m copy -a "src="/data01/yc_files/ansible_test/1/1.txt" dest="/data01/yc_files/ansible_test/2/""
注:root执行,新文件属主为root;yc执行,新文件属主为yc
shell模块
** ansible执行的shell类型命令stdout为false时会错误
ansible test -m shell -a "[ -d /data01/test ] && rm -rf /data01/test/" # 在/data01/test目录不存在时会报错
ansible test -m shell -a "[ -d /data01/test ] && rm -rf /data01/test/ || echo no"
切换身份
** 切换身份安装libselinux-python
1.配置ansible.cfg中的become_ask_pass=True
2.ansible all --become --become-user=root --become-method=sudo -v -a "yum -y install libselinux-python"
错误汇总
1.Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!
安装libselinux-python,关闭selinux,重启系统
2.Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
http://lxlxlx.blog.51cto.com/3363989/1894379
以指定用户执行命令时,需执行如下代码(root为例)
ssh-copy-id -i ~/.ssh/id_rsa.pub root@localhost