Playbook
指令:ansible-playbook <filename.yml> … [options]
-C --check #隻檢測可能會發生的改變,但不真正執行操作
--list-hosts #列出運作任務的主機
--list-tags #列出tag
--list-tasks #列出task
--limit 主機清單 #隻針對主機清單中的主機執行
-v -vv -vvv #顯示過程
playbook 建立使用者
useradd.yml
---
# useradd
- hosts: web
remote_user: root
gather_facts: false
vars:
- username: user1
- chpasswd: '123456'
tasks:
- name: change user passwd
user: name={{ username }} password={{ chpasswd | password_hash('sha512') }} update_password=always
playbook安裝jdk
---
# install jdk
- hosts: web
remote_user: root
gather_facts: no
tasks:
- name: unarchive package
unarchive: src=/opt/src/jdk-8u101-linux-x64.gz dest=/usr/local/ owner=root group=root
- name: link
file: src=/usr/local/jdk1.8.0_101 path=/usr/local/java state=link
- name: PATH variable
copy: src=/etc/ansible/files/jdk.sh dest=/etc/profile.d/ backup=yes
playbook編譯安裝nginx
---
# install nginx
- hosts: web
remote_user: nginx
gather_facts: no
tasks:
- name: mkdir tools
file: path=/home/nginx/tools state=directory
- name: unarchive pcre package
unarchive: src=/opt/src/pcre-8.30.tar.gz dest=/home/nginx/tools owner=nginx group=nginx
- name: make install pcre
shell: ./configure --prefix=/home/nginx/tools/pcre && make && make install
args:
chdir: /home/nginx/tools/pcre-8.30
- name: unarchive zlib package
unarchive: src=/opt/src/zlib-1.2.7.tar.gz dest=/home/nginx/tools owner=nginx group=nginx
- name: make install zlib
shell: ./configure --prefix=/home/nginx/tools/zlib && make && make install
args:
chdir: /home/nginx/tools/zlib-1.2.7
- name: unarchive openssl package
unarchive: src=/opt/src/openssl-1.0.2.tar dest=/home/nginx/tools owner=nginx group=nginx
- name: make install openssl
shell: ./config --prefix=/home/nginx/tools/openssl && make && make install
args:
chdir: /home/nginx/tools/openssl-1.0.2
- name: unarchive nginx package
unarchive: src=/opt/src/nginx-1.20.1.tar.gz dest=/home/nginx/tools owner=nginx group=nginx
- name: make install nginx
shell: ./configure --prefix=/home/nginx/nginx --with-http_ssl_module --with-http_gzip_static_module --with-pcre=/home/nginx/tools/pcre-8.30 --with-zlib=/home/nginx/tools/zlib-1.2.7 --with-openssl=/home/nginx/tools/openssl-1.0.2 && make && make install
args:
chdir: /home/nginx/tools/nginx-1.20.1
playbook安裝mysql
---
#Installing Mysql Binary Tarballs
- hosts: dbsrvs
remote_user: root
gather_facts: no
tasks:
- name: create group
group: name=mysql gid=27 system=yes
- name: create user
user: name=mysql uid=27 system=yes group=mysql shell=/sbin/nologin home=/usr/local/mysql create_home=no
- name: mkdir datadir
file: path=/data/mysql owner=mysql group=mysql state=directory
- name: unarchive package
unarchive: src=/root/mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz dest=/usr/local/ owner=root group=root remote_src=yes
- name: link
file: src=/usr/local/mysql-5.7.14-linux-glibc2.5-x86_64 path=/usr/local/mysql state=link
- name: config file
copy: src=/etc/ansible/files/my.cnf dest=/etc/ backup=yes
- name: install database
shell: /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/ --user=mysql --initialize-insecure &
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: start service
service: name=mysqld state=started enabled=yes