一,Ansible各模块用法简介
ansible web_servers -i inventory.ini -a "echo hello | grep -o e' "
hello | grep e
ansible web_servers -i inventory.ini -m shell -a "echo hello | grep -o 'e' "
script:管理脚本
创建
执行
严重
vim a.sh
#!/bin/bash
touch /tmp/testfile
:wq
ansible web_servers -i inventory.ini -m script -a "/root/a.sh"
copy:拷贝模块
拷贝文件
src:源地址
dest:目的地址
backup:发生变化,则对备份原文件
owner:拷贝文件所有者
group:所属组
mode:权限
例:拷贝nginx.repo到被管理节点
ansible web_servers -i inventory.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/"
例:拷贝nginx.repo并作备份
ansible web_servers -i inventory.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/ ba=yes"
例:copy并修改用户组
ansible web_servers -i inventory.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/ own=jpz group=jpz"
例:copy并设置权限
ansible web_servers -i inventory.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/ mode=777"
yum_repository:添加yum仓库
对.repo里的内容修改
name:[]中的名
description
baseurl:地址
file:文件名,不包含.repo,默认为name.repo
state preset 添加仓库文件,absent删除仓库文件
gpgcheck:
例:添加yum原
ansible web_servers -i inventory.ini -m yum_repository -a "name=epel baseurl=http://www.baidu.com description='epel yum' state=present"
例:删除yum源,只能删除ansible添加的yum源
ansible web_servers -i inventory.ini -m yum_repository -a "name=epel state=absent"
例:
yum模块
name:软件包名
state:present安装但不升级,
installed确认安装
latest安装并升级
removed确认移除
例:被管理节点安装vim
ansible web_servers -i inventory.ini -m yum -a "name=vim state=installed"
例:被管理节点删除vim
ansible web_servers -i inventory.ini -m yum -a "name=vim state=removed"
例:安装软件包组
ansible web_servers -i inventory.ini -m yum -a "name=@de ' state=installed"
systemd启动服务模块
daemon_reload 重载systemd扫面变动单元
enabled 是否开机自启动yes|no
state启动停止重启等
name服务名
例:停止服务
ansible web_servers -i inventory.ini -m systemd -a "name=httpd state=stopped"
group模块,对组管理
name组名
system是否为系统组,默认no
state:present创建 absent删除
user模块,对用户管理
name用户名
password默认不设置
例:
pass=$(echo 000000|openssl passwd -1 -stdin)
echo $pass
ansible web_servers -i inventory.ini -m user -a "name=zs password=${pass}"
例:密钥对
ansible web_servers -i inventory.ini -m user -a "name=jpz generate_ssh_key=yes ssh_key_type=ecdsa"
例:有效期,加入组,不改变原组,
-a "name=jpz expires=$(date +%s -d 20220501) groups=db_group append=yes"
二,ansible模块实操
1、安装ansible并查看其配置文件
yum -y install ansible
cat /etc/ansible/ansible.cfg
cat /etc/ansible/hosts
二、建立管理节点与被管理节点的SSH 信任关系
ssh-keygen
ssh-copy-id 192.168.100.61
三、案例题
1、在管理节点上,测试与所有被管理节点的⽹络连通性。
ansible jpz_web -i jpzhosts.ini -m "ping"
2、创建资产管理列表,并列出其中一组资产。
cat jpzhosts.ini
ansible jpz_web -i jpzhosts.ini --list-hosts
3、求列表中两个组的并集、交集、差集。
cat jpzhosts.ini
交集
ansible 'jpz_web:&jpz_server' -i jpzhosts.ini --list-host
差集
ansible 'jpz_server:!jpz_web' -i jpzhosts.ini --list-host
并集
四、练习题
例1:在被管理主机执行使用command模块和shell模块执行echo hello
命令。
ansible jpz_web -i jpzhosts.ini -a "echo hello"
ansible jpz_web -i jpzhosts.ini -m shell -a "echo hello"
例2:在被管理主机执行使用command模块和shell模块执行 echo
'hello'|grep -o 'e'命令,查看效果。理解两个模块的区别。
ansible jpz_web -i jpzhosts.ini -m command -a "echo hello | grep -o 'e'"
ansible jpz_web -i jpzhosts.ini -m shell -a "echo hello | grep -o 'e'"
例3:管理节点上的⼀个脚本。
ansible jpz_web -i jpzhosts.ini -m script -a "/root/mkdir.sh"
被管理节点查看
ll /tmp/
例4:copy 管理节点上的 nginx.repo 到被管理节点上。
cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever
/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$r
eleasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
命令:
ansible jpz_web -i jpzhosts.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/"
被管理节点查看
ll /etc/yum.repos.d/
例5:copy 前, 在被管理节点上对原⽂件进⾏备份
管理节点,修改一下文件内容
echo "jpzjpzwqdj">> nginx.repo
ansible jpz_web -i jpzhosts.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/ backup=yes"
被管理节点查看
例6:copy ⽂件的同时对⽂件进⾏⽤户及⽤户组设置
ansible jpz_web -i jpzhosts.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/ owner=nobody group=nobody"
被管理节点查看
例7:copy ⽂件的同时对⽂件进⾏权限设置
ansible jpz_web -i jpzhosts.ini -m copy -a "src=/root/nginx.repo dest=/etc/yum.repos.d/ mode=777"
被管理节点查看
例8:添加 epel 源
ansible jpz_web -i jpzhosts.ini -m yum_repository -a "name=epel baseurl=http://www.baidu.com state=present description='epel yum'"
被管理节点查看
例9:删除 epel 源
ansible jpz_web -i jpzhosts.ini -m yum_repository -a "name=epel baseurl=http://www.baidu.com state=absent description='epel yum'"
被管理节点查看
例10:安装⼀个软件包
ansible jpz_web -i jpzhosts.ini -m yum -a "name=vim state=installed"
例11:移除⼀个软件包
ansible jpz_web -i jpzhosts.ini -m yum -a "name=vim state=removed"
例12:安装⼀个软件包组
ansible jpz_web -i jpzhosts.ini -m yum -a "name='@Development tools' state=installed"
例13:重新加载 system
ansible jpz_web -i jpzhosts.ini -m systemd -a "daemon_reload=yes"
例14:启动 Nginx 服务
ansible jpz_web -i jpzhosts.ini -m systemd -a "name=nginx state=started"
例15:关闭 Nginx 服务
ansible jpz_web -i jpzhosts.ini -m systemd -a "name=nginx state=stopped"
例16:重启 Nginx 服务
ansible jpz_web -i jpzhosts.ini -m systemd -a "name=nginx state=restarted"
例17:重新加载 Nginx 服务
ansible jpz_web -i jpzhosts.ini -m systemd -a "name=nginx daemon_reload=yes"
例18:将 Nginx 服务设置开机⾃启动
ansible jpz_web -i jpzhosts.ini -m systemd -a "name=nginx enabled=yes"
例19:创建普通组 db_admin
ansible jpz_web -i jpzhosts.ini -m group -a "name=db-jpz state=present"
例20:创建⽤户并设置密码先⽣成加密密码
pass=$(echo 000000|openssl passwd -1 -stdin)
echo $pass
例21:执⾏ ansible 命令 创建⽤户 foo 并设置密码
ansible jpz_web -i jpzhosts.ini -m user -a "name=jpz password=${pass}"
例22:创建⽤户 yangge, 并且为其创建密钥对,并且密钥
类型为: ecdsa
ansible jpz_web -i jpzhosts.ini -m user -a "name=jpz1 generate_ssh_key=yes ssh_key_type=ecdsa"
例23:创建⽤ 户tom, 并且设置其有效期到 2020年4⽉15⽇,
加⼊到组db_admin 中, 不改变⽤户原有加入的组。
ansible jpz_web -i jpzhosts.ini -m user -a "name=jpz2 expires=$(date +%s -d 20220501) groups=nobody append=yes"



