1:yum模块使用如下:
ansible all -m yum -a "name=nginx state=installed" 安装
ansible all -m yum -a "name=nginx state=absent/removed" 卸载
ansible all -m yum -a 'name=nginx disable_gpg_check=yes enablerepo=local' 使用某个源,不检查gpg秘钥
2:service模块使用如下:
ansible all -m service -a "name=nginx state=started/stopped enabled=yes"
3:file模块使用如下:
---
- hosts: all
gather_facts: False
tasks:
- name: Ensuring config directories exist
file: ####调用file模块
path: "{{ item }}" ###使用loops,调用with_items中list出的值
state: "directory" ###新建目录,如果存在不进行任何操作,没有则创建
recurse: true ###递归创建
with_items: ##以下为list的值
- "/etc/os-net-config"
- "/var/lib/os-net-config"
单个ansible命令如下:
ansible ansible-demo3 -m file -a "path=/testdir/testfile1 state=touch
- name: set /var/log/messages only append but modify
file:
attributes: '+a' ###为文件添加属性只能追加数据,不能删除
path: /var/log/messages
其他参数解释如下:
path参数:
用于指定要操作的文件或目录的路径
state参数:
创建目录是指定state=directory; 创建文件时指定state=touch; 创建软连接时指定state=link
创建硬链接时指定state=hard; 删除目录或者文件时指定state=absent;
src参数:
当state设置为link或者hard时,表示我们想要创建一个软链或者硬链,所以,
我们必须指明软链或硬链链接的哪个文件,通过src参数即可指定链接源。
force参数:
当state=link的时候,可配合此参数强制创建链接文件,
当force=yes时,表示强制创建链接文件。
owner参数:
用于指定被操作文件的属主,属主对应的用户必须在远程主机中存在,否则会报错
group参数 :
用于指定被操作文件的属组,属组对应的组必须在远程主机中存在,否则会报错。
mode参数:
用于指定被操作文件的权限
recurse参数:
当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性或者创建目录
4:copy模快
常用参数
src参数 :用于指定需要copy的文件或目录。
dest参数 :用于指定文件将被拷贝到远程主机的哪个目录中,dest为必须参数。
content参数 :当不使用src指定拷贝的文件时,可以使用content直接指定文件内容,
src与content两个参数必有其一,否则会报错。
force参数 : 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,可选值有yes和no,默认值为yes,表示覆盖,
如果设置为no,则不会执行覆盖拷贝操作,远程主机中的文件保持不变
backup参数 : 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否对远程主机的文件进行备份,可选值有yes和no,当设置为yes时,会先备份远程主机中的文件,
然后再将ansible主机中的文件拷贝到远程主机。
owner参数 : 指定文件拷贝到远程主机后的属主,但是远程主机上必须有对应的用户,否则会报错。
group参数 : 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错。
mode参数 : 指定文件拷贝到远程主机后的权限,如果你想将权限设置为”rw-r--r--“,则可以使用mode=0644表示,
如果你想要在user对应的权限位上添加执行权限,则可以使用mode=u+x表示。
---
- hosts;all
tasks:
- name: create locarepo.repo file
copy:
content: |
[local]
name=local repo from http
baseurl=http:///opt/
enabled=1
gpgcheck=0
dest: /etc/yum.repos.d/local.repo
5:facts模块
Ansible facts 是远程系统的信息,主要包含IP地址,操作系统,以太网设备,mac 地址,
时间/日期相关数据,硬件信息等信息。
如何获取facts信息:
ansible all -m setup
设置facts缓存
gather_facts:no ##表示不搜集facts信息
Ansible 的配置文件中可以修改 gathering 的值为 smart、implicit 或者 explicit。
smart 表示默认收集 facts,但 facts 已有的情况下不会收集,即使用缓存 facts;
implicit 表示默认收集 facts,要禁止收集,必须使用 gather_facts: False;
explicit 则表示默认不收集,要显式收集,必须使用 gather_facts: Ture。
可以在ansible.cfg添加如下内容设置facts缓存到内存中
gathering = smart
fact_caching = yaml
fact_caching_connection = /tmp/ansible_fact_cache
fact_caching_timeout = 300
6:mysql模块
如下:
---
- hosts: control
tasks:
- name: init the mariadb ###初始化数据库
mysql_user:
user: root
password: 1234567890
update_password: always
when: ansible_default_ipv4.address == "{{ groups['all'].0 }}"
- name: create test db
mysql_db:
login_user: root ###登录数据库的用户名
login_password: 1234567890 ###登录密码
login_host: 192.168.124.129 ###登录主机
login_port: 3306 ###登录port
name: test ###需要创建的db name
state: present ###默认是present(创建)
- name: create haproxy user
mysql_user:
login_user: 'root'
login_password: '1234567890'
login_host: 192.168.124.129
user: 'haproxy'
password: '123456'
priv: '*.*:ALL' ###为用户授权
state: present
login_host之后接具体的ip(不可为localhost【会被解析成127.0.0.1】,或者127.0.0.1)
具体区别如下:
当设置为127.0.0.1时,数据库通过tcp的方式进行连接
设置为localhost时,数据库通过socket连接。
7:meta
- name: flush handlers
meta: flush_handlers
meta: flush_handlers 表示立即执行task对应的handler,
因为handler默认要等所有任务都执行完成才执行
8: systemd
- name: restart docker service
systemd:
name: docker
state: restarted
daemon_reload: true
主要参数如下:
name: 服务名,例如crond.service,最好带上后缀.service
state: 需要的操作,reloaded, restarted, started, stopped
enabled:是否需要开机启动
daemon_reload:systemd 读取配置文件,每次修改了文件,最好都运行一次,确保应用了
9: openvswich_db的使用方法
- name: Initialize DPDK
openvswitch_db:
table: Open_vSwitch
record: .
col: other_config
key: dpdk-init
value: "true"
state: "{{ 'present' if enable_ovs_dpdk | bool else 'absent' }}"
become: true
notify:
- restart openvswitch service
when: enable_ovs_dpdk | bool
- name: Specify the hugepage memory of the DPDK
openvswitch_db:
table: Open_vSwitch
record: .
col: other_config
key: dpdk-socket-mem
value: "{{ dpdk_socket_mem }}"
state: "{{ 'present' if enable_ovs_dpdk | bool else 'absent' }}"
become: true
notify:
- restart openvswitch service
when: enable_ovs_dpdk | bool
10:docker_container模块使用
- name: start ironic conductor container
docker_container:
name: "{{ ironic_conductor_container_name }}" ##容器名字
image: "{{ image_full }}" ##容器image ID
network_mode: host ###网络模式
working_dir: /root
state: present/absent/started/stopped
restart_policy: unless-stopped ##重启策略
pull: "{{ True if image_pull|bool else False }}" ##如果指定的image无效则会自动下载最新image
command: ##传递的命令
- ironic-conductor
- sleep infinity
env: ##传递的变量
PS1: '(ironic_conductor)[\u@\h \W]\$ '
volumes: ###映射的卷
- /etc/localtime:/etc/localtime:ro
- "{{ config_root }}/ironic/ironic.conf:/etc/ironic/ironic.conf:ro"
- /var/lib/minironic/custom:/var/lib/ironic/public/custom
- /var/lib/minironic/boot:/var/lib/ironic/public/boot
- /var/lib/minironic/database:/var/lib/ironic/database
- /var/log/minironic:/var/log/ironic
- /tftpboot:/tftpboot
- /httpboot:/httpboot
ports:
- "800:800" ###端口映射
11:wait_for使用
- name: sleep for 120 seconds and continue with play
wait_for:
timeout: 120
本任务延迟120秒之后执行下一个任务,只能在当前指定的host执行,不能使用delegate_to
12:set_fact使用
set_fact模块可以自定义facts,这些自定义的facts可以通过template或者变量的方式
在playbook中使用
1. 在tasks内部调用变量,使用set_fact
[root@server4 ~]# cat bl2.yml
---
- hosts: testB
remote_user: root
tasks:
- set_fact: # 在tasks内部调用变量,使用set_fact
testvar: "test"
- debug:
msg: "{{testvar}}"
2. 使用set_fact调用register中的值
当shell执行完后,使用register获取shell的运行结果存放到shellreturn这个变量中,shellreturn是一个key value字典,输出形式为shellreturn.stdout
[root@server4 ~]# cat bl3.yml
---
- hosts: testB
remote_user: root
vars:
testvar1: test1
tasks:
- shell: "echo test2"
register: shellreturn
- set_fact:
testf1: "{{testvar1}}"
testf2: "{{shellreturn.stdout}}"
- debug:
msg: "{{testf1}} {{testf2}}"
3. 只有set_fact定义的变量才能在不同的play中应用,而vars定义的变量不能在不同的play中应用
[root@server4 ~]# cat bl4.yml
---
- hosts: testB
remote_user: root
vars:
testvar1: tv1 # 不可以被调用
tasks:
- set_fact:
testvar2: tv2 # 可以备调用
- debug:
msg: "{{testvar1}}----{{testvar2}}"
- hosts: testB
remote_user: root
tasks:
- name: get testvar2
debug:
msg: "{{testvar2}}"
- name: get testvar1
debug:
msg: "{{testvar1}}"
13:run_once使用
通过run_once: true来指定该task只能在某一台机器上执行一次. 可以和delegate_to 结合使用
- command: /opt/application/upgrade_db.py
run_once: true
# connection: local ###和delegate_to用法一样
delegate_to: web01.example.org
如果没有delegate_to, 那么这个task会在第一台机器上执行
14:fetch使用
用途:用于从 远程主机 中拷贝文件到 管理主机
- name: Store file into /tmp/fetched/host.example.com/tmp/somefile
fetch:
src: /tmp/somefile
dest: /tmp/fetched
- name: Specifying a path directly
fetch:
src: /tmp/somefile
dest: /tmp/prefix-{{ inventory_hostname }}
flat: yes
15:git使用方法
- name: Get updated files from git repository
git:
repo: "https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git"
dest: /tmp
version: ###可以使用commit ID或者tag
注意 {{ githubpassword | urlencode }} is used here, 如果你的密码中包含特殊字符
@,#,$ ,需要使用这种格式**{{ githubpassword | urlencode }}**
执行playbook如下,传入用户名和密码的变量
ansible-playbook -i hosts github.yml -e "githubuser=arbabname" -e "githubpassword=123456"