Ansible自动化配置管理

Ansible课程大纲

1.什么是ansible?

可以通过一个命令行完成一系列的操作

2.ansible优点,特点

3.ansible 基础架构? 控制端 被控端 inventory ad-hoc playbook 连接协


image.png

4..ansible 配置文件 优先级?

ANSIBLE_CONFIG
ansible.cfg         #当前项目目录中
.ansible.cfg        #当前执行用户的家目录
/etc/ansible/ansible.cfg

5..ansible inventory主机清单?

1.基于IP地址+密码的方式
[webservers]
172.16.1.7 ansible_ssh_user='root' ansible_ssh_pass='1'
172.16.1.8 ansible_ssh_user='root' ansible_ssh_pass='1'

2.基于密钥连接,需要先创建公钥和私钥,并下发公钥至被
控端
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub
root@172.16.1.7
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub
root@172.16.1.8
方式一、主机+端口+密钥
[root@manager ~]# cat hosts
[webservers]
172.16.1.7
172.16.1.8

3.主机组使用方式
定义webserver组
[webservers] 
172.16.1.7
172.16.1.8
定义servers组包括两个子组
[servers:children]
[lbservers,webserver]
lbservers
webserver

[root@manager project1]# ansible webservers --listhosts -i hosts
hosts (2):
172.16.1.7
172.16.1.8

Ansible Ad-Hoc

1.ansible ad-Hoc? 单条命令

image.png
command              执行命令 默认 不支持管道
shell 执行命令       支持管道
yum_reposity         yum仓库配置
yum yum              安装软件
get_url              和linux的wget一致
copy                 拷贝配置文件
service|systemd      启动服务
user
group
file                 创建目录 创建文件 递归授权
mount                挂载
cron                 定时任务
firewalld            防火墙
selinux              selinuix

1.command

ansible webservers -a "ps axu|grep nginx" -i hosts
#不支持管道(简单命令)

2.shell

ansible webservers -m shell -a "ps axu|grep nginx" - i hosts #支持管道

3.yum

state:
present   安装
absent   卸载
latest   最新
enablerepo   指定使用按个仓库
disablerepo   排除使用哪个仓库

安装最新的httpd服务
[root@manager project1]# ansible webservers -m yum -a "name=httpd state=latest disablerepo=webtaticphp" -i hosts

移除httpd服务
[root@manager project1]# ansible webservers -m yum -a "name=httpd state=absent disablerepo=webtaticphp" -i hosts

安装httpd指定从按个仓库安装
[root@manager project1]# ansible webservers -m yum -a "name=httpd state=latest enablerepo=testing" -i hosts

通过URL方式进行安装
[root@manager project1]# ansible webservers -m yum -a "name=https://mirrors.aliyun.com/zabbix/zabbix/3.0/ rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm state=present disablerepo=webtatic-php" -i hosts

通过软件包安装(软件包必须在被空端主机)
[root@manager project1]# ansible webservers -m yum -a "name=/root/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present disablerepo=webtatic-php" -i hosts

4.copy

src        本地路径,可以是相对,可以是绝对
dest       目标位置
owner      属主
group      属组
mode       权限
backup     备份
[root@manager project1]# ansible webservers -m copy -a "src=./dddddddd dest=/root/sss owner=root group=root mode=644" -i hosts
[root@manager project1]# ansible webservers -m copy -a "src=./dddddddd dest=/root/sss owner=root group=root mode=644 backup=yes" -i hosts

5.systemd

state:
      started           启动
      stopped           停止
      restarted         重启
      reloaded          重载
      enabled           是否开机自启
      yes               是
      no                否

[root@manager project1]# ansible webservers -m systemd -a "name=nginx state=restarted enabled=yes" -i hosts

6.file

创建 /code/ansible
path          路径
state
touch         创建文件
directory     创建目录
owner         属主
group         属组
mode          权限

准备站点目录
[root@manager project1]# ansible webservers -m file -a "path=/code/zhl state=directory mode=755 owner=www group=www" -i hosts
准备站点代码
[root@manager project1]# ansible webservers -m copy -a "src=./index.html dest=/code/zhl/ owner=www owner=www mode=644" -i hosts

7.user group

group
[root@manager project1]# ansible webservers -m group -a "name=zhl gid=1011 state=present" -i hosts

user
name               名称
uid                uid
group              组名或gid
create_home        是否创建家目录
system             是否作为系统组
shell              指定登录shell
state
        present
        absent
remove
groups
append
password

程序使用 www 666 666 /sbin/nologin /home
-->无
[root@manager project1]# ansible webservers -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin state=present" -i hosts

正常用户
[root@manager project1]# ansible webservers -m user -a "name=zhl" -i hosts

删除用户以及他的家目录
[root@manager project1]# ansible webservers -m user -a "name=zhl state=absent remove=yes" -i hosts

创建 other用户.有两个附加组root bin,创建家目录,指定登录
shell,设定密码123
#生成一个密码
ansible all -i localhost, -m debug -a "msg={{ '123' | password_hash('sha512', 'mysecretsalt') }}"
[root@manager project1]# ansible webservers -m user -a 'name=other groups="root,bin" create_home=yes shell=/bin/bash password="$6$mysecretsalt$gIIYs0Xgc7sSQkH.zKaz8/AfaMomYzR1QZYtccwmJcUt8VpLq4D055UCCX4MlwgePOP80ZRwhppvBF72RIAVi/"' -i hosts

8.mount

准备好nfs服务端
[root@manager project1]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/edu   172.16.1.0/24
/date/zrlog 172.16.1.0/24
/data       172.16.1.0/24
#用管理端操作被控端,让被控端挂载nfs存储数据
present       写入/etc/fstab
absent        卸载/etc/fstab
mounted      临时挂载
unmounted     卸载当前挂载

挂载过程中,如果目录不存在,则会创建该目录
[root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/zrlog path=/test_zrlog fstype=nfs opts=defaults state=mounted" -i hosts
[root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/zrlog path=/test_zrlog fstype=nfs opts=defaults state=unmounted" -i hosts

9.cron

minute     分
hour       时
day        日
month      月
week       周
job
创建定时任务
[root@manager project1]# ansible webservers -m cron -a 'name=test_job minute=00 hour=02 job="/bin/bash /server/scripts/client.sh &>/dev/null"' -i hosts
[root@manager project1]# ansible webservers -m cron -a 'name=test job="/bin/bash /server/scripts/test.sh &>/dev/null"' -i hosts

删除定时任务
[root@manager project1]# ansible webservers -m cron -a 'name=test_job minute=00 hour=02 job="/bin/bash /server/scripts/client.sh &>/dev/null" state=absent ' -i hosts
[root@manager project1]# ansible webservers -m cron -a 'name=test job="/bin/bash /server/scripts/test.sh &>/dev/null" state=absent' -i hosts

10.firewalld

[root@manager project1]# ansible webservers -m systemd -a "name=firewalld state=started" -i hosts
#针对服务
[root@manager project1]# ansible webservers -m firewalld -a "service=http state=enabled" -i hosts
#针对端口
[root@manager project1]# ansible webservers -m firewalld -a "port=9999/tcp state=enabled" -i hosts
#针对source来源
[root@manager project1]# ansible webservers -m firewalld -a "source=10.0.0.0/24 permanent=no state=enabled" -i hosts
#针对rule

11.selinux

关闭selinux
[root@manager project1]# ansible webservers -m selinux -a "state=disabled" -i hosts
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,227评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,755评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,899评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,257评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,617评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,757评论 1 221
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,982评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,715评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,454评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,666评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,148评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,512评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,156评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,112评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,896评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,809评论 2 279
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,691评论 2 272

推荐阅读更多精彩内容