--- title: "Ansible" date: 2019-10-30T11:38:39+08:00 lastmod: 2019-10-30T11:38:39+08:00 tags: ["ansible"] categories: ["dev/ops"] --- ## CentOS7 安装 ansible ```bash yum install ansible ``` ## 修改配置文件 /etc/ansible/ansible.conf ``` ## 取消 ssh key 验证 host_key_checking = False ## 改用 debug 输出模式 stdout_callback = debug ``` ## 修改配置文件 hosts ``` ## 增加 tomcat 服务器组 [tomcat] tomcat101 ansible_ssh_host=192.168.1.101 tomcat102 ansible_ssh_host=192.168.1.102 tomcat103 ansible_ssh_host=192.168.1.103 ## 增加 mysql 服务器组 [mysql] mysql201 ansible_ssh_host=192.168.1.201 ansible_ssh_pass=111111 mysql202 ansible_ssh_host=192.168.1.202 mysql203 ansible_ssh_host=192.168.1.203 ## 全局设置全部服务器的默认设置 [all:vars] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456 ``` ## authorized_key 模块 ```bash ## 分发密钥 ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}'" ``` ## 简单使用 - 批量设置主机名为资产名 ```bash ansible all -m shell -a 'hostnamectl set-hostname {{inventory_hostname}}' ansible all -m shell -a 'echo "{{ansible_ssh_host}} {{inventory_hostname}}" >> /etc/hosts' ``` ## ansible 常用 roles - [https://gitee.com/colben/ansible](https://gitee.com/colben/ansible)