# by this action

Here "this action" refers to David's plan to count all the men of Israel who are able to fight.

# so he attacked Israel

The nature of this attack is not clear. It was apparently enough to cause David to realize that Yahweh was angry with him for counting the people.