ansbile安装与使用

安装

ansible安装

yum -y install ansible

修改配置文件

[root@shuan-node0150 ~]# cat /etc/ansible/ansible.cfg | egrep -v "^#|^$"
[defaults]
host_key_checking = False
remote_user = root

修改主机组

cat /etc/ansible/host | egrep -v "^#|^$"
...
[tmp]
192.168.1.151
[nfs]
192.168.1.90
[bak]
192.168.1.151
[zabbix]
192.168.1.150
[other]
192.168.1.16
192.168.1.38
192.168.1.40
192.168.1.41
192.168.1.50
192.168.1.51
192.168.1.60
192.168.1.70
192.168.1.71
192.168.1.72
192.168.1.120
192.168.1.123
192.168.1.127
192.168.1.132
192.168.1.135
192.168.1.191
[k8s]
192.168.1.30
192.168.1.31
192.168.1.32
192.168.1.52
192.168.1.61
192.168.1.80
192.168.1.81
192.168.1.82
192.168.1.83
192.168.1.84
192.168.1.85
192.168.1.86
[allhost:children] ##执行以下的组
k8s
nfs
other

测试连通性

ansible all -m ping

使用

ansible-doc查询模块说明

ansible-doc -s 模块名

示例:

ansible-doc -s service
ansible-doc -s shell
ansible-doc -s template
ansible-doc -s systemd

ansible-playbook

模板文件config_node_exporter.yml

---
- hosts: allhost #执行任务的主机
  remote_user: root #执行任务的账号
  tasks:
  - name: Config node_exporter service
    lineinfile:
      path: /etc/systemd/system/node_exporter.service #需要修改的文件
      regexp: '^ExecStart'  #正则匹配行
      line: ExecStart=/usr/bin/node_exporter --collector.disable-defaults --collector.cpu --collector.diskstats --collector.filesystem --collector.loadavg --collector.meminfo --collector.netclass --collector.netdev --collector.netstat --collector.uname --collector.time --collector.stat --web.listen-address=:9101  #替换后的内容
  - name: reload service
    shell:
      cmd: systemctl daemon-reload #重新加载服务
  - name: restart node_exporter
    service:
      name: node_exporter
      state: restarted

查看执行的主机

ansible-playbook config_node_exporter.yml --list-hosts

执行任务

ansible-playbook config_node_exporter.yml

常用模块

template

示例:

src: 控制主机端模板文件,该文件中可以配置ansible变量达到各个主机配置不一致的效果

​ ansible_all_ipv4_addresses:通过该变量设置IP,变量在文件里通过{{ ansible_all_ipv4_addresses }}引用

​ ansible_hostname: 通过该变量设置主机名,变量在文件里通过{{ ansible_hostname }}引用

dest: 被控主机端配置文件

  - name: Config zabbix agent #自定义任务名称
    template: src=/root/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf

service模块

示例:

  - name: start and enable zabbix-agent #自定义任务名称
    service:
      name: zabbix-agent  #服务名
      state: started  #启动
      enabled: yes    #设置开启自启

shell模块

示例:

  - name: reload service
    shell:
      cmd: systemctl daemon-reload #重新加载服务

setup模块

ansible all -m setup 
查看所有全局变量,可搭配grep命令过滤
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值