返回

ansible使用技巧

   
摘要GPT
摘要小助理暂时失联跑路啦……😜

官方文档https://docs.ansible.com/ansible/latest/installation_guide/index.html

项目:https://github.com/ansible/ansible

安装ansible

在Ubuntu上安装Ansible

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

在Debian上安装Ansible

先复制对应版本的 UBUNTU_CODENAME

Debian 12   jammy  #jammy为UBUNTU_CODENAME

Debian 11   focal  #focal为UBUNTU_CODENAME

Debian 10   bionic #bionic为UBUNTU_CODENAME

然后

sudo apt install wget -y

最后

UBUNTU_CODENAME=jammy
wget -O- "https://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=get&search=0x6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367" | sudo gpg --dearmour -o /usr/share/keyrings/ansible-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/ansible-archive-keyring.gpg] http://ppa.launchpad.net/ansible/ansible/ubuntu $UBUNTU_CODENAME main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt update && sudo apt install ansible

使用pip安装和升级Ansible

确保 pip 可用,要验证是否已为首选Python安装了 pip :

python3 -m pip -V

如果你看到像 No module named pip 这样的错误

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user

安装Ansible

python3 -m pip install --user ansible

升级Ansible

python3 -m pip install --upgrade --user ansible

管理Ansible配置文件

📜第一层 配置文件优先级

Ansible配置文件ansible.cfg,可以存在于多个位置,他们的被读取的顺序如下

ANSIBLE_CONFIG                   # (环境变量)
ansible.cfg                      # (当前目录)
.ansible.cfg                     # (用户家目录)
/etc/ansible/ansible.cfg         # (默认配置文件)

📜第二层 配置选项

查看ansible.cfg的配置项

grep "^\[" /etc/ansible/ansible.cfg

配置项

[defaults]                     # 默认常用配置
[inventory]                    # 主机清单插件
[privilege_escalation]         # 用于提权
[paramiko_connection]          # python paramiko模块的连接设置(默认使用SSH)
[ssh_connection]               # SSH连接设置
[persistent_connection]        # 长连接设置
[accelerate]                   # 加速模式的配置
[selinux]                      # selinux设置
[colors]                       # 输出结果颜色的设置
[diff]                         # 输出不同的设置

📜第三层 常用配置选项解读

虽然ansible.cfg配置文件一堆配置选项参数,但常用的,就那么几个

1、常用配置

[defaults]

inventory =/etc/ansible/hosts
forks=5
remote_user=
host_key_checking=False
ask_pass=False
module_name=

解释

[defaults]                ; 默认设置部分开始

inventory = /etc/ansible/hosts  ; 指定清单文件路径

forks = 5                ; 并行执行任务的进程数

remote_user =            ; 远程用户,默认为空

host_key_checking = False ; 主机密钥检查设置为禁用,不检查主机密钥

ask_pass = False         ; 是否要求输入密码设置为禁用

module_name =            ; 模块名称,默认为空

2、用于提权

[privilege_escalation]

become=True
become_method=sudo
become_user=root
become_ask_pass=False

解释

[privilege_escalation]   ; 特权升级设置部分开始

become = True             ; 启用特权升级功能

become_method = sudo      ; 使用sudo进行特权升级

become_user = root        ; 指定特权升级后的用户为root

become_ask_pass = False  ; 是否需要sudo密码

实验开始

以主机名做为访问互联

sudo vim /etc/hosts #可选,为了方便可以不输入ip

例如:

192.168.159.111 servera 
192.168.159.112 serverb 
192.168.159.113 serverc 
192.168.159.114 serverd

📑编写配置文件

sudo vim /etc/ansible/ansible.cfg

内容如下:

[defaults]
inventory =/etc/ansible/hosts
forks=5
remote_user=
host_key_checking=False
ask_pass=False
module_name=

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

📑编写主机清单

sudo vim /etc/ansible/hosts

格式

#可以是ip地址,使用主机名是为了方便
[web]
xx.xx.xx.com
192.168.1.11
192.168.1.28
node3 

#children表示包含若干组
[everyone:children]
web

自定义参数

192.168.1.11 ansible_port=6871 ansible_user=root ansible_ssh_pass=123

解释:

ansible_host #主机地址

ansible_port #端口,默认是22端口

ansible_user #认证的用户

ansible_ssh_pass  #用户认证的密码

查看清单

cat /etc/ansible/hosts

📑验证主机清单

ansible all --list-hosts #查看主机
ansible all -m ping  #连通性检查

测试命令:

ansible web -m command -a "hostname"

提权测试

添加账户xiao1

ansible web -m command -a 'useradd xiao1'

删除账户xiao1

ansible web -m command -a 'userdel -r xiao1'

密钥连接

生成密钥对

ssh-keygen -t rsa -P '' -q -f ~/.ssh/id_rsa

复制公钥到被控主机

主机数量少,可用以下命令,假设ssh端口是6871,ip是 8.8.8.8

ssh-copy-id -i ~/.ssh/id_rsa.pub -p 6871 root@8.8.8.8

登录测试

ssh -p 6871 root@8.8.8.8

默认端口是22

ssh-copy-id -i ~/.ssh/id_rsa.pub root@8.8.8.8

主机名是node1,node2,node3,有规律的,使用以下命令,

for host in node{1..3}; do ssh-copy-id -i ~/.ssh/id_rsa.pub $host; done

单独复制

ssh-copy-id -i ~/.ssh/id_rsa.pub node2

登录测试

ssh node2

或者指定用户名:vagrant

ssh-copy-id -i ~/.ssh/id_rsa.pub vagrant@node2

主机数量多

下载脚本

wget https://gitee.com/dayu777/open_shell/raw/main/pub_send.py

编辑脚本

vim pub_send.py

运行脚本

python3 pub_send.py

被控主机无需输入sudo密码

每次 sudo 不用输入密码

sudo visudo

末尾添加

laoge ALL=(ALL) NOPASSWD: ALL  #laoge 为用户名

CTRL+x保存,y 确认,回车


知识共享许可证 CC BY-NC-SA 4.0
最后更新于 2024-07-17 18:28
使用 Hugo 构建
主题 hugo-magic小洋葱 魔改 由 Jimmy 设计
Written by Human, Not by AI