目录
前言
练习1:用copy拷贝一个文件到db主机组。
练习2:修改1.yaml的内容如下。
练习3:验证,命令如下。
2. if 判断
练习:写一个 jinja2模板文件,内容如下。
3. for循环
练习:写一个变量文件users_list.txt,里面包含一个名称为users的列表,命令如下。
3. handlers
可以使用copy模块把本地的一个文件拷贝到远端机器,下面再次复习一下。
[bdqn@RHEL813 ~]$ mkdir demo4
[bdqn@RHEL813 ~]$ cp ansible.cfg hosts demo4
[bdqn@RHEL813 ~]$ cd demo4
[bdqn@RHEL813 demo4]$ ls
ansible.cfg hosts
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat aa.txt
+----------------------------------------------------+
| 我的IP地址是: {{ansible_default_ipv4.address}}
+----------------------------------------------------+
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 1.yaml
---
- hosts: db
tasks:
- name: 拷贝一个文件到远端主机
copy: src=aa.txt dest=/opt/aa.txt
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 1.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [拷贝一个文件到远端主机] ******************************************************************************************************************
changed: [server2]
changed: [server3]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible db -m shell -a "cat /opt/aa.txt"
server2 | CHANGED | rc=0 >>
+----------------------------------------------------+
| 我的IP地址是: {{ansible_default_ipv4.address}}
+----------------------------------------------------+
server3 | CHANGED | rc=0 >>
+----------------------------------------------------+
| 我的IP地址是: {{ansible_default_ipv4.address}}
+----------------------------------------------------+
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 1.yaml
---
- hosts: db
tasks:
- name: 拷贝一个文件到远端主机
template: src=aa.txt dest=/opt/aa.txt
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 1.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [拷贝一个文件到远端主机] ******************************************************************************************************************
changed: [server2]
changed: [server3]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible db -m shell -a "cat /opt/aa.txt"
server2 | CHANGED | rc=0 >>
+----------------------------------------------------+
| 我的IP地址是: 192.168.103.14
+----------------------------------------------------+
server3 | CHANGED | rc=0 >>
+----------------------------------------------------+
| 我的IP地址是: 192.168.103.15
+----------------------------------------------------+
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ mv aa.txt aa.j2
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 1.yaml
---
- hosts: db
tasks:
- name: 拷贝一个文件到远端主机
template: src=aa.j2 dest=/opt/aa.txt
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ mkdir templates
[bdqn@RHEL813 demo4]$
在templates目录中创建aa.j2,内容如下。
[bdqn@RHEL813 demo4]$ cat templates/aa.j2
+-----------------------------------------------------+
| 我的主机名是: {{ansible_fqdn}}
+-----------------------------------------------------+
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat aa.j2
+----------------------------------------------------+
| 我的IP地址是: {{ansible_default_ipv4.address}}
+----------------------------------------------------+
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 1.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [拷贝一个文件到远端主机] ******************************************************************************************************************
changed: [server3]
changed: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible db -m shell -a "cat /opt/aa.txt"
server2 | CHANGED | rc=0 >>
+-----------------------------------------------------+
| 我的主机名是: RHEL8
+-----------------------------------------------------+
server3 | CHANGED | rc=0 >>
+-----------------------------------------------------+
| 我的主机名是: RHEL812
+-----------------------------------------------------+
[bdqn@RHEL813 demo4]$
{% if 判断1 %}
内容1
{% elif 判断2 %}
内容2
...多个elif...
{% else %}
内容3
{% endif %}
(1)“%”两边有没有空格都可以,不过所有的“%”前后空格要保持一致,即要有都
有,要没有都没有。
(2)if和elif中的内容如果太长了,可以另起一行写。
如果判断1成立,则打印内容1,后面的条件不再判断,直接跳转到endif后面的内容;如果判断1不成立,则执行elif后面的判断2,如果成立则打印内容2,后面的条件不再判断,直接跳转到endif后面的内容。以此类推,如果所有的f和elif都不成立,则打印else中的内容。
(3)elif和 else不是必需的
[bdqn@RHEL813 demo4]$ cat templates/bb.j2
1111
{% if ansible_fqdn=="RHEL8" %}
{{ansible_fqdn}}
{% else %}
aaaa
{% endif %}
3333
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 2.yaml
---
- hosts: db
tasks:
- name: 拷贝一个文件过去
template: src=bb.j2 dest=/opt/bb.conf
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 2.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [拷贝一个文件过去] *********************************************************************************************************************
changed: [server2]
changed: [server3]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible db -m shell -a "cat /opt/bb.conf"
server3 | CHANGED | rc=0 >>
1111
aaaa
3333
server2 | CHANGED | rc=0 >>
1111
RHEL8
3333
[bdqn@RHEL813 demo4]$
判断1 or 判断11:判断1和判断11只要有一个成立就算成立,只有全部不成立才算不成
立。
判断1 and 判断11:判断1和判断11只有全部成立才算成立,只要有一个不成立就算不成
立。
[bdqn@RHEL813 demo4]$ cat templates/cc.j2
1111
{% if ansible_fqdn=="RHEL8"
and
ansible_distribution_major_version=="8" %}
{{ansible_fqdn}}
{% else %}
aaaa
{% endif %}
3333
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 3.yaml
---
- hosts: db
tasks:
- name: 我要 拷贝一个文件过去
template: src=cc.j2 dest=/opt/cc.conf
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 3.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [我要 拷贝一个文件过去] ******************************************************************************************************************
changed: [server2]
changed: [server3]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible db -m shell -a "cat /opt/cc.conf"
server3 | CHANGED | rc=0 >>
1111
aaaa
3333
server2 | CHANGED | rc=0 >>
1111
RHEL8
3333
[bdqn@RHEL813 demo4]$
{% for i in 列表名 %}
{{i}}
{% endfor %}
[bdqn@RHEL813 demo4]$ cat templates/dd.conf.j2
{% set list1=['aa','bb','cc'] %}
1111
{% for i in list1 %}
{{i}}
{% endfor %}
5555
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 4.yaml
---
- hosts: db
tasks:
- name: 拷贝一个文件到远端主机
template: src=dd.conf.j2 dest=/opt/dd.conf
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 4.yaml
PLAY [db] ***************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
ok: [server3]
TASK [拷贝一个文件到远端主机] ******************************************************************************************************************
changed: [server2]
changed: [server3]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible server2 -m shell -a "cat /opt/dd.conf"
server2 | CHANGED | rc=0 >>
1111
aa
bb
cc
5555
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat user_list.txt
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 21
sex: man
- uname: mary
age: 22
sex: woman
- uname: wangwu
age: 23
sex: man
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat templates/ee.j2
现在公司中所有员工姓名是:
{% for i in users %}
{{i.uname}}
{% endfor %}
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat 5.yaml
---
- hosts: server2
vars_files:
- user_list.txt
tasks:
- name: 拷贝一个文件到远端主机
template: src=ee.j2 dest=/opt/ee.conf
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook 5.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [拷贝一个文件到远端主机] ******************************************************************************************************************
changed: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible server2 -m shell -a "cat /opt/ee.conf"
server2 | CHANGED | rc=0 >>
现在公司中所有员工姓名是:
tom
bob
mary
wangwu
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ egrep -v '#|^#' /etc/httpd/conf/httpd.conf > httpd.conf.j2
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ head -4 httpd.conf.j2
ServerRoot "/etc/httpd"
Listen {{myport}}
[bdqn@RHEL813 demo4]$
[root@RHEL8 ~]# setenforce 0
[root@RHEL8 ~]# getenforce 0
Permissive
[root@RHEL8 ~]#
[bdqn@RHEL813 demo4]$ cat hand-1.yaml
---
- hosts: server2
vars:
myport: 80
tasks:
- name: tasks1安装httpd
yum: name=httpd state=installed
- name: tasks2拷贝配置文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: tasks3启动httpd服务
service: name=httpd state=started
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ ansible-playbook hand-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1安装httpd] ****************************************************************************************************************
ok: [server2]
TASK [tasks2拷贝配置文件] *****************************************************************************************************************
changed: [server2]
TASK [tasks3启动httpd服务] **************************************************************************************************************
changed: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat hand-1.yaml
---
- hosts: server2
vars:
myport: 808
tasks:
- name: tasks1安装httpd
yum: name=httpd state=installed
- name: tasks2拷贝配置文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: tasks3启动httpd服务
service: name=httpd state=started
[bdqn@RHEL813 demo4]$
再次运行此playbook,
[bdqn@RHEL813 demo4]$ cat hand-1.yaml
---
- hosts: server2
vars:
myport: 808
tasks:
- name: tasks1安装httpd
yum: name=httpd state=installed
- name: tasks2拷贝配置文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: tasks3启动httpd服务
service: name=httpd state=restarted
[bdqn@RHEL813 demo4]$
[bdqn@RHEL813 demo4]$ cat hand-1.yaml
---
- hosts: server2
vars:
myport: 808
tasks:
- name: tasks1安装httpd
yum: name=httpd state=installed
- name: tasks2拷贝配置文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: tasks3启动httpd服务
service: name=httpd state=restarted
handlers:
- name: restart httpd1
service: name=httpd state=restarted
- name: restart httpd2
service: name=httpd state=restarted
[bdqn@RHEL813 demo4]$