实例学习ansible系列(1)Helloworld

知识点:Ansible安装与设定
知识点:第一个Helloworld

安装ansible

在192.168.32.31上安装ansible

[root@host31 local]# yum -y install epel-release
[root@host31 local]# yum -y install ansible
  • 1
  • 2

确认安装

[root@host31 local]# ansible --version
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
[root@host31 local]#
  • 1
  • 2
  • 3
  • 4
  • 5

设定ssh通路和ansible

分别在两台机器上生成ssh的key

[root@host31 ~]# ssh-keygen
  • 1

设定/etc/hosts

[root@host31 ~]# grep host31 /etc/hosts
192.168.32.31 host31
[root@host31 ~]#
  • 1
  • 2
  • 3

作如下设定,保证ssh通路畅通

# ssh-copy-id -i host31
  • 1

在ansible所安装的机器上,追加机器信息到/etc/ansible/hosts中

[root@host31 ansible]# grep host31 /etc/ansible/hosts
host31
[root@host31 ansible]#
  • 1
  • 2
  • 3

确认ansible正常动作

[root@host31 ~]# ansible localhost -m ping
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[root@host31 ~]#
[root@host31 ~]# ansible host31 -m ping
host31 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第一个Helloworld例子

[root@host31 ~]# ansible host32 -m shell -a "echo hello world |tee /tmp/helloworld"
host32 | SUCCESS | rc=0 >>
hello world

[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

使用说明:
host32: host名称需要在/etc/ansible/hosts中设定,或者在inventory中设定,ansible是基于ssh通路的python实现,此处应该理解为ansible的操作对象机器
-m:指定ansible所用到的module,ansible支持很多的module,而且还在不断的增长中,ansible2.1的版本已经增加到500个以上。
整体这句ansible语句的语义为,在host32上执行后其会执行echo hello world并且将结果输出到host32的/tmp/helloworld中

[root@host31 ~]# ll /tmp/helloworld
ls: cannot access /tmp/helloworld: No such file or directory
[root@host31 ~]# ssh host32 cat /tmp/helloworld
hello world
[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/firsttry/p/10294026.html