首页 > 其他分享 >Ansible

Ansible

时间:2022-10-09 08:33:51浏览次数:28  
标签:etc ansible nginx Ansible install root yml

自动化运维

(1)环境准备

关闭防火墙和SELinux,并修改/etc/hosts文件

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

getenforce 0

cat /etc/selinux/config

#     disabled - No SELinux policy is loaded.

SELINUX=disabled       //将此处改为disabled

# SELINUXTYPE= can take one of three two values:

[root@ansible-test1 ~]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.x.x ansible-test1    //添加两台主机的IP和主机名

192.168.x.x ansible-test2

 

 

(2)安装Ansible

[root@ansible-test1 ~]# yum install epel-release -y

[root@ansible-test1 ~]# yum install -y ansible

[root@ansible-test1 ~]# ansible --version

 

 

 

(3)免密配置

【test-1】:

ssh-keygen -t rsa

ssh-copy-id 192.168.x.x    #test2机器的id

ssh 192.168.x.x

(4)主机组设置

grep ^[^#] /etc/ansible/hosts

[testhost]

127.0.0.1 ansible_ssh_port=22  ansible_ssh_user=root ansible_ssh_pass=123456

192.168.x.x

2 Ansible远程执行命令

ansible testhost -m command -a "hostname"

ansible 192.168.2.20 -m command -a "hostname"

3 Ansible拷贝文件或目录

ansible 192.168.2.20 -m copy -a "src=/etc/passwd

dest=/tmp/123"

 

4 脚本执行

cat /tmp/test.sh

ansible testhost -m copy -a "src=/tmp/test.sh

dest=/tmp/test.sh "

5批量运行脚本

ansible testhost -m shell -a "/tmp/test.sh

ansible testhost -m shell -a "cat /etc/passwd |wc -l "

 

6 管理任务计划

ansible testhost -m cron -a "name='test cron'

job='/bin/bash/tmp/test.sh' weekday=6"

 

ansible testhost -m cron -a "name='test cron'

state=absent"           #删除cron

7 Ansible安装RPM包/管理服务

ansible testhost -m service -a "name=httpd state=started

enabled=yes"             #在name后面还可以加上state=installed/removed。

ansible-doc -l #列出服务模块

Ansible playbook使用

vim /etc/ansible/test.yml

---

- hosts: "192.168.116.136"

  remote_user: root

  tasks:

  - name: test_playbook

shell: "touch /tmp/playbook_test.txt"

ansible-playbook test.yml

cat create_user.yml

ansible-playbook create_user.yml

 

 

ansible playbook中的循环

    cat while.yml

ansible-playbook while.yml

ansible playbook中的条件判断‘

ansible-playbook when.yml

 

ansible playbook中的handlers

ansible-playbook handlers.yml

 

Ansible playbook实战

 

ansible自动化安装nginx

./configure --prefix=/usr/local/nginx

make && make install

cat /etc/init.d/nginx

 

 

cat /usr/local/nginx/conf/nginx.conf

# /usr/local/nginx/sbin/nginx -t

启动nginx  #psmisc可以安装killal清理httpd端口占用

# service nginx start

 

环境准备

mv nginx-1.9.6.tar.gz /etc/ansible/nginx_install/roles/install/files/

cp nginx-1.9.6/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/

cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

 

 

[root@ansible1 nginx_install]# cat install.yml

---

- hosts: 192.168.x.x             #入口文件,ssh连接机器

  remote_user: root

  gather_facts: True

  roles:

    - common

    - install

[root@ansible2 nginx_install]# cat roles/common/tasks/main.yml

- name: install initialization require software  #安装需要的依赖

  yum: name={{ item }} state=installed   ##item变成nginx_packages

  with_items:          

    - zlib-devel

    - pcre-devel

    - gcc

[root@ansible1 nginx_install]# cat roles/install/vars/main.yml

nginx_user: www             #定义所需变量

nginx_port: 80

nginx_basedir: /usr/local/nginx

[root@ansible1 nginx_install]# cat roles/install/tasks/copy.yml

- name: Copy Nginx Software    #复制压缩包

  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root

- name: Uncompression Nginx Software #解压压缩包

  shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/

- name: Copy Nginx Start Script        #复制启动脚本

  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755

- name: Copy Nginx Config        #复制nginx配置文件

  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root

mode=0644

[root@ansible2 nginx_install]# cat roles/install/tasks/install.yml

- name: create nginx user    #创建用户

  user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin

- name: start nginx service   #开启服务

  shell: /etc/init.d/nginx start

- name: add boot start nginx service       #加入开机启动

  shell: chkconfig --level 345 nginx on

- name: delete nginx compression files  #删除压缩包

  shell: rm -rf /tmp/nginx.tar.gz

[root@ansible2 nginx_install]# cat roles/install/tasks/main.yml

- include: copy.yml #调用copy.yml和install.yml

- include: install.yml

 

 

#添加shell: cd /usr/local/nginx-1.9.6 && ./configure --prefix=/usr/local/nginx

      shell: cd /usr/local/nginx-1.9.6 && make && make install

 

 

 

ansible-playbook /etc/ansible/nginx_install/install.yml

 

 

 

 

 

管理配置文件

#在/etc/ansible/nginx_config/roles/new/files/下创建vhosts文件夹,并复制nginx.conf

 

cat /etc/ansible/nginx_config/roles/new/tasks/main.yml

- name: copy conf file      #复制.conf和hosts文件

  copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644

  with_items:

    - { src: nginx.conf, dest: conf/nginx.conf }

    - { src: vhosts, dest: conf/ }

  notify: restart nginx

[root@ansible-test1 ansible]# clear

[root@ansible-test1 ansible]# cat /etc/ansible/nginx_config/roles/new/handlers/main.yml

- name: restart nginx   #用于重新加载nginx服务

  shell: /etc/init.d/nginx reload

[root@ansible-test1 ansible]#

[root@ansible-test1 ansible]# cat /etc/ansible/nginx_config/roles/new/tasks/main.yml

- name: copy conf file      #复制.conf和hosts文件

  copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644

  with_items:

    - { src: nginx.conf, dest: conf/nginx.conf }

    - { src: vhosts, dest: conf/ }

  notify: restart nginx

[root@ansible-test1 ansible]#

[root@ansible-test1 ansible]#

[root@ansible-test1 ansible]# cat /etc/ansible/nginx_config/roles/new/vars/main.yml

nginx_basedir: /usr/local/nginx #定义变量

[root@ansible-test1 ansible]#

[root@ansible-test1 ansible]#

[root@ansible-test1 ansible]# cat nginx_config/update.yml

---

- hosts: 192.168.200.153  #入口文件

  user: root

  roles:

  - new         #这里只有new

 

 

 

 ansible-playbook /etc/ansible/nginx_config/update.yml

 

标签:etc,ansible,nginx,Ansible,install,root,yml
From: https://www.cnblogs.com/lzqabc/p/16770894.html

相关文章

  • 关于Centos6、selinux和ansible
    简单说,一般来说,ansible想通过证书访问其他主机,需要关闭selinux。或者(也可能是并且),安装libselinux-python。而centos6已经停止维护了,需要更新yum源,或者挂载系统安装包。否......
  • Ansible 自动化最佳实践
    Ansible自动化最佳实践                         版本标识V1编制单位李斌编制日期2022年......
  • ansible学习笔记03(最佳实践)
    1、控制提权Ansibleplaybook能实现多种不同方式的提权操作,提权的级别取决于你打算控制的级别,ansible使用命令或连接参数,在play、role、block和tasks中,都可以设置提权,设......
  • ansible基础 命令
    ansible基础命令:1.远程执行命令ansible组名-mcommand-a"命令"  2.文件拷贝ansibleip-mcopy-a"src=/etc/passwddest=/tmp/123" 3.ansible远程......
  • 14.ansible模块之 selinux firewalld iptables
    1.1selinux官方示例EXAMPLES:-name:EnableSELinuxselinux:policy:targetedstate:enforcing-name:PutSELinuxinpermissivemode,loggingact......
  • 安装zabbix-agent2之ansible-playbook
    ----name:installagenthosts:allvars:server_host:"192.168.100.206"tasks:-shell:"rpm-Uvhhttps://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zab......
  • 安装zabbix-agent2之ansible-playbook
    zabbix被监控端安装zabbix-agent2之ansible-playbook----name:installagenthosts:allvars:server_host:"192.168.100.206"tasks:-shell:"rpm......
  • Linux部署ansible
    说明:本人使用系统版本为centos7系列,以下操作仅供参考。一般而言,部署某个运维工具或者应用时,使用yum是最为简单高效的,而有些时候由于主机无法访问外网,那么可以使用离线的方式......
  • Ansible常用模块Ad-Hoc用法
    1、配置主机清单[root@rocky8~]#cd/data/ansible/[root@rocky8~]#ansible-configinit--disabled>ansible.cfg#生成初始化配置文件[root@rocky8ansible]#vimansi......
  • ansible使用collection
    1.安装collectiondevsec.hardening$ansible-galaxycollectioninstalldevsec.hardening2.查看role$tree-d/home/rocky/.ansible/collections/ansible_collect......