ansible 模板使用语法:
1. 使服务器执行一个shell 命令
- name: execute a shell command (ansible 任务名称,可写可不写,写了直观,同时可以直接根据任务名称定位到 任务行)
shell : hostname (表示 在服务器上执行 hostname 这条命令)
或
- shell: hostname
2. copy 复制文件,从服务器本地到本地 或 从ansible server 服务器 到 被操作的服务器本地。
- name: Get EPEL 任务名称
copy:
src: epel-release-6-8.noarch.rpm (cp 的源文件的位置,不加绝对路径,代表在ansible 服务器,位置位于 执行的yml 文件所在的roles 下的 file 目录下 )
dest: /tmp/epel-release-6-8.noarch.rpm
when: ansible_distribution_major_version == '6' (判断条件,但服务器版本是6 才执行 cp 这条任务,即名称是 Get EPEL 这个的这条任务 )
3.yum ,用yum 进行包操作,如 install,update ,remove
- name: disable selinux ()
yum:
name: libselinux-python (包名 也可跟 一个rpm 绝对路径,代表安装本地下载下来的rpm 包,而非从yum 源来安装)
state: present
(state参数:用于指定软件包的状态 ,默认值为。present,表示确保软件包已经安装,除了 present,其他可用值有 installed、latest、absent、removed,其中 installed 与present 等效,latest 表示安装 yum 中最新的版本,absent 和 removed 等效,表示删除对应的软件包。)
4. item ,当执行的参数有多个时 使用,举例。
- name: Install common package
yum:
name: "{{ item }}"
state: latest
with_items:
- ntpdate
- ntp
5.get_url 下载url 到指定目录
- name: Get url
get_url:
url: url
dest: /tmp/ (直接跟目录,下载下的文件名字不重新命令,也可加新的文件名字,代表重命名。如 /tmp/newname )
url_username:
url_password:
owner: root
group: root
mode: 0755
6 service ,对 服务进行操作,如重启某服务,
- name: config named-chroot
service:
name: named
state: stopped
enabled: no
7 lineinfile: 类似于sed ,向文件末尾插入文本或 替换文本,
- name: modify resolv.conf
lineinfile:
regexp: "nameserver" (正则匹配)
line: "nameserver 114.114.114.114" (替换后的内容)
dest: /etc/resolv.conf
backrefs: yes (匹配失败,不插入,不替换)
8 file ,新建文件,目录,做软链等等
ansible all -m file -a "path=/IT state=directory owner=it" 创建IT目录,并制定属主是it ansible all -m file -a "path=/tmp/IT.txt state=touch mode=777" 创建文件IT.txt 并指定权限 ansible all -m file -a "path=/tmp/cron src=/var/log/cron state=link" 创建软连接,连接是自己本机的文件前面的path是连接存放地址,后面的src是源文件地址 ansible all -m file -a "path=/tmp/cron state=absent" 删除软连接 ansible all -m file -a "path=/IT state=absent"
9 var and ansible 的setup
- name: Modify xinetd nrpe config
lineinfile:
dest: /etc/xinetd.d/nrpe
regexp: "only_from"
line: " only_from = 127.0.0.1 {{ net_mask_bond1 | ipaddr('network') }} {{ net_mask_bond0 | ipaddr('network') }}"
backrefs: yes
vars:
net_mask_bond1: "{{ ansible_bond1.ipv4.network }}/{{ ansible_bond1.ipv4.netmask }}" (服务器的属性参数)
net_mask_bond0: "{{ ansible_bond0.ipv4.network }}/{{ ansible_bond0.ipv4.netmask }}"
tags: fetch
ipaddr('network') 网段 ipaddr('ipaddress') IP ipaddr('prefix') 数字表示的掩码 ipaddr('netmask') IP段表示的掩码
10 debug 调试用信息
debug:
msg: "bond1: {{ net_mask_bond1 |ipaddr('network') }} ; bond2:{{ net_mask_bond1 | ipaddr('address') }} "
vars:
net_mask_bond1: 192.168.1.2/24
11 setup 收集远程主机的一些系统参数信息 ,用法:
ansible ansible-demo3 -m setup
ansible ansible-demo3 -m setup -a "filter=ansible_bond0.ipv4.addresses"
12 ignore_errors : 是否忽略本条任务执行错误
如果某一条任务 ,允许执行错误的情况下也继续往下执行,加这个参数 yes or no 如
- name: Stop ssh service
service:
name: sshd
state: stopped
enabled: no
ignore_errors: yes
即使这条命令 执行错误 如 sshd 服务 停止失败,也继续执行其他任务,如果不加ignore ,ansible 会在任务执行过程中某一条任务执行报错, 退出。
13 import
- import_role:
name: common: 先执行roles 下为common 的任务。
- import_tasks: update_kernel.yml 执行名字为 update_kernel.yml 的任务先
14 url POST 请求
- name: Update rms virtualserver ip
uri:
url: "https://www.test.com/index.html?access_id=hpcc×tamp={{ ansible_date_time.epoch }}&token={{ rms_string | hash('md5') }}"
method: POST
headers:
Content-Type: "application/json"
body: '{"operation_type": "update","device_name": {{VIP}} }'
body_format: json
validate_certs: no
vars:
rms_string: hpcc338a5b1880c42e3161d32b34008f71a3{{ ansible_date_time.epoch }}
VIP: "{{ groups['Vip'][0] }}"
when: NodeType == 'test1'
15 fetch 将远程服务器的一些信息拉取到ansible 服务器所在的本地。
ansible all -m fetch -a "src=/root/test.sh dest=/root/test"
16 template : 可以将带有参数的配置文件(本地)传递到目标地址
参考 :https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-gm1w27pd.html
17 register :
参考:https://blog.csdn.net/qianggezhishen/article/details/53939188
来源:CSDN
作者:blackfwhite
链接:https://blog.csdn.net/blackfwhite/article/details/103576655