一看必会系列:ansible 本站实验基础

来源:本站原创 Linux 超过285 views围观 0条评论

 

以下为ansible实验的基础配置

           

创建免密登陆
[root@ansible_master ~]# ssh-keygen -t rsa -P ”
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
3b:13:81:60:1d:34:54:32:27:b8:35:64:e3:d1:07:8e root@ansible_master
The key’s randomart image is:

 

将.pub导入到目标服务器

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.142.102
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.142.103
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.142.104

修改 /etc/ansible/hosts

#以下[ ]中为rols调用名

[test]
192.168.142.104

 

运行 ansible-playbook

ansible-playbook /etc/ansible/roles/xx.yaml -vv

—-技巧————————————–
ansible 常用模块之command/shell/raw

是否支持管道

module    是否支持管道
command    不支持管道
shell    支持管道——–
raw    支持管道  ——–

使用方法

[root@ ~]# ansible localhost -m command -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello jeff

[root@ ~]# ansible localhost -m shell -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello jeff

[root@ ~]# ansible localhost -m raw -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello jeff

直接执行某个文件

[root@ ~]# ansible host32 -m command -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello jeff

[root@ ~]# ansible host32 -m raw -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello jeff

[root@ ~]# ansible host32 -m shell -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello jeff

 

[root@ansible_master roles]# ansible all -m shell -a ‘systemctl status iptables  |grep Active’
192.168.142.102 | SUCCESS | rc=0 >>
   Active: active (exited) since Wed 2018-02-28 11:29:23 CST; 33min ago

192.168.142.104 | SUCCESS | rc=0 >>
   Active: active (exited) since Mon 2018-02-26 17:48:34 CST; 1 day 18h ago

192.168.142.103 | SUCCESS | rc=0 >>
   Active: active (exited) since Mon 2018-02-26 17:49:06 CST; 1 day 18h ago

[root@ansible_master roles]#

ansible-doc -l  列出所有支持的模块
            -s  列出模块支持的动作
            -h
            -v 或-vvv 显示详细信息

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=3742转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点