一、如何使用模块
使用 ansible 完成实际任务时,需要依靠 ansible 的各个模块,比如,我们想要去 ping 某主机,则需要使用 ping 模块命令如下:
[root@Ansible ansible]# ansible all -m ping
ansible 还有很多模块可供我们使用。我们可以使用如下命令,查看 ansible 都有哪些模块:
[root@ansible-manager ~]# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' server object.
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
a10_service_group Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' service groups.
a10_virtual_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' virtual servers.
accelerate Enable accelerated mode on remote node
aci_aaa_user Manage AAA users (aaa:User)
aci_aaa_user_certificate Manage AAA user certificates (aaa:UserCert)
aci_access_port_to_interface_policy_leaf_profile Manage Fabric interface policy leaf profile interface selectors (infra:HPortS, infra:RsAccBaseGrp, infra:PortBlk)
aci_aep Manage attachable Access Entity Profile (AEP) objects (infra:AttEntityP, infra:ProvAcc)
aci_aep_to_domain Bind AEPs to Physical or Virtual Domains (infra:RsDomP)
aci_ap Manage top level Application Profile (AP) objects (fv:Ap)
aci_bd Manage Bridge Domains (BD) objects (fv:BD)
aci_bd_subnet Manage Subnets (fv:Subnet)
aci_bd_to_l3out Bind Bridge Domain to L3 Out (fv:RsBDToOut)
......
通过ansible-doc -l命令获取到的模块信息比较概括,并不是特别详细。
如想模块更加详细的用法,可以使用ansible-doc -s命令。
[root@Ansible ~]# ansible-doc -s fetch
- name: Fetch files from remote nodes
fetch:
dest: # (required) A directory to save the file into. For example, if the `dest' directory is `/backup' a `src' file named `/etc/profile' on host `host.example.com', would be saved into
`/backup/host.example.com/etc/profile'. The host name is based on the inventory name.
fail_on_missing: # When set to `yes', the task will fail if the remote file cannot be read for any reason. Prior to Ansible 2.5, setting this would only fail if the source file was missing. The default was changed to
`yes' in Ansible 2.5.
flat: # Allows you to override the default behavior of appending hostname/path/to/file to the destination. If `dest' ends with '/', it will use the basename of the source file, similar to the copy module. This
can be useful if working with a single host, or if retrieving files that are uniquely named per host. If using multiple hosts with the same filename, the file will be
overwritten for each host.
src: # (required) The file on the remote system to fetch. This `must' be a file, not a directory. Recursive fetching may be supported in a later release.
validate_checksum: # Verify that the source and destination checksums match after the files are fetched.
如果忘记相关用法可以使用:
[root@Ansible ~]# ansible-doc -v fetch
...
EXAMPLES:
- 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
- name: Specifying a destination path
fetch:
src: /tmp/uniquefile
dest: /tmp/special/
flat: yes
- name: Storing in a path relative to the playbook
fetch:
src: /tmp/uniquefile
dest: special/prefix-{{ inventory_hostname }}
flat: yes
假如我们想要将group1 组中所有主机的 /test文件拉取到本地,则可以使用如下命令:
- 主机清单配置:
[root@Ansible test]# tail -n 3 /etc/ansible/hosts
[group1]
192.168.0.160
192.168.0.161
- 测试文件:
192.168.0.160主机文件
[root@CentOSA ~]# cd /test/
[root@CentOSA test]# echo "CentOSA">>t1.txt
[root@CentOSA test]# cat t1.txt
CentOSA
192.168.0.161主机文件
[root@CentOSB ~]# cd /test/
[root@CentOSB test]# echo "CentOSB">>t1.txt
[root@CentOSB test]# cat t1.txt
CentOSB
如下述命令所示。
- -m 选项用于调用指定的模块,”-m fetch“表示调用 fetch 模块.
- -a 选项用于传递模块所需要使用的参数, -a “src=/etc/hosts dest=/test/” 表示我们在使fetch 模块时,为 fetch 模块传入了两个参数,src 与 dest。
[root@Ansible test]# ansible group1 -m fetch -a "src=/test/t1.txt dest=/test/"
- 运行结果:
二、关于幂等性
ansible 具有幂等性,幂等性能够保证我们重复的执行一项操作时,得到的结果是相同的。
- 当返回信息为绿色时,”changed” 为 false,表示 ansible 没有进行任何操作,没有”改变什么”。
- 当返回信息为黄色时,”changed” 为 true,表示 ansible 执行了操作,”当前状态”已经被 ansible改变成了”目标状态”。
- ansible 进行 fetch 操作时,会对对应文件进行哈希计算,算出文件哈希值。改变了文件中的内容,哈希值也将随之发生改变。
三、取消目录指定位置
会在本地进行目录创建
[root@Ansible ~]# tree /test/
/test/
├── 192.168.0.160
│ └── test
│ └── t1.txt
└── 192.168.0.161
└── test
└── t1.txt
4 directories, 2 files
如下述命令所示。
[root@Ansible test01]# ansible group1 -m fetch -a "src=/test/t1.txt dest=/test01/{{ inventory_hostname }}-t1.txt flat=yes"
[root@Ansible test01]# tree /test01/
/test01/
├── 192.168.0.160-t1.txt
└── 192.168.0.161-t1.txt
0 directories, 2 files
[root@Ansible test01]# cat 192.168.0.160-t1.txt
CentOSA
[root@Ansible test01]# cat 192.168.0.161-t1.txt
CentOSB