ansible Note (3): Basic module using ansible

1.ping module

  [Root @ ansible-control /] # host in the group ansible A -m ping #pingA

  [root@ansible-control /]# ansible 192.168.232.182 -m ping

  [Root @ ansible-control /] # ansible aliastest -m ping # to host controlled using an alias, but note that you want to add ansible_host = IP address

2. Module queries

  2.1 query all available modules

    [root@ansible-control /]# ansible-doc  -l

  2.2 Query detailed usage and introduce a module   

    [root@ansible-control /]# ansible-doc -s ping

3.fetch module

  Action: receiving from the host pulls control file. Optional parameters: dest, fail_on_missing, flat, src, validate_checksum

    src: indicates which file (the file path) by pulling from the host control

    dest: Specifies the control file is stored in the host locally pulled position

  Example module 3.1fetch   

    [Root @ ansible-control /] # ansible A -m fetch -a "src = / fetch / test.txt dest = / test" # -m indicates that the call module, -a parameter indication which

See ansible by host control terminal, can be found, has been successfully controlled to pull the distal end of host file /fetch/test.txt ansible taken to the control terminal.

  3.2fetch module generates a hash value file

  从返回信息中可以看到,当ansible进行fetch操作时,会对对应文件进行哈希计算,算出文件哈希值,也就是说,如果我们改变了文件中的内容,哈希值也将随之发生改变,这个时候,即使对应目录中存在同名的文件,ansible也会判断出两个文件属于不同的文件,因为它们的哈希值并不相同,我们来实验一下,操作如下:

【修改受控主机中/fetch/test.txt文件的内容之前在ansible控制主机中拉取该文件时】

【修改受控主机中/fetch/test.txt文件的内容后再次在ansible控制主机中拉取该文件后】

 4.ansible的幂等性

  什么是幂等性?举个例子,你想把一个文件拷贝到目标主机的某个目录上,但是你不确定此目录中是否已经存在此文件,当你使用ansible完成这项任务时,就非常简单了,因为如果目标主机的对应目录中已经存在此文件,那么ansible则不会进行任何操作,如果目标主机的对应目录中并不存在此文件,ansible就会将文件拷贝到对应目录中,说白了,ansible是以结果为导向的,我们指定了一个目标状态ansible会自动判断,当前状态是否与目标状态一致,如果一致,则不进行任何操作,如果不一致,那么就将;当前状态变成目标状态,这就是幂等性,幂等性可以保证我们重复的执行同一项操作时,得到的结果是一样的。

 现在我们来实验一下,重复输入两条相同的ansible命令,看一看有什么不同:[root@ansible-control fetch]# ansible A -m fetch -a "src=/fetch/test.txt dest=/test"

重复输入相同ansible命令后:

通过查看返回信息,发现changed字段中的值为true时发生了实质性改变(第一次输入该命令时);changed字段中的值为false时并没有什么改变,因为本地已经存在该文件(第二次输入该命令时)

 

 

 

    

Guess you like

Origin www.cnblogs.com/python-wen/p/11230972.html