Run a specified role in ansible

Let's run a specified role in Ansible

install lldpd by ansible roles

ansible layout

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@03e6e45a7f92 cc]# tree ansible
ansible
├── roles
│   └── lldp
│       ├── defaults
│       │   └── main.yml
│       ├── files
│       └── tasks
│           ├── disable_lldp.yml
│           ├── enable_lldp.yml
│           └── main.yml
└── site.yml

5 directories, 5 files

show the content

site.yml

1
2
3
4
5
6
---
-   hosts: localhost
    name: lldpd installation
    roles:
        -   role: lldp
            tags: lldp

Or like this

1
2
3
4
5
6
7
---
-   hosts: localhost
    name: lldpd installation
    tasks:
        -   import_role:
                name: lldp
            tags: lldp

defaults/main.yml

1
2
---
LLDPD_VERSION: 1.0.5

tasks/main.yml

1
2
---
-   include: enable_lldp.yml

tasks/enable_lldp.yml

 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
-   name: Retrieve lldpd source code
    get_url:
        # TODO replace it
        url: https://media.luffy.cx/files/lldpd/lldpd-{{ LLDPD_VERSION }}.tar.gz
        dest: /tmp/lldpd-{{ LLDPD_VERSION }}.tar.gz
-   name: Extract archive
    unarchive:
        # if configured like as the following, 'Retrieve lldpd source code' task can be removed
        # src: https://media.luffy.cx/files/lldpd/lldpd-{{ LLDPD_VERSION }}.tar.gz
        src: /tmp/lldpd-{{ LLDPD_VERSION }}.tar.gz
        dest: /tmp
-   name: Configure install
    command: ./configure
    args:
        chdir: /tmp/lldpd-{{ LLDPD_VERSION }}
        creates: /tmp/lldpd-{{ LLDPD_VERSION }}/configure.status.log
-   name: Build lldpd
    command: make
    args:
        chdir: /tmp/lldpd-{{ LLDPD_VERSION }}
        creates: /tmp/lldpd-{{ LLDPD_VERSION }}/make.status.log
-   name: Install lldpd
    become: true
    command: make install
    args:
        chdir: /tmp/lldpd-{{ LLDPD_VERSION }}
        creates: /tmp/lldpd-{{ LLDPD_VERSION }}/make_install.status.log
-   name: Create chroot for lldpd
    file:
        path: /usr/local/var/run/lldpd
        state: directory
-   name: Create _lldpd group
    group:
        name: _lldpd
        state: present
-   name: Create _lldpd user
    user:
        name: _lldpd
        group: _lldpd
        comment: lldpd user
-   name: Remove build directory
    file:
        path: /tmp/lldpd-{{ LLDPD_VERSION }}
        state: absent
-   name: Remove archive
    file:
        path: /tmp/lldpd-{{ LLDPD_VERSION }}.tar.gz
        state: absent
-   name: Create lldpd.conf
    file:
        path: /etc/lldpd.conf
        state: touch
-   name: Configure lldpd.conf
    blockinfile:
        path: /etc/lldpd.conf
        block: |
            # Reference from https://vincentbernat.github.io/lldpd/usage.html
            configure system chassisid <chassisid>
            configure system hostname <hostname>
            configure system description <description>
            configure system platform <platform>

            configure system interface pattern eth0,eth1
            configure system interface permanent eth0,eth1

            # rx-and-tx: receive and transmit LLDP frames
            # tx-only:
            # rx-only:
            # disabled:
            configure ports eth0,eth1 lldp status tx-only

            # TTL is tx-interval * tx-hold, i.e. 120 seconds
            configure lldp tx-interval <number, default is 30 seconds>
            configure lldp tx-hold <number, default is 4>            
-   name: Enable lldpd service
    systemd:
        name: lldpd
        enabled: true
        masked: false
-   name: Start lldpd service
    systemd:
        name: lldpd
        state: started

run

1
2
3
ansible-playbook site.yml
# or only run role: lldp
ansible-playbook site.yml -t lldp
Licensed under CC BY-NC-SA 4.0
Last updated on Aug 18, 2023 13:13 UTC