1、自动配置SSH密钥实例

- hosts: hadoop3
  gather_facts: no
  roles:
   - roles
  tasks:
   - name: close ssh yes/no check
     lineinfile: path=/etc/ssh/ssh_config regexp='(.*)StrictHostKeyChecking(.*)' line="StrictHostKeyCheck
ing no"
   - name: delete /root/.ssh/
     file: path=/root/.ssh/ state=absent
   - name: create .ssh directory
     file: dest=/root/.ssh mode=0600 state=directory
   - name: generating local public/private rsa key pair
     local_action: shell ssh-keygen -t rsa -b 2048 -N '' -y -f /root/.ssh/id_rsa
   - name: view id_rsa.pub
     local_action: shell cat /root/.ssh/id_rsa.pub
     register: sshinfo
   - set_fact: sshpub={{sshinfo.stdout}}
   - name: add ssh record
     local_action: shell echo {{sshpub}} > {{AnsibleDir}}/roles/templates/authorized_keys.j2
   - name: copy authorized_keys.j2 to all
     template: src={{AnsibleDir}}/roles/templates/authorized_keys.j2 dest=/root/.ssh/authorized_keys mode=0600
     tags:
     - install ssh

2、自动修改主机名实例

- hosts: hadoop3
  remote_user: root
  tasks:
  - name: change name
    shell: "echo {{hostname}} > /etc/hostname"
  - name:
    shell: hostname {{hostname|quote}}

3、自动优化系统配置实例

- hosts: hadoop3
  remote_user: root
  gather_facts: false
  tasks:
   - name: selinux disabled
     lineinfile: dest=/etc/selinux/config regexp='SELINUX=(.*)' line='SELINUX=disabled'
   - name:  
     lineinfile: dest=/etc/security/limits.conf line="{{item.value}}"
     with_items:
     - {value: "*         soft    nofile         655360"}
     - {value: "*         hard    nofile         655360"}
   - name: disabled iptables and firewalld
     shell: systemctl stop firewalld&&systemctl disable firewalld&&iptables -F
   - name: cron ntpdate
     cron: name=ntpdate minute=*/5 user=root job="source /etc/profile;/usr/sbin/ntpdate -u 172.16.21.1
;/sbin/hwclock -w"

4、自动配置主机hosts文件实例

- hosts: hadoop3
  remote_user: root
  gather_facts: false
  tasks:
   - name: selinux disabled
     lineinfile: dest=/etc/selinux/config regexp='SELINUX=(.*)' line='SELINUX=disabled'
   - name:  
     lineinfile: dest=/etc/security/limits.conf line="{{item.value}}"
     with_items:
     - {value: "*         soft    nofile         655360"}
     - {value: "*         hard    nofile         655360"}
   - name: disabled iptables and firewalld
     shell: systemctl stop firewalld&&systemctl disable firewalld&&iptables -F
   - name: cron ntpdate
     cron: name=ntpdate minute=*/5 user=root job="source /etc/profile;/usr/sbin/ntpdate -u 172.16.21.1
;/sbin/hwclock -w"

5、自动安装JDK并设置环境变量实例

- hosts: hadoop3
  remote_user: root
  roles:
  - roles
  tasks:
   - name: mkdir jdk directory
     file: path=/usr/java state=directory mode=0755
   - name: copy and unzip jdk
     unarchive: src={{AnsibleDir}}/roles/files/jdk1.8.tar.gz dest=/usr/java
   - name: delete line
     lineinfile: dest=/etc/profile regexp='(.*)JAVA_HOME(.*)' state=absent
   - name: set jdk env
     lineinfile: dest=/etc/profile line="{{item.value}}" state=present
     with_items:
     - {value: "export JAVA_HOME=/usr/java/jdk1.8.0_162"}
     - {value: "export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"}
     - {value: "export PATH=$JAVA_HOME/bin:$PATH"}
   - name: source profile
     shell: source /etc/profile