Ansible part 2: how to use modules - debug copy file template ping gather-facts user group
Posted onEdited onViews: Disqus:
Ansible configuration
Before running ansible,I am going to write a default inventory which will be used when I am running site.yml.
1 2
[defaults] inventory = hosts
Ansible commands
Instead of using ansible playbook, I can also use ansible command to run automation, for example, I am going to check remote nodes directories, I can use ansible command to do that directly, no need to login romote node to check.
1 2 3 4 5 6 7 8
anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/files-modules/inventory$ ansible all -m shell -a "ls /etc/tmp" ansible-node1 | SUCCESS | rc=0 >> ansible-file-test.txt ansible-file-test.txt.16075.2020-05-24@22:58:43~
anna@ansible-controller:~/ansible-code/inventory$ ansible-playbook helloworld.yml [WARNING]: * Failed to parse /etc/ansible/hosts with yaml plugin: YAML inventory has invalid structure, it should be a dictionary, got: <class 'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: * Failed to parse /etc/ansible/hosts with ini plugin: /etc/ansible/hosts:44: Expected key=value host variable assignment, got: ansible-controller
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [hello world] *****************************************************************************************************
PLAY RECAP ************************************************************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0
files modules: copy and file
I am going to create a directory and copy some files to remote nodes, so I am going to use two modules, copy and file which will create directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- name: file module hosts: all gather_facts: no become: yes # sudo to root
tasks: - name: mkdir file: path: /etc/tmp/ # etc dir needs root right state: directory
- name: copy files from local to remote copy: src: files/ansible-file-test.txt dest: /etc/tmp/ansible-file-test.txt backup: yes # will keep the same name file not be overwrite,give file a time tag
file modules create a dir named tmp under /etc, since I sudo to root, so I can create dir under etc. after dir is created, I copied my local file named ansible-file-test.txt to node’ destination dir.
template modules: template
template is like a different copy, in which data is got by variables. Usaually template file is wrote by jinja, so the end of file is .j2. following is a template file named template-demo.j2
1 2 3 4 5 6
[default] ansible_user = {{ ansible_user }} [demo] {% for ip in range(201, 220) %} {{ host_prefix }}{{ "%02d" | format(id-200)}}-{{ ip_prefix}}.{{ ip }} {% endfor %}
this is template playbook
1 2 3 4
- name: template test template: src: templates/template-demo.j2 dest: /etc/tmp/test.cfg
system module: ping
There are two ways to ping hosts, first use command line ansible -m ping all -i inventory
In command line, use ansible -m gather_facts -i inventory --tree ./facts, which will get all info of nodes and save it to file which is named facts. If I need the info from facts I can just call it in playbook. For example, I am going to print out the nodes’s distribution.
Notice: the default of gather_facts is yes, which means, it will get all info if you did not specify it is No.
manage user and group in linux, such as add new user and group or delete them.
1 2 3 4 5 6 7 8 9 10 11 12 13
tasks: - name: create group group: name: ansible_demo state: absent # this is default state - name: create user user: name: demo - name: delete user user: name: demo state: absent # will not delete home dir remove: yes # add this command will delete user home dir