ansible基本操作

 

ansible模块

1、主机连通性测试

ansible qf -m   ping

 

2、command模块  ,默认就是command模块

 

3、shell模块

ansible qf  -m  shell  -a  'cat  /etc/passwd | grep "root"'

 

4.copy模块

====================

- hosts: qf

  vars:

    IP: "{{ ansible_ens33['ipv4']['address'] }}"

  tasks:

  - name: 将原有的hosts文件备份

    shell: mv /etc/hosts /etc/hosts_bak

  - name: 将ansible端的hosts复制到各自机器上

    copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0644

  - name: 将新的hosts文件后面追加各自机器内网和hostname

    lineinfile: dest=/etc/hosts line="{{ IP }} {{ ansible_hostname }} "

=======================

  - name: copy {{ rpmname }}.conf   

    copy: src=/tmp/{{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf  backup=yes

ansible qf -m copy  -a  'content="jfsakf\n" dest=/data/name mode=666'

 

5.file模块

ansible qf -m  file -a 'path=/data/app state=directory'

  - name: 创建目录

    file: path=/data/a state=directory

       file: path=/data/a state=absent  //删除

6.fetch模块

ansible qf -m fetch -a 'src=/data/hello.txt dest=/data'  //只能是文件

 

7.cron计划任务模块      

ansible qf -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'      

ansible qf -m cron -a 'name="df everyday" hour=15 job="df -lh >> /tmp/disk_total &> /dev/null" state=absent'

      

8.yum模块

name=  #所安装的包的名称

state =  #present--->安装, latest--->安装最新的, absent---> 卸载软件。

  - name: install httpd

    yum: name=httpd state=present

 

9.service模块

ansible qf -m service -a 'name=nginx state=started enabled=true'

- name: start {{ rpmname }} service

      service: name={{ rpmname }} state=started//stopped//restarted

        

10.user模块

ansible qf -m user -a 'name=keer uid=11111'

 

11.group模块

ansible qf -m group -a 'name=sanguo gid=12222'

 

12.script模块

vim  /tmp/df.sh

chmod  -v  +x  df.sh

ansible web -m script -a '/tmp/df.sh'

 

13.setup模块

ansible web -m setup -a 'filter="*mem*"'   #查看内存

========================

安装mariadb服务

---

- hosts: qf

  remote_user: root

  tasks:

    - name: install mariadb-server package

      yum: name=mariadb-server state=present

    - name: starting mariadb-server service

      service: name=mariadb state=started

============================

---

- hosts: qf

  remote_user: root

  vars:

    - rpmname: nginx

      nginxport: 8080

  tasks:

    - name: install {{ rpmname }}

      yum: name={{ rpmname }} state=present

    - name: copy {{ rpmname }}.conf

      template: src=/tmp/{{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf backup=yes

      notify: reload

      tags: reload{{ rpmname }}

    - name: start {{ rpmname }} service

      service: name={{ rpmname }} state=started

      tags: start{{ rpmname }}

 

  handlers:

    - name: reload

      service: name={{ rpmname }} state=restarted

        

ansible-playbook nginx.yml   &&   ansible qf -m  shell  -a  'ss -tunlp|grep :80'

ansible  qf  -m  shell  -a  'systemctl  stop  nginx'    ansible qf -m  shell  -a  'ss -tunlp|grep :80'

ansible-playbook  nginx.yml  -t  startnginx

===============================================

---

- hosts: qf

  vars:

    IP: "{{ ansible_ens33['ipv4']['address'] }}"

  tasks:

  - name: 将原有的hosts文件备份

    shell: mv /etc/hosts /etc/hosts_bak

  - name: 将ansible端的hosts复制到各自机器上

    copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0644

  - name: 将新的hosts文件后面追加各自机器内网和hostname

    lineinfile: dest=/etc/hosts line="{{ IP }} {{ ansible_hostname }} "

===========================      

---

- hosts: qf

  remote_user: root

  tasks:

    - name: install {{ rpmname }}

      yum: name={{ rpmname }} state=present

    - name: copy {{ rpmname }}.conf

      template: src=/tmp/{{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf backup=yes

      notify: reload

      tags: reload{{ rpmname }}

    - name: start {{ rpmname }} service

      service: name={{ rpmname }} state=started

      tags: start{{ rpmname }}

 

  handlers:

    - name: reload

      service: name={{ rpmname }} state=restarted

cp  /etc/keepalived/keepalived.conf  /tmp/keepalived.conf.j2

ansible-playbook  nginx.yml  -e  rpmname=keepalived

=================================      

template模板

---

- hosts: qf

  tasks:

  - name: install conf file to centos7

    shell: cat /etc/redhat-release > /root/aa.txt

    when: ansible_distribution_major_version == "7"

  - name: install conf file to centos6

    template: src=files/nginx.cong.c6.j2

    when: ansible_distribution_major_version == "6"

  - name: install web packages

    yum:

      name: [ 'tcpdump', 'net-tools' ]

      state: present

  - name: add some groups

    group: name={{ item }} state=present

    with_items:

      - group11

      - group12

      - group13

  - name: add some users

    user: name={{ item.name }} group={{ item.group }} state=present

    with_items:

      - { name: 'user11', group: 'group11' }

      - { name: 'user12', group: 'group12' }

      - { name: 'user13', group: 'group13' }

============================================

[root@server tmp]# vim nginx.conf.j2

    worker_processes  {{ ansible_processor_vcpus }};

    listen       {{ nginxport }};      

---

- hosts: qf

  remote_user: root

  vars:

    - rpmname: nginx

         nginxport: 8888

  tasks:

    - name: install {{ rpmname }}

      yum: name={{ rpmname }} state=present

    - name: copy {{ rpmname }}.conf

      template: src=/etc/{{ rpmname }}/{{ rpmname }}.conf dest=/etc/{{ rpmname }}/{{ rpmname }}.conf.bak backup=yes

      notify: reload

      tags: reload{{ rpmname }}

    - name: start {{ rpmname }} service

      service: name={{ rpmname }} state=started

      tags: start{{ rpmname }}

 

  handlers:

    - name: reload

      service: name={{ rpmname }} state=restarted

~

=============================================

roles       角色定制

[root@node12 roles]# tree

.

└── nginx

    ├── files

    ├── handlers

    │   └── main.yml

    ├── tasks

    │   └── main.yml

    ├── templates

    │   └── nginx.cong.j2

    └── vars

        └── main.yml

==================================             

vim handlers/main.yml    //触发器

---

  - name: reload

    service: name=nginx state=restarted

========================================      

vim  tasks/main.yml

---

  - name: install {{ rpmname }}

    yum: name={{ rpmname }} state=present

  - name: copy {{ rpmname }}.conf

    template: src={{ rpmname }}.conf.j2 dest=/etc/{{ rpmname }}/{{ rpmname }}.conf backup=yes

    notify: reload

    tags: reload{{ rpmname }}

  - name: start {{ rpmname }} service

    service: name={{ rpmname }} state=started

    tags: start{{ rpmname }}

=========================================

vim vars/main.yml    //变量

---

nginxport: 9999

rpmname: nginx

========================================

vim  templates/nginx.conf.j2   //模板  //源文件   从nginx配置文件中复制过来的

 

=======

在roles统计目录

vim  /nginx_test.yml

---

- hosts: qf

  roles:

    - nginx

ansible-playbook nginx_test.yml

 

=============================================