美文网首页工具癖
ansible 初体验2

ansible 初体验2

作者: 王哲理 | 来源:发表于2018-01-06 16:02 被阅读45次

使用命令生成秘钥(机器互信)

[root@ansible ~]# ssh-keygen 

将公钥发送到所有安装 客户端的主机

[root@ansible ~]# ssh-copy-id 192.168.163.170

[root@ansible ~]# ssh-copy-id 192.168.163.171

安装 Ansible 软件

[root@ansible ~]# yum install -y ansible

修改配置文件,将客户机添加进组,

(在文末添加即可)

[root@ansible ~]# vim /etc/ansible/hosts 

[webserver]

192.168.163.170

192.168.163.171

ansible(主)命令体验:

1.ping

ansible all -m ping 

!检测机器是否可登录,ping模块不需要传送参数

!注:这里的ping模块并非调用了系统的ping命令,而是类似于登录到远程机器再echo出一个信息。

2.command

!可以执行任意命令,但不接受管道命令和重定向符号,如果想使用这些,则需要使用Shell模块。

ansible all -m command -a "ls" #在所有匹配主机上执行ls命令

playbook:

- name: test for command

command: ls

3.copy

#复制一个文件到远程服务器

ansible all -m copy -a "src=/tmp/text.txt dest=/home/tmp/"

#将本地text.txt文件复制到所有匹配主机的/home/tmp/路径下。

playbook:

- name: test for copy

copy: src=/tmp/text.txt dest=/home/tmp/ force=yes

4.file

这个模块这次构建系统中并没有用到,官方的解释是用来设置文件、文件夹或快链的属性,或者删除文件、快链、文件夹。

创建一个文件夹,如果目标不存在

ansible webservers -m file -a "path=/tmp/tdir state=directory mode=0755"

playbook

- name: create a directory if it doesn't exist

- file:

path: /etc/some_directory

state: directory

mode: 0755

5.get_url

!从服务器上下载一个文件到远程主机指定的目录

ansible webservers -m get_url -a "url='http://baidu.com dest=/tmp/ba.html"

playbook

- name: download foo.conf

get_url:

url: http://example.com/path/file.conf

dest: /etc/foo.conf

mode: 0440

6.yum

特别适用于批量安装一些依赖包的时候

ansible webservers -m yum -a "name=httpd state=latest"

ansible webservers -m yum -a "name=httpd state=absent" !删除包

playbook

- name: install the latest version of Apache

yum:

name: httpd

state: latest 

7.service

控制远程服务器上的服务

ansible webservers -m service -a "name=nginx state=restart"

playbook

- name: restart rmote nginx

service: name=nginx state=restart

8.setup

收集远程服务器信息

ansible all -m setup

在playbook中,这项任务默认是执行的,不需要列出。

9.server start

ansbile all -m service -a 'name=httpd state=started

10.script

ansibleall -m script -a '/root/123.sh

11.shell

ansible all -m shell -a "echo $RANDOM | tr'0-9' 'a-z' > /root/123"

相关文章

网友评论

    本文标题:ansible 初体验2

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