一、Use Ansible-2.9.27 Modular on openEuler
1 地址
2 ad-hoc 概述
ad-hoc是临时命令,执行完就结束了,可以执行简单的任务。如:临时批量查看被控制的机器版本进程拷贝等等。
命令格式:ansible iyuyixyz -m command -a 'cat /etc/openEuler-release'
格式说明:命令 主机名称 指定模块 模块名称 模块动作 执行命令
返回结果的颜色:绿色,黄色,红色,粉色。
3 ad-hoc模式的模块有
command
shell
scripts
yum
yum_repository
copy
file
mount
service
cron
iptables
firewalld
get_url
二、常用模块
1 command 模块
不支持管道及特殊符号,需使用 shell 模块
[root@manage ~]# ansible 10.0.1.51 -a 'df -h'
##
[root@manage ~]# ansible 10.0.1.51 -a 'ip a |grep ens33'
[WARNING]: Platform linux on host 10.0.1.51 is using the discovered Python interpreter at
/usr/bin/python3, but future installation of another Python interpreter could change this.
See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for
more information.
10.0.1.51 | FAILED | rc=255 >>
Command "|grep" is unknown, try "ip address help".non-zero return code
2 shell 模块
##
[root@manage ~]# ansible 10.0.1.51 -m shell -a 'ip a |grep ens33'
[WARNING]: Platform linux on host 10.0.1.51 is using the discovered Python interpreter at
/usr/bin/python3, but future installation of another Python interpreter could change this.
See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for
more information.
10.0.1.51 | CHANGED | rc=0 >>
3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 10.0.1.51/24 brd 10.0.1.255 scope global noprefixroute ens33
3 script模块
## 管理节点上创建一个脚本
cat > /opt/xyz.sh << EOF
touch /opt/xyz.txt
EOF
##
chmod +x /opt/xyz.sh
##
[root@manage ~]# ansible 10.0.1.51 -m script -a '/opt/xyz.sh'
10.0.1.51 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 10.0.1.51 closed.\r\n",
"stderr_lines": [
"Shared connection to 10.0.1.51 closed."
],
"stdout": "",
"stdout_lines": []
}
## 测试
[root@manage ~]# ansible 10.0.1.51 -m shell -a 'ls -l /opt'
[WARNING]: Platform linux on host 10.0.1.51 is using the discovered Python interpreter at
/usr/bin/python3, but future installation of another Python interpreter could change this.
See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for
more information.
10.0.1.51 | CHANGED | rc=0 >>
total 16
drwxr-xr-x. 2 rsyncxyz rsyncxyz 4096 Nov 24 14:31 backupxyz
drwxr-xr-x 3 root root 4096 Nov 25 22:19 nfs
drwxr-xr-x. 2 root root 4096 Nov 23 15:48 software
-rw-r--r-- 1 root root 0 Nov 27 17:10 xyz.txt
4 yum 模块
yum 模块参数 | 说明 |
name | 指定安装软件名字,可以安装多个软件 |
state | 状态present 或者 installed 安装,absent或者removed删除,latest更新 |
exclude | 安装软件时,可以排除 |
enablerepo | 安装软件时临时开启被关闭的yum源 |
## 下载并安装
ansible 10.0.1.51 -m yun 'name=https://repo.openeuler.org/openEuler-22.09/everything/x86_64/Packages/httpd-2.4.51-11.oe2209.x86_64.rpm state=installed'
## 安装最新的httpd
ansible 10.0.1.51 -m yun 'name=httpd state=latest'
## 删除
ansible 10.0.1.51 -m yun 'name=tree state=removed'
X、One Step Success
1 帮助命令
## 查询模块
ansible-doc -l
## 查询某个模块
ansible-doc -s ping
## 查询有多少模块
ansible-doc -l | wc -l
Y、Error message
Z、Related Links
Installing Ansible-2.9.27 Inventory Use on openEuler:https://www.cnblogs.com/huaxiayuyi/p/16928621.html
标签:10.0,Use,27,##,Modular,ansible,模块,1.51,root From: https://www.cnblogs.com/huaxiayuyi/p/16929863.html