ansible基础及搭建

自动化运维:ansible

_________________–
前提:装包
1.ansible_soft.tar.gz ansible.tar.gz lnmp_soft.tar.gz
[root@localhost /]# tar -xf lnmp_soft.tar.gz
[root@localhost /]# tar -xf ansible_soft.tar.gz
[root@localhost /]# tar -xf ansible.tar.gz


配置ssh秘钥
[root@control ~]# ssh-keygen -f /root/.ssh/id_rsa -N ‘’ N后面是密码
[root@control ~]# for i in node1 node2 node3 node4 node5

do
ssh-copy-id $i
done
Now try logging into the machine, with: “ssh ‘node5’”
and check to make sure that only the key(s) you wanted were added.


部署ansible软件(control主机操作.软件包在ansible_soft)
解压安装
修改ansible的配置文件 ansible.cfg
[root@control ansible_soft]# ls /etc/ansible/ansible.cfg #模板,;用来参考

[root@control ~]# mkdir ~/ansible 本身没有ansible目录,要自己在家目录下创建
[root@control ~]# cd ansible/       一定要进入ansible 才能使用SSH
[root@control ansible]# vim ansible.cfg

[defaults]
inventory = ~/ansible/hosts #被控制端主机清单
#forks = 5 #ssh并发数量
#ask_pass = True $使用秘钥还是密码 (密码
#host_key_checking = False #是否校验密钥 (不校验


创建控制端主机清单 # 参考 /etc/ansible/hosts
vim ~/ansible/hosts
[test] 主机组
node1 具体主机
[proxy] 代理服务器
node2
[webserver] web服务器
node[3:4]
[database] 数据库
node5
[cluster:children] 嵌套组
webserver
database

###################################################
使用`ansible,
方式一: 命令行执行 ad-hoc
方式二: 将要做的动作行为写入文件[playbook] ,ansible读取剧本自动完成相应的任务


方式一: 格式: ansible 主机集合 -m 模块名 -a 参数
其他参数 -k 使用密码远程 -i 指定主机列表文件
[root@control ansible]# ansible all --list-hosts
hosts (5):
node3
node4 all : 是ansible自带的已经定义好的特殊的组,所有的主机都在这个组
node5
node2
node1


ansible 的 [ping] 模块
[root@control ansible]# ansible node1 -m ping 一定要cd 到 /root/ansible
node1 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}


[root@control ansible]# ansible node1 -m command -a “uptime”
node1 | SUCCESS | rc=0 >>
16:38:27 up 5:05, 1 user, load average: 0.00, 0.01, 0.01


[root@control ansible]# ansible-doc -l | wc -l 列出所有模块
1378


shell 的功能比 command 模块的命令 更全 , 一旦退出.所有状态失效
[root@control ansible]# ansible test -m shell -a “ps aux | wc -l”
node1 | SUCCESS | rc=0 >>
80


[root@control ansible]# ansible test -m shell -a “chdir=/tmp touch my.txt”
#chdir 将目录切换到tmp , 然后创建my.txt


creates 文件名: 文件存在 不执行shell命令
removes 文件名 : 文件不存在 不执行shell命令

[root@control ansible]# ansible test -m shell -a “ssh-keygen -f ~/.ssh/id_rsa -N ‘’ creates=~/.ssh/id_rsa”
#如果已经有密钥文件id_rsa,则不创建密钥(skip跳过)


scripts 脚本模块
[root@control ansible]# ansible test -m script -a “./test.sh”

###############################
案例三 ansible ad-hoc 应用二


步骤1:file模块 : 可以创建文件\目录\链接;修改权限与属性 ,ansible 模块具有[幂等性]
#####幂等性:任意次执行所产生的影响均与一次执行的影响相同
1.ansible all -m file -a “path=/tmp/file.txt state=touch”  state =touch 创建文件
2.ansible all -m file -a “path=/tmp/mydir state=directory”    创建目录
3. ansible test -m file -a "path=/tmp/file.txt owner=sshd group=adm mode=0777"修改
4. state=absent              删除

步骤2.:copy 模块
[root@control ansible]# ansible test -m copy -a “src=~/a3.txt dest=/root/”
src 源   dest 目的
[root@control ansible]# ansible test -m copy -a “content=‘hello the world’ dest=/root/new.txt”      content 可以直接提供文件内容

步骤3 : fetch 模块  与copy 作用相反, 可以将文件拷贝到本地
[root@control ansible]# ansible test -m fetch -a “src=/etc/hostname dest=~/”

步骤四: lineinfile | replace 模块  #修改单个文件的单行内容
ansible test -m lineinfile -a “path=/etc/issue line=‘hello world’” 默认在最后一行添加

ansible test -m lineinfile -a “path=/etc/issue line=‘insert’ insertafter=‘Kernel’”  
                        在kernel后面添加
ansible test -m lineinfile  -a “path=/etc/issue regexp=‘hello’ line=‘ni hao’”
                         将hello 换成 ni hao
#lineinfile 可以替换一整行  replace 可以替换关键词
[root@control ansible]# ansible test -m replace -a “path=/etc/issue regexp=Kernel replace=Ocean”                 替换某一行
_____________________
案例四
步骤1.  user 模块
[root@control ansible]# ansible test -m user -a ‘name=tuser1’   创建系统账户
node1 | SUCCESS => {
“changed”: true,
“comment”: “”,
“createhome”: true,
“group”: 1001,
“home”: “/home/tuser1”,
“name”: “tuser1”,
“shell”: “/bin/bash”,
“state”: “present”,
“system”: false,
“uid”: 1001
}

__________
ansible test -m user -a ‘name=tuser2 uid=1010 group=adm groups=daemon,root home=/home/tuser2’              创建账户并设置对应的属性
_______
ansible test -m user -a “name=tuser1 password={{‘abc’| password_hash(‘sha512’)}}”
                       修改账户密码
[root@control ansible]# ansible test -m user -a “name=tuser1 groups=root,daemon”
                      修改user1账户的附加组
_________________
步骤二:  yum_repository模块
[root@control ansible]# ansible test -m yum_repository -a ‘name=myyum description=hello baseurl=ftp://192.168.4.254/centos gpgcheck=no’
baseurl = ftp://192.168.4.254/centos gpgcheck = 0 name = hello
ansible test -m yum_repository -a “name=myyum state=absent”
___________________
步骤三:  yum 模块
使用yum 安装 present  卸载 absent 升级 latest
ansible test -m yum -a “name=unzip state=present”


步骤四: service 模块
service 服务管理模块 (启动 关闭 重启)
started stopped restarted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值