Understanding Configuration Changes to Ansible Multiple Targets

Asked 1 years ago, Updated 1 years ago, 111 views

I am currently studying Ansible.Programming is almost beginner level.

Verification is underway to allow Ansible to make configuration changes to multiple Linux servers.
·I would like to set different settings for multiple targets and restart the service and OS.
·In the future, we will create playbopok from Excel so that users can easily do it

I thought of the following method, but it doesn't work.
It is a method of making the destination address a variable, creating it in a separate file, and calling it.
I need the file for the target, but I thought it would be easy to create.
If you put the parameter values of the module as variables in the file you want to call, the same call will not work.

The actual syntax is listed below.I wonder if it is possible to store the set value together with this content as a variable.Or do I need to call the files/variables in the module...
If you are familiar with programming and configuration management tools, could you give me some advice?

 ·inventory file
Client01

·Client01.yml
---
ipaddr —192.168.0.1
new_ipaddr —172.16.0.10
new_prefix —24
new_gateway —172.16.0.200

·playbook.yml
---
- hosts —all
  vars_files:
    - vars /{{ansable_hostname}}.yml
  tasks:
  - name — ipv4 configuration
 lineinfile:
    dest: /etc/sysconfig/network-scripts/ifcfg-eth0
    line: "{{item}}"
  with_items:
    - IPADDR="new_ipaddr"
    - PREFIX="new_prefix"
    - GATEWAY="new_gateway"

●Additional information
At first, I thought it would be easy to use import_playbook, but I couldn't specify the host in the playbook from which I read it.
I thought it would be easy if I changed the target and necessary reading playbook to the source without messing with the basics of reading, but it won't be easy...

python linux ansible yaml configuration-management

2022-09-29 22:30

1 Answers

Host_vars is available for values that change per host (per target).

By leaving a yaml file in the host_vars directory with the same file name as the one you wrote in the inventory, you can use the yaml file settings in host_vars when you target the target host.

I think the values that change per host (per target) can be addressed in this way, but I think there is another problem with changing the IP address of the connected host as mentioned in the question.

./inventory

[servers]
client01 answerable_host=192.168.56.30 answerable_user=ansible
client02ansable_host=192.168.56.31ansable_user=ansible

If you want to change the IP address, I think it would be easier to make it look like the IP address of the management LAN for the answer and the actual operating LAN to be configured... (even if it's different from what you want to do)

./playbook.yml

---
- hosts —all

  tasks:
  - name —test setting.
    lineinfile:
      dest: "/tmp/test_setting"
      line: "{{item}}"
      create —Yes
    with_items:
    - IPADDR="{{new_ipaddr}}"
    - PREFIX="{{new_prefix}}"
    - GATEWAY="{{new_gateway}}"

./host_vars/client01.yml

---
new_ipaddr —172.16.1.10
new_prefix —24
new_gateway —172.16.1.200

./host_vars/client01.yml

---
new_ipaddr —172.16.2.10
new_prefix —24
new_gateway —172.16.2.200

Run playbook

$ansable-playbook-i inventory playbook.yml

PLAY [all] ********************************************************************************************************************************************************************

TASK [Gathering Fact] **************************************************************************
ok: [client02]
ok: [client01]

TASK [test setting.] ************************************************************************
changed: [client02] = > (item=IPADDR="172.16.2.10")
changed: [client01] = > (item=IPADDR="172.16.1.10")
changed: [client02] = > (item=PREFIX="24")
changed: [client01] = > (item=PREFIX="24")
changed: [client02] = > (item=GATEWAY="172.16.2.200")
changed: [client01] = > (item=GATEWAY="172.16.1.200")

PLAY RECAP *******************************************************
client01:ok=2changed=1 unreachable=0 failed=0 skipped=0 reserved=0 ignored=0 ignored 00 ignored=0
client02: ok = 2 changed = 1 unreachable = 0 failed = 0 skipped = 0 reserved = 0 ignored = 0


2022-09-29 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.