在 Ubuntu 22.04安装配置 Ansible

一、按官网指引安装

我使用的ubuntu22.04版本,使用apt安装。官网指引如下:

bash 复制代码
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

由于内部网络需要代理上网,需要在apt配置代理。

bash 复制代码
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 密钥,并在被管理主机之间共享。执行 ssh-keygen 命令,执行 ssh-copy-id 命令,在受控主机之间共享 ssh 密钥,示例如下

bash 复制代码
$ ssh-keygen

$ ssh-copy-id node1.example.com
$ ssh-copy-id node2.example.com

三、ansible命令

1、ansible

语法:ansible host options

举例:检查所有主机是否存活,执行命令如下

bash 复制代码
root@:/etc/ansible# ansible server -m ping
192.168.1.1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
2、Ansible-doc

Ansible-doc用来查询ansible模块文档的说明,类似于man命令

3、Ansible-playbook

语法格式:ansible-play playbook.yml

<!--playbook.yml文件要提前编写好,建议使用绝对路径-->

4、Ansible-console

Ansible-console是Ansible为用户提供的一款交互式工具,类似于Windows的cmd或者是Linux中shell

bash 复制代码
[root@centos01 ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
      <!--输入help或?获取帮助-->
root@all (2)[f:5]$ cd web    <!--使用cd命令切换主机或分组-->
root@web (2)[f:5]$ list                  <!--列出当前的设备-->
192.168.100.20
192.168.100.30
<!--支持Tab键补全,快捷键Ctrl+D或Ctrl+C即可退出当前的虚拟终端-->

四、ansible模块

1、command模块

command模块在远程主机执行基础命令,不支持管道、重定向等shell的特性

在所有主机上运行"ls ./"命令,运行前切换到/home目录下。

操作如下:

bash 复制代码
[root@centos01 ~] ansible web -m command -a "chdir=/ ls ./"
2、shell模块

shell模块在远程主机执行命令,相当于调用远程主机的Shell进程

bash 复制代码
[root@centos01 ~] ansible web -m shell -a "echo hello world "        <!--输出到屏幕-->
192.168.100.20 | SUCCESS | rc=0 >>
hello world
3、copy模块

copy模块用于复制指定主机文件到远程主机的指定位置

bash 复制代码
[root@centos01 ~] ansible web -m copy -a "src=/etc/hosts 
dest=/root/a1.hosts mode=777 owner=root group=root"
<!--/将本机的hosts文件复制到web组中的所有主机上存放在家目录下的a1.hosts目录,
权限是777,属主是root,属组是root-->
4、hostname模块

hostname模块用于管理远程主机上的主机名

bash 复制代码
[root@centos01 ~] ansible 192.168.100.20 -m hostname -a "name=test"
<!--将192.168.100.20的主机名改为test,
但是192.168.100.20需要敲一下bash才生效-->
5、service模块

service模块为用来管理远程主机上的服务的模块

bash 复制代码
[root@centos01 ~] ansible web -m service -a "name=httpd 
enabled=yes state=restarted"
<!--设置httpd服务重新启动和开机自动启动 相当于systemctl restart httpd-->
6、user模块

user模块主要用于管理远程主机上的用户账号

bash 复制代码
[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"
<!--在web组的所有主机上新建一个系统用户,UID为502,
属组是root,名字是user01,密码是pwd@123-->

相关参考中文官网:Ansible中文权威指南- 国内专业的Ansible中文官方学习手册

https://www.cnblogs.com/-wenli/p/13292855.html

shell和ansible自动化运维实例_ansible playbook执行shell脚本_一个高效工作的家伙的博客-CSDN博客

相关推荐
tyatyatya1 天前
Ansible自动化配置,从入门到实战
运维·自动化·ansible
lbb 小魔仙2 天前
【Linux】Ansible 自动化运维实战:2000+ 节点配置标准化教程
linux·运维·ansible
扑火的小飞蛾5 天前
【Ansible学习笔记01】 批量执行 shell 命令
笔记·学习·ansible
oMcLin5 天前
如何在 Red Hat Linux 服务器上使用 Ansible 自动化部署并管理多节点 Hadoop 集群?
linux·服务器·ansible
linux修理工8 天前
vagrant ubuntu 22.04 ansible 配置
ubuntu·ansible·vagrant
biubiubiu07069 天前
Ansible自动化
运维·自动化·ansible
秋42710 天前
ansible配置与模块介绍
ansible
秋42710 天前
ansible剧本
linux·服务器·ansible
码农101号11 天前
Ansible - Role介绍 和 使用playbook部署wordPress
android·ansible
2301_8000509913 天前
Ansible
运维·ansible