常用模块
1. lineinfile:修改文件某一行文本
- 常用参数:
- path:操作的文件路径
- regexp:正则表达式,要替换的内容
- line:指定修改后的文本内容
- state:当设置为absent代表删除匹配的行
- backrefs:默认为no,当未匹配到时line对应的内容会被插入到文本的末尾,为yes表示不插入
- insertafter:借助insertafter参数可以将文本插入到“指定的行”之后
- insertbefore:借助insertbefore参数可以将文本插入到“指定的行”之前
- backup:是否进行备份
- create:操作的文件不存在时是否创建
简单示例:
- name: 替换文件中包含line123的行为testline lineinfile: path=/tmp/test regexp="^line" line="testline"
示例1:
- hosts: master
remote_user: root
tasks:
- name: replace hosts
lineinfile:
path: /etc/hosts
regexp: "10.10.10.100 www.wanghengzhi.com" # 正则匹配
# state: absent # 匹配到就删除
line: "#10.10.10.100 www.wanghengzhi.com" # 这个表示匹配到后修改,若未匹配上,则增加
- name: test hosts
shell: ping -c2 k8s-apiserver.com
register: result
failed_when: false
- name: "show result"
debug:
msg: "{{result.stdout_lines}}"
执行结果:
[root@master01 ansible]# ansible-playbook MyPlaybook.yaml
PLAY [master] *******************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [192.168.2.210]
ok: [192.168.2.203]
ok: [192.168.2.99]
TASK [replace hosts] ************************************************************************************************************************************************************
changed: [192.168.2.210]
changed: [192.168.2.203]
changed: [192.168.2.99]
TASK [test hosts] ***************************************************************************************************************************************************************
changed: [192.168.2.210]
changed: [192.168.2.203]
changed: [192.168.2.99]
TASK [show result] **************************************************************************************************************************************************************
ok: [192.168.2.99] => {
"msg": "PING k8s-apiserver.com (192.168.2.100) 56(84) bytes of data.\n64 bytes from k8s-apiserver.com (192.168.2.100): icmp_seq=1 ttl=64 time=0.218 ms\n64 bytes from k8s-apiserver.com (192.168.2.100): icmp_seq=2 ttl=64 time=0.208 ms\n\n--- k8s-apiserver.com ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1000ms\nrtt min/avg/max/mdev = 0.208/0.213/0.218/0.005 ms"
}
ok: [192.168.2.203] => {
"msg": "PING k8s-apiserver.com (192.168.2.100) 56(84) bytes of data.\n64 bytes from k8s-apiserver.com (192.168.2.100): icmp_seq=1 ttl=64 time=0.357 ms\n64 bytes from k8s-apiserver.com (192.168.2.100): icmp_seq=2 ttl=64 time=0.292 ms\n\n--- k8s-apiserver.com ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1000ms\nrtt min/avg/max/mdev = 0.292/0.324/0.357/0.037 ms"
}
ok: [192.168.2.210] => {
"msg": "PING k8s-apiserver.com (192.168.2.100) 56(84) bytes of data.\n64 bytes from k8s-apiserver.com (192.168.2.100): icmp_seq=1 ttl=64 time=0.343 ms\n64 bytes from k8s-apiserver.com (192.168.2.100): icmp_seq=2 ttl=64 time=0.275 ms\n\n--- k8s-apiserver.com ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1000ms\nrtt min/avg/max/mdev = 0.275/0.309/0.343/0.034 ms"
}
PLAY RECAP **********************************************************************************************************************************************************************
192.168.2.203 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.210 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.99 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. debug模块
- 常用参数:
- debug调试模块,在调试中输出信息常用参数:
- msg:调试输出的消息;
- var:将某个任务执行的输出作为变量传递给debug模块
示例1:
[root@master01 ansible]# cat test.yaml
- hosts: master
tasks:
- name: Command run line
shell: date
register: return_value
- name: Show debug info
debug:
debug: var=return_value # 也可以使用这种方式,和上面的效果一样
执行结果如下:
[root@master01 ansible]# ansible-playbook test.yaml
PLAY [master] **************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************
ok: [192.168.2.210]
ok: [192.168.2.203]
ok: [192.168.2.99]
TASK [Command run line] ****************************************************************************************************************************************
changed: [192.168.2.210]
changed: [192.168.2.203]
changed: [192.168.2.99]
TASK [Show debug info] *****************************************************************************************************************************************
ok: [192.168.2.99] => {
"msg": {
"changed": true,
"cmd": "date",
"delta": "0:00:00.011500",
"end": "2021-06-23 15:17:02.133336",
"failed": false,
"rc": 0,
"start": "2021-06-23 15:17:02.121836",
"stderr": "",
"stderr_lines": [],
"stdout": "Wed Jun 23 15:17:02 CST 2021",
"stdout_lines": [
"Wed Jun 23 15:17:02 CST 2021"
]
}
}
ok: [192.168.2.203] => {
"msg": {
"changed": true,
……省略部分
示例2:
[root@master01 ansible]# cat test.yaml
- hosts: master
tasks:
- name: Command run line
shell: date
register: return_value
- name: Show debug info
debug:
msg: "{{ return_value.stdout }}" # 可以去掉.stdout,返回所有的debug信息
# debug: var=return_value.stdout 也可以使用这种方式,和上面的效果一样
执行结果
[root@master01 ansible]# ansible-playbook 1.yaml
PLAY [http] *********************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [192.168.2.210]
ok: [192.168.2.203]
ok: [192.168.2.99]
TASK [Command run line] *********************************************************************************************************************************************************
changed: [192.168.2.210]
changed: [192.168.2.203]
changed: [192.168.2.99]
TASK [Show debug info] **********************************************************************************************************************************************************
ok: [192.168.2.99] => {
"msg": "Wed Jun 23 15:37:13 CST 2021"
}
ok: [192.168.2.203] => {
"msg": "Wed Jun 23 15:37:12 CST 2021"
}
ok: [192.168.2.210] => {
"msg": "Wed Jun 23 15:37:12 CST 2021"
}
PLAY RECAP **********************************************************************************************************************************************************************
192.168.2.203 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.210 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.99 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@master01 ansible]# cat test.yaml
- hosts: all
remote_user: root
any_errors_fatal: no
gather_facts: no
serial:
- 5
tasks:
- name: ltp dir is exits
shell: readlink -f /ltp
ignore_errors: True
register: result
- name: Debug
debug:
msg: "{{ result.stdout }}"
- name: create ltp dir
shell: mkdir /ltp
when: result.stdout.find(ltp) = -1
- name: 1.txt file is exits
shell: cat /ltp/1.txt
# ignore_errors: True
register: check_file
when: result.rc = 0
- name: copy packages
copy:
src: /ltp/1.txt
dest: /ltp
owner: root
when: check_file is failed
- name: "View file information"
shell: cat /ltp/1.txt
register: content
- name: Debug
debug:
msg: "{{ content.stout }}"
常用模板
vim /etc/ansible/ansible.cfg
host_key_checking = False # 去掉ssh指纹验证
shell模块常用的几种格式
- hosts: all
remote_user: root
tasks:
- name: 直接执行shell命令
shell: "hostname -I"
- name: 切换目录执行
shell:
cmd: "ls ./"
chdir: "/var"
- name: 脚本的样式执行shell
shell: |
if [ -f /temp ];then
echo "file exist"
else
echo "file is not found"
fi
args:
executable: /bin/bash
copy模块常用的几种格式
- hosts: all
remote_user: root
tasks:
- name: copy文件
copy:
src: /1.txt
dest: 1.txt
owner: root
group: root
mode: u+rw,g-wx,o-rwx
backup: yes
file模块:管理文件和文件属性
- hosts: all
remote_user: root
tasks:
- name: 创建目录
path: /root/test
state: directory
mode: '0755'
- name: 删除文件
file:
path: /root/test/1.txt
state: absent
- name: 递归删除目录
file:
path: /root/test
state: absent
yum模块(latest和present表示安装,absent表示卸载)
- hosts: all
remote_user: root
tasks:
- name: 安装apache
yum:
name: http
state: latest
- name: 安装多个包
yum:
name:
- http
- telnet
state: present
- name: 卸载
yum:
name: http
state: absent
- name: 更新
yum:
name: http
state: latest
- name: 使用远程的repo源
yum:
name: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0-el7.ngx.noarch.rpm
state: present
service/systemd模块
- hosts: all
remote_user: root
tasks:
- name: 启动和关闭
service:
name: http
state: started # stoped/restarted/reloaded
- name: 开机自启
service:
name: http
enabled: yes
- name: systemd管理的
systemd:
name: http
state: restarted
enabled: yes
daemon_reload: yes
unarchive解压
- hosts: all
remote_user: root
tasks:
- name: 解压
unarchive:
src: 'test.tar.gz'
dest: /root
迭代遍历
- hosts: all
remote_user: root
tasks:
- name: 通配符的方式copy文件
copy: src={{item}} dest=/temp
with_fileglob:
- "/temp/*.html"
- name: 遍历字典
copy: src={{item}} dest=/temp
with_dict:
-
template示例
{% for host in groups['master'] %}
masterIp = {{ hostvars[host].inventory_hostname }}
{% end for %}
变量存储
- group_vars:存放组的变量
- groups_vars/all.yaml:表示所有主机有效,相当于
[all:vars]
- groups_vars/test.yaml:表示只对test这个组有效,等同于[test:vars]
inventory_hostname
:该变量表示主机的ip
角色
- role目录结构
- tasks:角色要执行的任务列表
- handles:包含处理程序,此角色此角色之外的任何地方都可以使用这些处理程序
- defaults:角色默认的变量
- files:角色需要copy的一些文件
- templates:存放模板文件
- vars:角色的其他变量