0%

Ansible part 2: how to use modules - debug copy file template ping gather-facts user group

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~

cube4200 | SUCCESS | rc=0 >>
ansible-file-test.txt
ansible-file-test.txt.11629.2020-05-24@15:58:44~

The command I used is ansible all -m shell -a "ls /etc/tmp"

some examples of how to use modules

Utilities modules -> debug -> print statements during execution

In this module, only three parameters.

1
2
3
4
5
6
7
- name: hello world
hosts: localhost
tasks:
- name: hello world
debug:
msg: "hello ansible"
verbosity: 0 # 0 is default, 1-3 parameters will skip the message

running it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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] *****************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [localhost]

TASK [hello world] *****************************************************************************************************
ok: [localhost] => {
"msg": "hello ansible"
}

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

after running

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/files-modules/invento$ ansible-playbook site.yml

PLAY [file module] *******************************************************************

TASK [mkdir] *************************************************************************
ok: [ansible-node1]
ok: [cube4200]

TASK [copy files from local to remote] ***********************************************
changed: [ansible-node1]
changed: [cube4200]

PLAY RECAP ***************************************************************************
ansible-node1 : ok=2 changed=1 unreachable=0 failed=0
cube4200 : ok=2 changed=1 unreachable=0 failed=0

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

second ways is to use playbook

1
2
3
tasks:
- name: test ping moudle
ping:

both work as following:

1
2
3
4
5
6
7
8
9
10
11
anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/system_modules/inventory$ ansible-playbook site.yml 

PLAY [system module] ************************************************************

TASK [test ping moudle] *********************************************************
ok: [cube4200]
ok: [ansible-node1]

PLAY RECAP **********************************************************************
ansible-node1 : ok=1 changed=0 unreachable=0 failed=0
cube4200 : ok=1 changed=0 unreachable=0 failed=0
system module: gather_facts

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.

1
2
3
4
tasks:
- name: print facts
debug:
msg: "{{ ansible_distribution }}"

after running

1
2
3
4
5
6
7
TASK [print facts] **************************************************************
ok: [cube4200] => {
"msg": "CentOS"
}
ok: [ansible-node1] => {
"msg": "Debian"
}
system module: user and group

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

to be continued