标签:delete 安装 echo command TE lin libs 模块 ror
批量管理linux服务器指的是:批量执行命令、下发文件等等yum install epel-release -y
#编辑/etc/yum.repos.d/epel.repo,注释mirrorlist,打开baseurl
yum list ansible #ansible版本,如果这个命令运行有问题的话,多运行几次
yum install ansible -y
ansible-doc -l #查看总帮助
ansible-doc -s shell #查看shell模块的帮助
ansible-doc -s raw
[testgroup]
192.168.56.40
192.168.56.41
192.168.56.42
[test41]
192.168.56.41
[test42]
192.168.56.42
ssh-keygen -t rsa#一直按回车即可
ssh-copy-id -i .ssh/id_rsa.pub ip #上传公钥到服务器
ansible 主机组或者主机 -m 模块 -a 命令
2 ansible语法测试
ansible testgroup -m ping
ansible testgroup -m command -a "pwd"
3 : ansible模块command(不支持管道,不建议使用)
ansible testgroup -m command -a "pwd"
ansible testgroup -m command -a "echo testa|grep a" #这个不能正常使用
ansible testgroup -m command -a "echo bb >>/tmp/testansible" #重定向也无法正常使用
6: ansible模块shell(支持管道)
ansible testgroup -m shell -a "echo testa|grep a" #支持管道
ansible testgroup -m shell -a "echo bb >>/tmp/testansible" #支持重定向
ansible testgroup -m shell -a "cat /etc/passwd|awk -F‘:‘ ‘{print \$1}‘" #遇到特殊符号需要加入\转义,这样子ansible才能正常运行
7: ansible模块raw(如果运行命令,一般使用shell模块)
说明为什么会有raw,由于ansible依赖python环境,例如需要有python-simplejson之类的包,如果没安装的话,就无法使用shell等模块,就需要先用raw安装一下。
ansible testgroup -m raw -a "yum install python-simplejson -y"
ansible testgroup -m raw -a "yum install libselinux-python -y"
ll -h /usr/local/src/
cat /tmp/testdir/test.txt
ansible testgroup -m copy -a "src=/tmp/testdir/test.txt dest=/usr/local/src/"
#src指定本地的文件
#dest指定远程主机的目录或者文件
ll -h /usr/local/src/
ansible testgroup -m copy -a "src=/tmp/testdir/ dest=/usr/local/src/" #testdir文件夹没拷贝
ansible testgroup -m copy -a "src=/tmp/testdir dest=/usr/local/src/" #testdir文件夹也拷贝了
ansible testgroup -m copy -a "src=/tmp/testdir/test.txt dest=/usr/local/src/ backup=yes"?
ansible testgroup -m copy -a "src=/tmp/testdir/test.txt dest=/usr/local/src/ backup=yes owner=nobody group=nobody mode=0600"
cat /usr/local/src/script
chmod a+x /usr/local/src/script
ansible testgroup -m script -a "/usr/local/src/script"
#cat /usr/local/src/test.yaml
- hosts: testgroup
tasks:
- name: test ansible
shell: echo "shell 1" >>/tmp/a
- name: test2
shell: echo "shell 2" >>/tmp/a
运行:ansible-playbook /usr/local/src/test.yaml
#cat /usr/local/src/test_copy.yaml
- hosts: testgroup
tasks:
- name: test ansible
shell: echo "shell 1" >>/tmp/a
- name: test copy
copy: src=/tmp/a dest=/usr/local/src/
#下发nginx配置,对nginx进行检测
#cat /usr/local/src/test_nginx.yaml
- hosts: testgroup
tasks:
- name: copy nginx conf
copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
- name: nginx conf check
shell: /usr/local/nginx/sbin/nginx -t
register: nginx_result
- debug: var=nginx_result
#可只输出stdout_lines或者stderr_lines
标签:delete 安装 echo command TE lin libs 模块 ror
原文地址:http://blog.51cto.com/395469372/2133486