|
| 1 | +# Ansible 安装和配置 |
| 2 | + |
| 3 | + |
| 4 | +## Ansible 说明 |
| 5 | + |
| 6 | +- Ansible 官网:<https://www.ansible.com/> |
| 7 | +- Ansible 官网 Github:<https://github.com/ansible/ansible> |
| 8 | +- Ansible 官网文档:<https://docs.ansible.com//> |
| 9 | +- 简单讲:它的作用就是把写 shell 这件事变成标准化、模块化。方便更好的自动化运维 |
| 10 | + |
| 11 | +## 安装 |
| 12 | + |
| 13 | +- 官网说明:<https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html> |
| 14 | +- CentOS:`sudo yum install -y ansible` |
| 15 | + - 查看版本:`ansible --version` |
| 16 | + |
| 17 | +------------------------------------------------------------------- |
| 18 | + |
| 19 | +## 配置基本概念 |
| 20 | + |
| 21 | +#### Ansible 基本配置文件顺序 |
| 22 | + |
| 23 | +- Ansible 执行的时候会按照以下顺序查找配置项,所以修改的时候要特别注意改的是哪个文件 |
| 24 | + |
| 25 | +``` |
| 26 | +ANSIBLE_CONFIG (环境变量) |
| 27 | +ansible.cfg (脚本所在当前目录下) |
| 28 | +~/.ansible.cfg (用户家目录下,默认没有) |
| 29 | +/etc/ansible/ansible.cfg(安装后会自动生成) |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | +#### 配置远程主机地址 (Ansible 称这些地址为 Inventory) |
| 34 | + |
| 35 | +- 假设我有 3 台机子: |
| 36 | + - 192.168.0.223 |
| 37 | + - 192.168.0.70 |
| 38 | + - 192.168.0.103 |
| 39 | +- 官网对此的配置说明:<https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#hosts-and-groups> |
| 40 | + |
| 41 | +###### 给这三台机子设置免密登录的情况(一般推荐方式) |
| 42 | + |
| 43 | +- 编辑 Ansible 配置文件:`vim /etc/ansible/hosts` |
| 44 | +- 添加如下内容 |
| 45 | + |
| 46 | +``` |
| 47 | +[hadoop-host] |
| 48 | +192.168.0.223 |
| 49 | +192.168.0.70 |
| 50 | +192.168.0.103 |
| 51 | +``` |
| 52 | + |
| 53 | +- 其中 `[hadoop-host]` 表示这些主机代表的一个组名 |
| 54 | + |
| 55 | + |
| 56 | +###### 如果不设置免密,直接采用账号密码(容易泄露信息) |
| 57 | + |
| 58 | + |
| 59 | +- 编辑 Ansible 配置文件:`vim /etc/ansible/hosts` |
| 60 | +- 添加如下内容 |
| 61 | + |
| 62 | +``` |
| 63 | +[hadoop-host] |
| 64 | +hadoop-master ansible_host=192.168.0.223 ansible_user=root ansible_ssh_pass=123456 |
| 65 | +hadoop-node1 ansible_host=192.168.0.70 ansible_user=root ansible_ssh_pass=123456 |
| 66 | +hadoop-node2 ansible_host=192.168.0.103 ansible_user=root ansible_ssh_pass=123456 |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +## 简单使用(`ad hoc`方式) |
| 72 | + |
| 73 | +- ad hoc 官网:<https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html> |
| 74 | + |
| 75 | +##### 运行 Ansible |
| 76 | + |
| 77 | +- 运行 Ansible 的 `ping` 命令,看看配置正确时输出如下: |
| 78 | + |
| 79 | +``` |
| 80 | +sudo ansible --private-key ~/.ssh/id_rsa all -m ping |
| 81 | +``` |
| 82 | + |
| 83 | +- 让远程所有主机都执行 `ps` 命令,输出如下 |
| 84 | + |
| 85 | +``` |
| 86 | +ansible all -a 'ps' |
| 87 | +``` |
| 88 | + |
| 89 | +- 让远程所有 hadoop-host 组的主机都执行 `ps` 命令,输出如下 |
| 90 | + |
| 91 | +``` |
| 92 | +ansible hadoop-host -a 'ps' |
| 93 | +``` |
| 94 | + |
| 95 | +------------------------------------------------------------------- |
| 96 | + |
| 97 | +## Playbook 脚本方式 |
| 98 | + |
| 99 | +- 官网:<https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html> |
| 100 | +- 一些语法:<https://docs.ansible.com/ansible/latest/modules/command_module.html> |
| 101 | +- playbook(剧本),顾名思义,就是需要定义一个脚本或者说配置文件,然后定义好要做什么。之后 ansible 就会根据 playbook 脚本对远程主机进行操作 |
| 102 | + |
| 103 | +#### 简单脚本 |
| 104 | + |
| 105 | +- 下面脚本让所有远程主机执行 `whoami` 命令,并把结果(当前用户名)输出到 `/opt/whoami.txt` 文件 |
| 106 | +- 创建脚本文件:`vim /opt/simple-playbook.yml` |
| 107 | + |
| 108 | +``` |
| 109 | +- hosts: all |
| 110 | + tasks: |
| 111 | + - name: whoami |
| 112 | + shell: 'whoami > /opt/whoami.txt' |
| 113 | +``` |
| 114 | + |
| 115 | +- 执行命令:`ansible-playbook /opt/simple-playbook.yml`,结果如下,并且 opt 下也有文件生成 |
| 116 | + |
| 117 | +``` |
| 118 | +PLAY [all] ************************************************************************************************************************** |
| 119 | +
|
| 120 | +TASK [Gathering Facts] ************************************************************************************************************** |
| 121 | +ok: [192.168.0.223] |
| 122 | +ok: [192.168.0.103] |
| 123 | +ok: [192.168.0.70] |
| 124 | +
|
| 125 | +TASK [whoami] *********************************************************************************************************************** |
| 126 | +changed: [192.168.0.103] |
| 127 | +changed: [192.168.0.223] |
| 128 | +changed: [192.168.0.70] |
| 129 | +
|
| 130 | +PLAY RECAP ************************************************************************************************************************** |
| 131 | +192.168.0.103 : ok=2 changed=1 unreachable=0 failed=0 |
| 132 | +192.168.0.223 : ok=2 changed=1 unreachable=0 failed=0 |
| 133 | +192.168.0.70 : ok=2 changed=1 unreachable=0 failed=0 |
| 134 | +``` |
| 135 | + |
| 136 | +------------------------------------------------------------------- |
| 137 | + |
| 138 | +## 平时用来测试 |
| 139 | + |
| 140 | +- 创建脚本文件:`vim /opt/test-playbook.yml` |
| 141 | + |
| 142 | +``` |
| 143 | +- hosts: hadoop-test |
| 144 | + remote_user: root |
| 145 | + vars: |
| 146 | + java_install_folder: /usr/local |
| 147 | + tasks: |
| 148 | + # 按行的方式写入 |
| 149 | + - name: Set JAVA_HOME 1 |
| 150 | + lineinfile: |
| 151 | + dest=/etc/profile |
| 152 | + line="JAVA_HOME={{ java_install_folder }}/jdk1.8.0_181" |
| 153 | + # 按块的方式写入,#{mark} 会被自动替换成:begin 和 end 字符来包裹整块内容(我这里自己定义了词语) |
| 154 | + - name: Set JAVA_HOME 2 |
| 155 | + blockinfile: |
| 156 | + path: /etc/profile |
| 157 | + marker: "#{mark} JDK ENV" |
| 158 | + marker_begin: "开始" |
| 159 | + marker_end: "结束" |
| 160 | + block: | |
| 161 | + export JAVA_HOME={{ java_install_folder }}/jdk1.8.0_181 |
| 162 | + export PATH=$PATH:$JAVA_HOME/bin |
| 163 | +``` |
| 164 | + |
| 165 | +- 执行命令:`ansible-playbook /opt/test-playbook.yml` |
| 166 | + |
| 167 | +------------------------------------------------------------------- |
| 168 | + |
| 169 | +## 更多 playbook 实战 |
| 170 | + |
| 171 | +#### 部署 JDK |
| 172 | + |
| 173 | +- 创建脚本文件:`vim /opt/jdk8-playbook.yml` |
| 174 | + |
| 175 | +``` |
| 176 | +- hosts: hadoop-host |
| 177 | + remote_user: root |
| 178 | + vars: |
| 179 | + java_install_folder: /usr/local |
| 180 | + tasks: |
| 181 | + - name: copy jdk |
| 182 | + copy: src=/opt/jdk-8u181-linux-x64.tar.gz dest={{ java_install_folder }} |
| 183 | + |
| 184 | + - name: tar jdk |
| 185 | + shell: chdir={{ java_install_folder }} tar zxf jdk-8u181-linux-x64.tar.gz |
| 186 | + |
| 187 | + - name: Set JAVA_HOME |
| 188 | + blockinfile: |
| 189 | + path: /etc/profile |
| 190 | + marker: "#{mark} JDK ENV" |
| 191 | + block: | |
| 192 | + JAVA_HOME={{ java_install_folder }}/jdk1.8.0_181 |
| 193 | + JRE_HOME=$JAVA_HOME/jre |
| 194 | + PATH=$PATH:$JAVA_HOME/bin |
| 195 | + CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar |
| 196 | + export JAVA_HOME |
| 197 | + export JRE_HOME |
| 198 | + export PATH |
| 199 | + export CLASSPATH |
| 200 | + |
| 201 | + - name: source profile |
| 202 | + shell: source /etc/profile |
| 203 | +``` |
| 204 | + |
| 205 | + |
| 206 | +- 执行命令:`ansible-playbook /opt/jdk8-playbook.yml` |
| 207 | + |
| 208 | + |
| 209 | +## 资料 |
| 210 | + |
| 211 | + |
| 212 | +- <https://www.the5fire.com/ansible-guide-cn.html> |
| 213 | +- <https://www.jianshu.com/p/62388a4fcbc6> |
| 214 | +- <http://showme.codes/2017-06-12/ansible-introduce/> |
0 commit comments