您的位置:首页 > 文旅 > 美景 > ansible

ansible

2025/1/10 8:02:34 来源:https://blog.csdn.net/weixin_70754025/article/details/141321158  浏览:    关键词:ansible

他是基于模块工作的,本身没有批量部署的能力。

真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

格式

ansible  主机ip| 域名|组名|别名  -m  ping|copy|...  '参数'

--ping模块

# 查看有没有安装epel
# yum list installed|grep epel
# 安装ansible
# yum -y install ansible
# 查看ansible的版本
# ansible --version

查找ansible的配置文件

# find /etc/ -name "*ansible*"
/etc/ansible
/etc/ansible/ansible.cfg
设置免密

# ssh-keygen

#  ls ./.ssh/
id_rsa  id_rsa.pub

给s0和s1设置免密登录

# ssh-copy-id -i 192.168.1.17
# ssh-copy-id -i 192.168.1.18
# vim /etc/ansible/hosts
[group01] 
192.168.1.17
192.168.1.18

ping  group01的IP

[root@bb ~]# ansible 192.168.1.17 -m ping
192.168.1.17 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
[root@bb ~]# ansible group01 -m ping
192.168.1.17 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
192.168.1.18 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}

--hostname模块

--file模块

在group01中的主机的 /tmp/中创建abc文件

-m  表示调用模块

state=directory表示当前的状态被设置为“目录”

[root@bb ~]#  ansible group01 -m file -a 'path=/tmp/abc state=directory'
192.168.1.17 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/abc", "size": 6, "state": "directory", "uid": 0
}
192.168.1.18 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/abc", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0
}
# ls -l /tmp/
总用量 4
drwxr-xr-x. 2 root root   6 8月  16 11:43 abc

--copy模块

(管理机)
# ls
anaconda-ks.cfg  mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz mysql57.tar.gz
# ls
anaconda-ks.cfg  mysql57.tar.gz
​
# 把mysql57.tar.gz传到group02组中的主机中
# ansible group02 -m copy -a 'src=./mysql57.tar.gz dest=~'
other | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "ca7c056f43922133ac4bfa788849172ff124ce47", "dest": "/root/mysql57.tar.gz", "gid": 0, "group": "root", "md5sum": "d7c8436bbf456e9a4398011a0c52bc40", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 694785800, "src": "/root/.ansible/tmp/ansible-tmp-1723791895.72-3029-205251780527035/source", "state": "file", "uid": 0
}
192.168.2.111 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "ca7c056f43922133ac4bfa788849172ff124ce47", "dest": "/root/mysql57.tar.gz", "gid": 0, "group": "root", "md5sum": "d7c8436bbf456e9a4398011a0c52bc40", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 694785800, "src": "/root/.ansible/tmp/ansible-tmp-1723791895.62-3027-254129236082512/source", "state": "file", "uid": 0
}
192.168.2.110 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "ca7c056f43922133ac4bfa788849172ff124ce47", "dest": "/root/mysql57.tar.gz", "gid": 0, "group": "root", "md5sum": "d7c8436bbf456e9a4398011a0c52bc40", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 694785800, "src": "/root/.ansible/tmp/ansible-tmp-1723791895.71-3026-134059210870560/source", "state": "file", "uid": 0
}
​
# 验证(被管理)
[root@s0 ~]# ls
aaa  anaconda-ks.cfg  mysql57.tar.gz
[root@s1 ~]# ls
aaa  anaconda-ks.cfg  mysql57.tar.gz
[root@s2 ~]# ls
aaa  anaconda-ks.cfg  mysql57.tar.gz

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com