day49——Ansible 自动化配置管理

Ansible 自动化配置管理

安装 配置 启动 nginx redhat红帽 ( 收购 ansible -->Ansible自动化运维配置管理专家)

Ansible课程大纲

1.什么是ansible? 可以通过一个命令行完成一系列的操作。

2.ansible 优点 特点?

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

ansible执行流程.png

4.ansible 配置文件 优先级?

在运行Ansible命令时,命令将会按照预先设定的顺序查找配置文件,如下所示:

  1. ANSIBLE_CONFIG: 首先,Ansible命令会检查环境变量,及这个环境变量指向的配置文件
  2. ./ansible.cfg: 其次,将会检查当前目录下的ansible.cfg配置文件
  3. ~/ansible.cfg: 再次,将会检查当前用户home目录下的.ansible.cfg配置文件
  4. /etc/ansible/ansible.cfg: 最后,将会检查再用软件包管理工具安装Ansible时自动产生的配置文件
ANSIBLE_CONFIG
ansible.cfg                #当前项目目录中
.ansible.cfg               #当前执行用户的家目录 
/etc/ansible/ansible.cfg
[root@manager ~]# cat /etc/ansible/ansible.cfg 
#inventory      = /etc/ansible/hosts      #主机列表配置文件
#library        = /usr/share/my_modules/  #库文件存放目录
#remote_tmp     = ~/.ansible/tmp          #临时py文件存放在远程主机目录
#local_tmp      = ~/.ansible/tmp          #本机的临时执行目录
#forks          = 5                       #默认并发数
#sudo_user      = root                    #默认sudo用户
#ask_sudo_pass = True                     #每次执行是否询问sudo的ssh密码
#ask_pass      = True                     #每次执行是否询问ssh密码
#remote_port    = 22                      #远程主机端口
host_key_checking = False                 #跳过检查主机指纹
log_path = /var/log/ansible.log           #ansible日志

普通用户需要配置提权

[root@manager ~]# cat /etc/ansible/ansible.cfg 
[privilege_escalation]   #如果是普通用户则需要配置提权
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False

测试优先级

#优先级最高
[root@manager ~]# export ANSIBLE_CONFIG="/tmp/ansible.cfg"
[root@manager ~]# touch /tmp/ansible.cfg

[root@manager ~]# mkdir /project1
[root@manager ~]# cd /project1/
[root@manager project1]# touch ansible.cfg
[root@manager project2]# ansible --version
ansible 2.8.5
config file = /project1/ansible.cfg

[root@manager /]# mkdir /project2
[root@manager /]# cd /project2/
[root@manager project2]# touch ansible.cfg
[root@manager project1]# ansible --version
ansible 2.8.5
config file = /project2/ansible.cfg

[root@manager tmp]# touch ~/.ansible.cfg
[root@manager tmp]# ansible --version
ansible 2.8.5
config file = /root/.ansible.cfg

/etc/ansible/ansible.cfg

5.ansible inventory主机清单?

#1.基于IP地址+密码的方式
2  [webservers]
3 172.16.1.7 ansible_ssh_user='root' ansible_ssh_pass='123456'
4 172.16.1.8 ansible_ssh_user='root' ansible_ssh_pass='123456'
#2.场景二、基于密钥连接,需要先创建公钥和私钥,并下发公钥至被控端
#1.创建一对密钥   公钥+私钥 ==配套
[root@manager ~]# ssh-keygen -C manager@qq.com               #一路回车
#2.将管理机的公钥推送至web服务器上   ( 需要输入对端服务器的密码  )
[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.场景三、主机组使用方式
[lbservers] #定义lbservers组
172.16.1.5
172.16.1.6
[webservers] #定义webserver组
172.16.1.7
172.16.1.8
[servers:children] #定义servers组包括两个子组 [lbservers,webserver]
lbservers
webserver
[root@manager project1]# ansible webservers --list-hosts -i hosts
hosts (2):
172.16.1.7
172.16.1.8

2 Ansible Ad-Hoc

2.1ansible ad-Hoc? 单条命令和模块

ansible命令格式.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        #排除使用哪个仓库
#1.安装最新的httpd服务

[root@manager project1]# ansible webservers -m yum -a "name=httpd state=latest disablerepo=webtatic-php" -i hosts

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

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

#4.通过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
24 - name: install nginx rpm from a local file (软件包必须在被控端主机)[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          #备份

#将当前目录下的的./file/ansible.oldxu.com.conf推送
[root@manager project1]# ansible webservers -m copy -a "src=./file/ansible.oldxu.com.conf dest=/etc/nginx/conf.d/ansible.oldxu.com.conf owner=root group=root mode=644" -i hosts
#将当前目录下的的./file/ansible.oldxu.com.conf推送,并缓存
[root@manager project1]# ansible webservers -m copy -a "src=./file/ansible.oldxu.com.conf dest=/etc/nginx/conf.d/ansible.oldxu.com.conf owner=root group=root mode=644 backup=yes" -i hosts 

5.service|systemd

state
started         #启动
stopped         #停止
restarted       #重启
reloaded        #重载
enabled         #是否开机自启
    yes         #是
    no          #否
#重启nginx
[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/ansible state=directory mode=755 owner=www group=www" -i hosts
#准备站点代码
[root@manager project1]# ansible webservers -m copy -a "src=./file/index.html dest=/code/ansible/index.html owner=www group=www mode=644" -i hosts
#删除
[root@manager project1]# ansible webservers -m file -a "path=/code/ansible state=absent" -i hosts

7.user group

#group 整数int 小数flot  字符str 真|假bool
创建组
[root@manager project1]# ansible webservers -m group -a "name=www gid=666 state=present" -i hosts
#user
name            #名称
uid             #uid
group           #组名或gid
create_home     #是否创建家目录
system          #是否作为系统组
shell           #指定登录shell
state           #动作
   present     #创建
   absent      #删除
remove          #递归 (相当于userdel -r ...)
groups          #附加组
append          #如果是yes,就是给这个用户添加一个组
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

# 正常用户 oldxu 1000 1000 /bin/bash  /home/oldxu
[root@manager project1]# ansible webservers -m user -a "name=oldxu" -i hosts

# 移除oldxu用户,并删除家目录所有内容.
[root@manager project1]# ansible webservers -m user -a "name=oldxu 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@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zrlog 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               #月
weekday             #周

[root@manager project1]# ansible webservers -m cron -a 'name=test_job minute=00 hour=02 job="/bin/bash /server/scripts/client_to_data_server.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="/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" zone=trusted state=enabled permanent=yes' -i hosts
#针对富规则rich_rule
[root@manager project1]# ansible webservers -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.1/32 service name=http accept" state=enabled' -i hosts

11.selinux

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

推荐阅读更多精彩内容