美文网首页
Day37 Ansible基础知识

Day37 Ansible基础知识

作者: a幕城 | 来源:发表于2019-10-09 20:03 被阅读0次

1.什么是ansible? 可以通过一个命令行完成一系列的操作
2.ansible基础架构?控制端 被控端inventory ad-hoc playbook连接协议

image.png
3.`ansible 配置文件的优先级`
   1.ANSIBLE_CONFIG 
   2.ansible.cfg                               #当前项目目录中
   3.ansible.cfg                               #当前执行用户的家目录
[root@manager ~]#export  ANSIBLE_CONFIG="/tmp/ansible.cfg"
[root@manager ~]#touch /tmp/ansible.cfg
[root@manager ~]#mkdir /project1
[root@manager ~]#cd /projectl
[root@manager project1]#touch ansible.cfg
[root@manager ~]#ansible --version 
ansible 2.8.5
    config file=/projectl/ansblie.cfg
[root@manager ~]#mkdir /project2
[root@manager ~]#cd /project2/
[root@manager project2]#touch ansblie.cfg
[root@manager project2]#ansblie --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
`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-ccpy-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]   # 定义webservers组
172.16.1.7
172.16.1.8
[server:children] #定义servers组包括俩个子组
[lbservers,webservers]
lbservers
webserver
[root@manager project1]# ansible webservers - -list -hosts  -i  hosts
  hosts (2)
        172.16.1.7
        172.16.1.8

Ansible Ad-Hoc

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

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

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

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=webtaticphp" -i hosts
#3.安装httpd指定从按个仓库安装
- name: install the latest version of Apache from
the testing repo
[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
- 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

src #本地路径,可以是相对,可以是绝对
dest #目标位置
owner #属主
group #属组
mode #权限
backup #备份
[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
[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
state
started #启动
stopped #停止
restarted #重启
reloaded #重载
enabled #是否开机自启
yes #是
no #否
[root@manager project1]# ansible webservers -m
systemd -a "name=nginx state=restarted enabled=yes"
-i hosts

#创建 /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
.user group
#group 整数int 小数 flot dasdsa 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
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
# 正常用户 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/Afa
MomYzR1QZYtccwmJcUt8VpLq4D055UCCX4MlwgePOP80ZRwhppv
BF72RIAVi/"' -i hosts

#提前准备好nfs服务端
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zrlog 172.16.1.0/24
/data/zh 172.16.1.0/24
/data/edu 172.16.1.0/24

9.cron
/data/blog 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
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_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来源
#针对rule

[root@manager project1]# ansible webservers -m
selinux -a "state=disabled" -i hosts

相关文章

  • Day37 Ansible基础知识

    1.什么是ansible? 可以通过一个命令行完成一系列的操作2.ansible基础架构?控制端 被控端...

  • ansible基础知识

    一、ansible简介 1、ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(pu...

  • Ansible基础知识

    Ansible主配置文件:/etc/ansible/ansible.cfgInventory配置文件:/etc/a...

  • Ansible基础知识

    首先我们看下ansible的配置文件(有删减): [root@www ~]# cat /etc/ansible/h...

  • 【Ansible学习】- Ansible初探

    安装 Ansible基础 Ansible架构图 Ansible核心组件说明 Ansible:Ansible的核心程...

  • 快速上手 Ansible

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 命令

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 主机清单配置文件

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible Playbook 剧本语法

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible(一)、实现SSH免密

    1.ansible安装 2.ansible配置 /etc/ansible/ansible.cfg /etc/ans...

网友评论

      本文标题:Day37 Ansible基础知识

      本文链接:https://www.haomeiwen.com/subject/ugbgpctx.html