我使用的ubuntu22.04版本,使用apt安装。官网指引如下:
- $ sudo apt-get install software-properties-common
- $ sudo apt-add-repository ppa:ansible/ansible
- $ sudo apt-get update
- $ sudo apt-get install ansible
由于内部网络需要代理上网,需要在apt配置代理。
- sudo export http_proxy=http://192.168.1.1:8080
- sudo export https_proxy=http://192.168.1.1:8080
安装完,reboot重启,并查看版本ansible --version

ssh 无密码认证。在控制节点为用户生成 SSH 密钥,并在被管理主机之间共享。执行 ssh-keygen 命令,执行 ssh-copy-id 命令,在受控主机之间共享 ssh 密钥,示例如下
- # 生成本机的密码
- $ ssh-keygen
-
- # 把本地主机的公钥复制到远程主机。
- $ ssh-copy-id root@108.61.163.242
-
- # 可能会如下情况:
- $ ssh root@108.61.163.242
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
- Someone could be eavesdropping on you right now (man-in-the-middle attack)!
- It is also possible that a host key has just been changed.
- The fingerprint for the ECDSA key sent by the remote host is
- SHA256:HDjXJvu0VYXWF+SKMZjSGn4FQmg/+w6eV9ljJvIXpx0.
- Please contact your system administrator.
- Add correct host key in /Users/wangdong/.ssh/known_hosts to get rid of this message.
- Offending ECDSA key in /Users/wangdong/.ssh/known_hosts:46
- ECDSA host key for 108.61.163.242 has changed and you have requested strict checking.
- Host key verification failed.
-
- # 一般这个问题,是你重置过你的服务器后。你再次想访问会出现这个问题。
- 解决办法如下:
- ssh-keygen -R 108.61.163.242
语法:ansible host options
举例:检查所有主机是否存活,执行命令如下
- root@:/etc/ansible# ansible server -m ping
- 192.168.1.1 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python3"
- },
- "changed": false,
- "ping": "pong"
- }
Ansible-doc用来查询ansible模块文档的说明,类似于man命令
语法格式:ansible-play playbook.yml
Ansible-console是Ansible为用户提供的一款交互式工具,类似于Windows的cmd或者是Linux中shell
- [root@centos01 ~]# ansible-console
- Welcome to the ansible console.
- Type help or ? to list commands.
-
- root@all (2)[f:5]$ cd web
- root@web (2)[f:5]$ list
- 192.168.100.20
- 192.168.100.30
command模块在远程主机执行基础命令,不支持管道、重定向等shell的特性
在所有主机上运行“ls ./”命令,运行前切换到/home目录下。
操作如下:
[root@centos01 ~] ansible web -m command -a "chdir=/ ls ./"
shell模块在远程主机执行命令,相当于调用远程主机的Shell进程
- [root@centos01 ~] ansible web -m shell -a "echo hello world "
- 192.168.100.20 | SUCCESS | rc=0 >>
- hello world
copy模块用于复制指定主机文件到远程主机的指定位置
- [root@centos01 ~] ansible web -m copy -a "src=/etc/hosts
- dest=/root/a1.hosts mode=777 owner=root group=root"
hostname模块用于管理远程主机上的主机名
- [root@centos01 ~] ansible 192.168.100.20 -m hostname -a "name=test"
service模块为用来管理远程主机上的服务的模块
- [root@centos01 ~] ansible web -m service -a "name=httpd
- enabled=yes state=restarted"
user模块主要用于管理远程主机上的用户账号
- [root@centos01 ~] ansible web -m user -a "name=user01
- system=yes uid=502 group=root groups=root shell=/etc/nologin
- home=/home/user01 password=pwd@123"
相关参考中文官网:Ansible中文权威指南- 国内专业的Ansible中文官方学习手册
https://www.cnblogs.com/-wenli/p/13292855.html
shell和ansible自动化运维实例_ansible playbook执行shell脚本_一个高效工作的家伙的博客-CSDN博客