ANSIBLE_CONFIG (环境变量)
ansible.cfg (脚本所在当前目录下)
~/.ansible.cfg (用户家目录下,默认没有)
/etc/ansible/ansible.cfg(安装后会自动生成)
[hadoop-host]
192.168.0.223
192.168.0.70
192.168.0.103
[hadoop-host]
hadoop-master ansible_host=192.168.0.223 ansible_user=root ansible_ssh_pass=123456
hadoop-node1 ansible_host=192.168.0.70 ansible_user=root ansible_ssh_pass=123456
hadoop-node2 ansible_host=192.168.0.103 ansible_user=root ansible_ssh_pass=123456
sudo ansible --private-key ~/.ssh/id_rsa all -m ping
ansible hadoop-host -a 'ps'
- hosts: all
tasks:
- name: whoami
shell: 'whoami > /opt/whoami.txt'
PLAY [all] **************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.0.223]
ok: [192.168.0.103]
ok: [192.168.0.70]
TASK [whoami] ***********************************************************************************************************************
changed: [192.168.0.103]
changed: [192.168.0.223]
changed: [192.168.0.70]
PLAY RECAP **************************************************************************************************************************
192.168.0.103 : ok=2 changed=1 unreachable=0 failed=0
192.168.0.223 : ok=2 changed=1 unreachable=0 failed=0
192.168.0.70 : ok=2 changed=1 unreachable=0 failed=0
- hosts: hadoop-test
remote_user: root
vars:
java_install_folder: /usr/local
tasks:
# 按行的方式写入
- name: Set JAVA_HOME 1
lineinfile:
dest=/etc/profile
line="JAVA_HOME=/jdk1.8.0_181"
# 按块的方式写入,#{mark} 会被自动替换成:begin 和 end 字符来包裹整块内容(我这里自己定义了词语)
- name: Set JAVA_HOME 2
blockinfile:
path: /etc/profile
marker: "#{mark} JDK ENV"
marker_begin: "开始"
marker_end: "结束"
block: |
export JAVA_HOME=/jdk1.8.0_181
export PATH=$PATH:$JAVA_HOME/bin
- hosts: all
remote_user: root
tasks:
- name: Disable SELinux at next reboot
selinux:
state: disabled
- name: disable firewalld
command: ""
with_items:
- systemctl stop firewalld
- systemctl disable firewalld
- setenforce 0
- hosts: all
remote_user: root
tasks:
- name: Disable SELinux at next reboot
selinux:
state: disabled
- name: disable firewalld
command: ""
with_items:
- systemctl stop firewalld
- systemctl disable firewalld
- setenforce 0
- name: install-basic
command: ""
with_items:
- yum install -y zip unzip lrzsz git epel-release wget htop deltarpm
- name: install-vim
shell: ""
with_items:
- yum install -y vim
- curl https://raw.githubusercontent.com/wklken/vim-for-server/master/vimrc > ~/.vimrc
- name: install-docker
shell: ""
with_items:
- yum install -y yum-utils device-mapper-persistent-data lvm2
- yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- yum makecache fast
- yum install -y docker-ce
- systemctl start docker.service
- docker run hello-world
- name: install-docker-compose
shell: ""
with_items:
- curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- docker-compose --version
- systemctl restart docker.service
- systemctl enable docker.service
- hosts: all
remote_user: root
tasks:
- name: update hosts
blockinfile:
path: /etc/hosts
block: |
192.168.0.223 linux01
192.168.0.223 linux02
192.168.0.223 linux03
192.168.0.223 linux04
192.168.0.223 linux05
- hosts: hadoop-host
remote_user: root
vars:
java_install_folder: /usr/local
tasks:
- name: copy jdk
copy: src=/opt/jdk-8u181-linux-x64.tar.gz dest=
- name: tar jdk
shell: chdir= tar zxf jdk-8u181-linux-x64.tar.gz
- name: set JAVA_HOME
blockinfile:
path: /etc/profile
marker: "#{mark} JDK ENV"
block: |
JAVA_HOME=/jdk1.8.0_181
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export JRE_HOME
export PATH
export CLASSPATH
- name: source profile
shell: source /etc/profile
- hosts: hadoop-host
remote_user: root
tasks:
- name: Creates directory
file:
path: /data/hadoop/hdfs/name
state: directory
- name: Creates directory
file:
path: /data/hadoop/hdfs/data
state: directory
- name: Creates directory
file:
path: /data/hadoop/hdfs/tmp
state: directory
- name: set HADOOP_HOME
blockinfile:
path: /etc/profile
marker: "#{mark} HADOOP ENV"
block: |
HADOOP_HOME=/usr/local/hadoop
PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
export HADOOP_HOME
export PATH
- name: source profile
shell: source /etc/profile