ansible实例

示例:

单个模块使用

ansible all -a ‘ifconfig’

ansible all -m shell -a "echo qwer1234|passwd --stdin root"

ansible all -m shell -a 'cat /etc/fstab|grep -Ev "^#|^$"'

ansible all -m copy -a "src=/etc/fstab dest=/tmp "

ansible all -m copy -a "src=./hello dest=/tmp "

ansible all -m copy -a "src=./hello dest=/tmp owner=nobody group=nobody mode=000"

ansible all -m cron -a "minute=*/1 name=cron-cs2 state=present job='echo a>>/root/hello3'"

ansible all -m fetch -a "src=/etc/fstab dest=/tmp"

 

ansible all -m cron -a "name=cron-cs state=absent "

ansible all -m yum -a "name=tree state=latest"

ansible all -m yum -a "name=httpd state=latest"

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

 

ansible all -m group -a "name=nihao gid=2000 state=present"

 

ansible all -m user -a "name=xiaoming state=present"

ansible all -m script -a  "/root/cs.sh "

ansible 192.168.181.31 -m ping

ansible 192.168.181.31 -m command -a "whoami"

 

 

 

安装httpd,复制本地配置文件(添加标签),启动httpd;当配置文件修改时重启httpd,

 

cat 1.yaml

- hosts: all

  remote_user: root

  tasks:

  - name: install httpd

    yum: name=httpd state=latest

  - name: copy config file

    copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf

    notify: restart httpd

  - name: start httpd

    service: name=httpd state=started

  handlers:

  - name: restart httpd

    service: name=httpd state=restarted

 

获取目标主机的变量值保存到指定文件,此处ansible_env为执行ansible自动收集的变量之一

 

cat 2.yaml

- hosts: 192.168.181.31

  remote_user: root

  tasks:

  - name: copy file

    copy: content={{ ansible_env }} dest=/tmp/ansible.env

 

自定义变量,安装指定服务

ansible-playbook -e package=lrzsz 3.yaml

 

- hosts: 192.168.181.31

  remote_user: root

  tasks:

  - name: install {{ package }}

    yum: name={{ package }} state=latest

 

创建用户,并设置密码

vim /etc/ansible/hosts

[cs2]

192.168.181.31 ansible_ssh_user=hello ansible_ssh_pass=hello

 

 

- hosts: 192.168.181.31

  remote_user: root

  tasks:

  - name: add users

    user: name=hello system=no state=present

  - name: set password

    shell: echo hello|passwd --stdin hello

 

 

 

vim /etc/ansible/hosts

[cs2]

192.168.181.31 hellovar=hellovar

[cs2:vars]

hivar=hivar

 

vim 5.yaml

- hosts: cs2

  remote_user: root

  vars:

  - pbvar: playbook variable testing

  tasks:

  - name: command line variables

    copy: content={{ cmdvar }} dest=/tmp/cmd.var

  - name: playbook variables

    copy: content={{ pbvar }} dest=/tmp/pb.var

  - name: host ivnetory variables

    copy: content={{ hivar }} dest=/tmp/hi.var

  - name: host ivnetory variables2

    copy: content={{ hellovar }} dest=/tmp/hello.var

 

 

ansible-playbook -e cmdvar='cmdcscs' 5.yaml

 

 

vim /etc/ansible/host

===

[cs1]

192.168.181.3[1:2]

[cs1:vars]

http_port=8080

 

vim 5.yaml

- hosts: all

  remote_user: root

  tasks:

  - name: install conf file

    template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf

    notify: restart httpd

    tags: instconf

  handlers:

  - name: restart httpd

    service: name=httpd state=restarted

 

ansible-playbook 5.yaml

 

 

vim 6.yaml

- hosts: all

  remote_user: root

  tasks:

  - name: install httpd

    yum: name=httpd state=latest

    when: ansible_os_family == "RedHat"

  - name: install apache2

    apt: name=apache2 state=latest

    when: ansible_os_family == "Debian"

 

 

 

vim 7.yaml

- hosts: all

  remote_user: root

  tasks:

  - name: install {{ item }} package

    yum: name={{ item }} state=latest

    with_items:

    - wget

    - zip

    - unzip

 

 

 

]# cat 8.yaml

- hosts: all

  remote_user: root

  roles:

  - nginx

 

 

 

/etc/ansible/roles/nginx

]# ls

default  files  handlers  meta  tasks  templates  vars

 

[root@localhost nginx]# cat files/index.html

<h1>vhost1</h1>

[root@localhost nginx]# cat handlers/main.yaml

- name: restart nginx

  service: name=nginx state=restarted

[root@localhost nginx]# cat tasks/main.yaml

- name: install nginx

  yum: name=nginx state=latest

- name: install conf

  template: src=vhosts1.conf.j2 dest=/etc/nginx/conf.d/vhost1.conf

  tags: conf

  notify: restart nginx

- name: install site home directory

  file: path={{ nginxroot }} state=directory

- name: install index page

  copy: src=index.html dest={{ nginxroot }}/

- name: start nginx

  service: name=nginx state=started

[root@localhost nginx]# cat templates/vhosts1.conf.j2

server {

 listen 81;

 server_name {{ ansible_fqdn }};

 location / {

  root "/nginxdata/vhost1";

}

}

[root@localhost nginx]# cat vars/main.yaml

nginxroot: /nginxdata/vhost1

 

 

 

主/备模式高可用keepalived+nginx(proxy)

使用一台nginx代理两台tomcat服务器

三台机器

nginx 192.168.181.3

两台tomcat  192.168.181.31-32

 

cat /etc/ansible/hosts

[lb]

192.168.181.3

[tcsrvs]

192.168.181.3[1:2]

 

mkdir -pv /etc/ansible/roles/{nginx,tomcat,java}/{files,templates,tasks,handlers,vars,meta,default}

 

 

cd /etc/ansible/roles

]# cat java/tasks/main.yaml

- name: install java

  yum: name=java-{{ version }}-openjdk-devel state=latest

- name: install env file

  copy: src=java.sh dest=/etc/profile.d/

 

]# cat java/files/java.sh

export JAVA_HOME=/usr

 

]# cat nginx/tasks/main.yaml

- name: install nginx

  yum: name=nginx state=latest

  when: ansible_os_family == "RedHat"

- name: install conf

  copy: src=lb.conf dest=/etc/nginx/conf.d/

  tags: conf

  notify: restart nginx

- name: start nginx

  service: name=nginx state=started enabled=yes

 

]# cat nginx/files/lb.conf

upstream tcsrvs {

       server 192.168.181.31:8080;

       server 192.168.181.32:8080;

}

 

server {

       listen 80;

       server_name 192.168.181.3;

       location / {

              proxy_pass http://tcsrvs;

}

}

 

]# cat nginx/handlers/main.yaml

- name: restart nginx

  service: name=nginx state=restarted

 

 

]# cat tomcat/tasks/main.yaml

- name: install package

  yum: name={{ item }} state=latest

  with_items:

  - tomcat

  - tomcat-admin-webapps

  - tomcat-webapps

  - tomcat-docs-webapp

  when: ansible_os_family == "RedHat"

- name: install index page

  template: src=index.html.j2 dest=/var/lib/tomcat/webapps/ROOT/index.html

- name: start tomcat

  service: name=tomcat state=started enabled=yes

 

]# cat tomcat/templates/index.html.j2

<h1>hello {{ ansible_fqdn }}</h1>

posted on 2025-05-10 17:55  赛博狗尾草  阅读(20)  评论(0)    收藏  举报

导航