«
ansible(二)

时间:2022-9-6     作者:李泽信     分类: ansible


一、ansible playbook应用概述

二、ansible playbook语法

ansible-playbook <filename.yml>...[options]
[options]:
-C, --check:并不在远程主机上执行,只是测试
-i PATH, --inventory=PATH:资产的文件路径
--flush-cache:清楚fact缓存
--list-hosts:列出匹配的远程主机,并不执行任何动作
-t,TAGS, --tags=TAGS:运行指定的标签任务
--skip-tags:跳过指定的notify
[root@ansible ~]# vi nginx_install.yaml
---
- hosts: 192.168.100.102
  remote_user: root
  tasks:
   - name: nginxa
     yum: name=pcre-devel,pcre,zlib-devel state=present
   - name: nginxb
     shell: cd /root; wget ftp://192.168.100.100/tools/nginx-1.12.2.tar.gz;tar zxvf /root/nginx-1.12.2.tar.gz -C /usr/src;cd /usr/src/nginx-1.12.2/;./configure --prefix=/usr/local/nginx;make;make install




---
- hosts: 192.168.100.103
  remote_user: root
  vars:
  - pack: httpd
  - serv: httpd
  tasks:
  - name: install httpd
    yum: name={{ pack }} state=present
  - name: copy html
    copy: src=/tmp/index.html dest=/var/www/html/
    notify:
      - restart httpd
  - name: start httpd services
    shell: systemctl start httpd;systemctl enable httpd
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted

[root@ansible ansible]# echo "haohaoxuexi">/tmp/index.html
[root@ansible ansible]# ansible-playbook httpd.yml


[root@ansible ansible]# echo "tiantianxiangshang">/tmp/index.html
[root@ansible ansible]# ansible-playbook httpd.yml


---
[root@ansible ansible]# vim loop.yml
- hosts: 192.168.100.103
  remote_user: root
  tasks:
  - name: create users
    user: name={{ item }} state=present
    with_items:
    - test11
    - test21
    - test31


[root@ansible ansible]# vim hostname.yml
---
- hosts: web
  remote_user: root
  tasks:
  - name: show hostname
    shell: hostname
  - name: show ip
    command: ip a
  - hostname: name=web{{ ansible_default_ipv4.address.split('.')[-1] }}